Insert Pandas Timestamp into Mongodb

2019-07-29 14:57发布

问题:

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

df.head()

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:

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