Pymongo - ValueError: NaTType does not support utcoffset when using insert_many

泪湿孤枕 提交于 2020-02-02 14:15:11

问题


I am trying to incrementally copy documents from one database to another.

Some fields contain date time values in the following format:

2016-09-22 00:00:00

while others are in this format:

2016-09-27 09:03:08.988

I extract and insert the documents like so:

pd.DataFrame(list(db_prod.db_name.collction_name.find({'_updated_at': {'$gt': last_added_timestamp}}).sort('_updated_at', 1)))
add = (df.to_dict('records'))

try:
    db_incremental.other_db.collection_name.insert_many(add)
except BulkWriteError as bwe:
    print(bwe.details)

here is the error:

  File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 684, in insert_many
    blk.execute(self.write_concern.document)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/bulk.py", line 470, in execute
    return self.execute_command(sock_info, generator, write_concern)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/bulk.py", line 302, in execute_command
    run.ops, True, self.collection.codec_options, bwc)
  File "pandas/tslib.pyx", line 663, in pandas.tslib._make_error_func.f (pandas/tslib.c:14736)
ValueError: NaTType does not support utcoffset

I dont actually need to modify the timestamps, just insert them as they are.

Any help appreciated.


回答1:


Replace it with None values which can be interpreted by pandas

df[['_updated_at']] = df[['_updated_at']].astype(object).where(df[['_updated_at']].notnull(), None)


来源:https://stackoverflow.com/questions/42228292/pymongo-valueerror-nattype-does-not-support-utcoffset-when-using-insert-many

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