使用插入pandas DataFrame到mongodb的最快方法是什么PyMongo?
PyMongo
尝试次数
db.myCollection.insert(df.to_dict())
给出了一个错误
InvalidDocument: documents must have only string keys, the key was Timestamp('2013-11-23 13:31:00', tz=None)
db.myCollection.insert(df.to_json())
TypeError: 'str' object does not support item assignment
db.myCollection.insert({id: df.to_json()})
InvalidDocument: documents must have only string a keys, key was <built-in function id>
df
<class 'pandas.core.frame.DataFrame'> DatetimeIndex: 150 entries, 2013-11-23 13:31:26 to 2013-11-23 13:24:07 Data columns (total 3 columns): amount 150 non-null values price 150 non-null values tid 150 non-null values dtypes: float64(2), int64(1)
我怀疑是否有 最快 和 最 简单的 方法。如果您不担心数据转换,可以
>>> import json >>> df = pd.DataFrame.from_dict({'A': {1: datetime.datetime.now()}}) >>> df A 1 2013-11-23 21:14:34.118531 >>> records = json.loads(df.T.to_json()).values() >>> db.myCollection.insert(records)
但是,如果您尝试将数据加载回,则会得到:
>>> df = read_mongo(db, 'myCollection') >>> df A 0 1385241274118531000 >>> df.dtypes A int64 dtype: object
所以你必须将“A” columnt回datetimeS,以及所有不int,float或str字段您DataFrame。对于此示例:
datetime
int
float
str
DataFrame
>>> df['A'] = pd.to_datetime(df['A']) >>> df A 0 2013-11-23 21:14:34.118531