Insert Pandas Timestamp into Mongodb

寵の児 提交于 2019-12-11 13:18:40

问题


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



来源:https://stackoverflow.com/questions/36902291/insert-pandas-timestamp-into-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!