Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 295
archienorman Avatar asked Feb 14 '17 14:02

archienorman


2 Answers

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)
like image 130
gies0r Avatar answered Sep 20 '22 17:09

gies0r


Apparently it worked for me by using

df.fillna("-",inplace=True)
like image 31
R.singh Avatar answered Sep 20 '22 17:09

R.singh