1

I'm trying to insert a Pandas DataFrame into Mongodb using PyMongo.

df.head()

enter image description here

Because the index if the Pandas DataFrame is a DatetimeIndex, converting the DataFrame to a dict and inserting it to Mongodb:

db.testCollection.insert( df.T.to_dict() )

gives rise to an error:

InvalidDocument: documents must have only string keys, key was Timestamp('2016-04-07 09:30:00')

How can we convert the DatetimeIndex to something else that can be inserted into Mongodb, and later still be able to be converted back to a DatetimeIndex when reading from Mongodb?

1 Answer 1

2

A solution would be that you turn index to str before attempting to store away in MongoDB, like this:

>> df.index = df.index.astype(str)
>> db.testCollection.insert(df.T.to_dict())

When reading the data out of db again later you can turn index to timestamp:

>> df.index = pd.to_datetime(df.index)

I hope this helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.