[英]Remove NaT values while inserting data into mongo using pymongo
Have a df with values有一个带有值的df
name date
tom 2019-12-10 07:44:03.733
mark NaT
if i try this:如果我试试这个:
data = df.to_dict("records")
mydb.test.insert_many(data)
I'm getting this error:我收到此错误:
ValueError: NaTType does not support utcoffset
How to send this data to mongodb without NaT and keeping the column empty without converting to string.如何在没有 NaT 的情况下将此数据发送到 mongodb 并保持列为空而不转换为字符串。
expected data in mongodb: mongodb 中的预期数据:
{
_id:ObjectId(5db012b123a2a1cabcc12345),
name:tom,
date:2019-10-23T10:55:50.569+00:00
}
{
_id:ObjectId(5db012b123a2a1cabcc12346),
name:mark
}
You can convert NaT
to None
, like below:您可以将NaT
为None
,如下所示:
If your df['date']
is not a datetime
column, convert it into one.如果您的df['date']
不是datetime
时间列,请将其转换为一个。
df['date'] = pd.to_datetime(df['date'])
df['date'] = df['date'].astype(object).where(df['date'].notnull(), None)
Then you can insert this in mongo
.然后你可以在mongo
中插入它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.