【问题标题】:PyMongo: NatType ValueError on Bulk Insert to New CollectionPyMongo:批量插入新集合时的 ​​NatType ValueError
【发布时间】:2019-02-25 00:46:32
【问题描述】:

我正在尝试使用 PyMongo 将一组混合的日期和文本数据上传到我的远程 MongoDB 服务器中的新集合。

但是,由于空值与日期混合,我得到了一个错误 - 即存在None 值而不是datetime.datetime() 对象的行。

作为一些背景知识:原始数据存储在 CSV 文件中,我正在使用pandas.read_csv() 将其读入pandas.DataFrame()。在pandas 中获得数据后,我会在将数据转换为字典列表之前进行一些基本的清理,然后使用标准的collection.insert_many() 方法将其上传到集合中。

最初,每行/文档/字典中的值都存储为字符串。但是,在上传数据之前,我通过对每个值调用 datetime.datetime.strptime() 将许多日期列转换为 datetime 对象。但是,并非每个字典都填充了这些日期字段。对于这些字典,我只是使用 None 而不是 datetime 对象。

然后,我尝试上传的结果数据是一个混合了许多 NoneType 值的字典列表,当我调用 insert_many() 时,我得到了这个: p>

ValueError: NaTType does not support utcoffset.

我不熟悉utcoffset,我对此进行研究的尝试让我感到困惑。

有没有人遇到过这个问题,或者有关于如何在 PyMongo 中处理丢失的日期时间数据的建议?

这是我的代码:

import pandas as pd
import pymongo

source = '/path/to/data'
sampleData = pd.read_csv(source, dtype=str)

Date_Columns = [
    'date_a',
    'date_b',
    'date_c',
    'date_d'
]
cleanData = sampleData
for col in Date_Columns:

    # Convert the strings to datetime objects for each column.
    # If a value is null, then use a None object instead of a datetime.
    Strings = sampleData[col].values
    Formats = [dt.datetime.strptime(d, '%m/%d/%Y') if isinstance(d, str) else None for d in Strings]
    cleanData[col] = Formats

client = pymongo.MongoClient('XX.XX.XX.XX', 99999)
db = client['my_db']
c = db['my_collection']

# Convert the cleaned DataFrame into a list of dictionaries.
Keys = [key for key in sampleData.columns.values]
Data = [dict(zip(Keys, L)) for L in sampleData.values]

c.insert_many(Data)

以及完整的追溯:

Traceback (most recent call last):
  File "/Users/haru/my_git/projects/pipeline/stable/sofla_permits_sunnyisles.py", line 738, in <module>
    setup_db()
  File "/Users/haru/my_git/projects/pipeline/stable/sofla_permits_sunnyisles.py", line 679, in setup_db
    c.insert_many(Data)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/collection.py", line 753, in insert_many
    blk.execute(write_concern, session=session)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/bulk.py", line 513, in execute
    return self.execute_command(generator, write_concern, session)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/bulk.py", line 338, in execute_command
    self.is_retryable, retryable_bulk, s, self)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1196, in _retry_with_session
    return func(session, sock_info, retryable)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/bulk.py", line 333, in retryable_bulk
    retryable, full_result)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/bulk.py", line 285, in _execute_command
    self.collection.codec_options, bwc)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/message.py", line 1273, in _do_bulk_write_command
    namespace, operation, command, docs, check_keys, opts, ctx)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/message.py", line 1263, in _do_batched_write_command
    namespace, operation, command, docs, check_keys, opts, ctx)
  File "pandas/_libs/tslibs/nattype.pyx", line 59, in pandas._libs.tslibs.nattype._make_error_func.f
ValueError: NaTType does not support utcoffset

【问题讨论】:

标签: python mongodb pymongo


【解决方案1】:

大多数机器的时钟都设置为utc,这是最理想的。它是从给定日期算起的秒的整数值(我相信在 70 年代的某个时候)。这意味着您的流程计划不依赖于当地时间,包括令人头疼的夏令时。

与美国东部标准的 UTC 偏移量为 4-5 小时(取决于夏令时)。

看看你的错误,这是一个pandas 错误,而pandas.datetime datetime.datetime 配合得很好。将其转换为所需精度的日期时间string。那应该避免这个错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    相关资源
    最近更新 更多