if (constant.gc in file.sheet_names):
coll = db[constant.gc]
print("Adding to " + constant.gc + " database")
df = file.parse(constant.gc)
df = clean(df)
data_dict = df.to_dict('r')
try:
result = coll.insert_many(data_dict)
nr_inserts = len(result.inserted_ids)
print(str(nr_inserts) + "Cases added to database")
except pymongo.errors.BulkWriteError as bwe:
nr_inserts = bwe.details["nInserted"]
print(nr_inserts)
我一直收到一个NaTType错误,并且我找不到数据帧的哪一行的日期为空。不幸的是,它来自一个39k行的Excel文件。所以,仅仅看一眼是不会有帮助的。我尝试了一个例外,理论上它可以告诉我在出现错误之前有多少成功插入,因此给了我一个在哪里查找的提示,但它没有打印出来。
错误如下所示:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "dataimport.py", line 71, in importFromExcel
result = coll.insert_many(data_dict)
File "C:\Python38\lib\site-packages\pymongo\collection.py", line 758, in insert_many
blk.execute(write_concern, session=session)
File "C:\Python38\lib\site-packages\pymongo\bulk.py", line 511, in execute
return self.execute_command(generator, write_concern, session)
File "C:\Python38\lib\site-packages\pymongo\bulk.py", line 345, in execute_command
client._retry_with_session(
File "C:\Python38\lib\site-packages\pymongo\mongo_client.py", line 1384, in _retry_with_session
return func(session, sock_info, retryable)
File "C:\Python38\lib\site-packages\pymongo\bulk.py", line 339, in retryable_bulk
self._execute_command(
File "C:\Python38\lib\site-packages\pymongo\bulk.py", line 295, in _execute_command
result, to_send = bwc.execute(ops, client)
File "C:\Python38\lib\site-packages\pymongo\message.py", line 898, in execute
request_id, msg, to_send = self._batch_command(docs)
File "C:\Python38\lib\site-packages\pymongo\message.py", line 890, in _batch_command
request_id, msg, to_send = _do_bulk_write_command(
File "C:\Python38\lib\site-packages\pymongo\message.py", line 1382, in _do_bulk_write_command
return _do_batched_op_msg(
File "C:\Python38\lib\site-packages\pymongo\message.py", line 1307, in _do_batched_op_msg
return _batched_op_msg(
File "pandas\_libs\tslibs\nattype.pyx", line 64, in pandas._libs.tslibs.nattype._make_error_func.f
ValueError: NaTType does not support utcoffset
根据猜测,ValueError和BulkWriteError是不同的,所以nInserted从不打印。有没有人知道如何在失败之前获得成功插入的次数?
发布于 2020-08-05 11:28:05
我怀疑是否会执行任何插入操作,因为错误很可能是在将数据传递给mongodb进行插入之前发生的。
在任何情况下,如果您想要查找数据帧中具有日期值的行,请尝试:(替换包含NaT的列的'date'
)
null_df = df[pd.isnull(df['date'])]
print(null_df)
要删除日期为空的项目,请使用:
df = df[pd.notnull(df['date'])]
https://stackoverflow.com/questions/63258419
复制