首页 > 解决方案 > 更改时间和 NonExistentTimeError:2019-03-31 01:50:24.455000

问题描述

这是我的代码

data = result["Document"]
df = pd.DataFrame(data)


df["Created"] = pd.to_datetime(df["Created"])

df["Created"] = pd.to_datetime(df["Created"],errors='coerce').dt.tz_localize('Europe/London').dt.tz_convert('Europe/Paris')
#print(df)hour
df['Created'] = df['Created'].dt.date

df["Barcode"] = df["Barcode"].astype(str)
fig, ax = plt.subplots()

myFmt = mdates.DateFormatter('%Y-%m-%d %H:%M')
ax.xaxis.set_major_formatter(myFmt)

df1 = df.groupby(["Created"])["Tag"].count().reset_index()
df2 = df[df["Tag"] == "DISPLAY"].groupby(["Created"])["Tag"].count().reset_index()
plt.plot(df2['Created'],df2['Tag'])
plt.plot(df1['Created'],df1['Tag'])
plt.gcf().autofmt_xdate()
plt.figure(figsize=(30,20))
plt.show()

问题是我有一个不存在的 hpur :

 2019-03-31 01:50:24.455000

随着时间的变化,这个时间日期在法国不存在。

所以这就是它崩溃的原因。

如何将日期转换为计数?

问候

标签: pythontimezone

解决方案


解决方案:更新到 pandas 0.24.2,然后对 tz_localize 使用不存在的参数,如下所示:

df = pd.DataFrame()
df['Created'] = ["2019-03-31 01:50:24.455000"]
df["Created"] = pd.to_datetime(df["Created"],errors='coerce').dt.tz_localize('Europe/London', nonexistent='shift_forward').dt.tz_convert('Europe/Paris')

有关更多不存在的转换选项,请参见此处: https ://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.tz_localize.html


推荐阅读