gpt4 book ai didi

python-3.x - 我如何找到特定文档insert_many()失败?

转载 作者:行者123 更新时间:2023-12-03 08:39:19 24 4
gpt4 key购买 nike

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永远不会打印。有谁知道如何在失败之前获得成功的插入次数?

最佳答案

我怀疑是否执行了任何插入操作,因为在将数据传递给mongodb进行插入之前很可能会发生错误。
无论如何,如果要在数据框中查找具有NaT值的行,请尝试:(用'date'替换包含日期的列)

null_df = df[pd.isnull(df['date'])]
print(null_df)
要删除日期为空的项目,请使用:
df = df[pd.notnull(df['date'])]

关于python-3.x - 我如何找到特定文档insert_many()失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63258419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com