Python 熊猫数据帧-to_json()to_csv()don';对于iso格式的日期,不应采取相同的措施

Python 熊猫数据帧-to_json()to_csv()don';对于iso格式的日期,不应采取相同的措施,python,pandas,dataframe,Python,Pandas,Dataframe,我用iso格式从API中获取日期 当我在做: df = DataFrame(results) df.to_csv(path_or_buf=file_name, index=False, encoding='utf-8', compression='gzip', quoting=QUOTE_NONNUMERIC) 我看一下我看到的CSV,例如: lastDeliveryDate 2018-11-21 16:25:53.990000-05:00 但是, 当

我用iso格式从API中获取日期

当我在做:

df = DataFrame(results)
df.to_csv(path_or_buf=file_name, index=False, encoding='utf-8',
          compression='gzip',
          quoting=QUOTE_NONNUMERIC)
我看一下我看到的CSV,例如:

lastDeliveryDate
2018-11-21 16:25:53.990000-05:00
但是,

当我这样做时:

df = DataFrame(results)
df.to_json(path_or_buf=file_name, orient="records",compression='gzip', lines=True)
我明白了(其他记录):

这是一个问题

当我将数据从CSV加载到Google BigQuery时,一切都很好。正确解析了日期

但是当我将加载更改为Json时。它无法正确解析日期

我看到了这种格式的日期:

50866-01-09 23:46:40 UTC
这是因为
to_json()
to_csv()
iso\u格式的日期产生不同的结果

我怎样才能解决这个问题?我必须编辑数据框并将所有日期列转换为常规UTC吗?我该怎么做?为什么需要
到_json()
而不需要
到_csv()

正如在上所解释的,我们尝试做:

df["lastDeliveryDate"] = dateutil.parser.parse(df["lastDeliveryDate"])  
但它给出了:

TypeError:解析器必须是字符串或字符流,而不是序列


基本上,
dateutil.parser.parse()需要一个字符串作为参数,但是您传递了整个列。尝试使用lambda函数:

df["lastDeliveryDate"] = df["lastDeliveryDate"].apply( lambda row: dateutil.parser.parse(row))

基本上,
dateutil.parser.parse()需要一个字符串作为参数,但是您传递了整个列。尝试使用lambda函数:

df["lastDeliveryDate"] = df["lastDeliveryDate"].apply( lambda row: dateutil.parser.parse(row))
从:

日期格式:{None,'epoch','iso'}
日期转换的类型。”历元'=历元毫秒,'iso'=ISO8601。默认值取决于方向。对于
orient='table'
,默认值为“iso”。对于所有其他方向,默认值为“历元”

因此,使用
orient=“records”
,您必须设置
date\u format=“iso”
,以获得稍后可以理解的日期时间格式:

df.to_json(path_or_buf=file_name, orient="records", date_format="iso", 
           compression='gzip', lines=True)
从:

日期格式:{None,'epoch','iso'}
日期转换的类型。”历元'=历元毫秒,'iso'=ISO8601。默认值取决于方向。对于
orient='table'
,默认值为“iso”。对于所有其他方向,默认值为“历元”

因此,使用
orient=“records”
,您必须设置
date\u format=“iso”
,以获得稍后可以理解的日期时间格式:

df.to_json(path_or_buf=file_name, orient="records", date_format="iso", 
           compression='gzip', lines=True)