【问题标题】:Changing hour and NonExistentTimeError: 2019-03-31 01:50:24.455000更改时间和 NonExistentTimeError:2019-03-31 01:50:24.455000
【发布时间】:2019-08-21 18:31:19
【问题描述】:

这是我的代码

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

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

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

如何将日期转换为计数?

问候

【问题讨论】:

标签: python timezone


【解决方案1】:

解决方案:更新到 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

【讨论】:

    猜你喜欢
    • 2020-01-25
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    相关资源
    最近更新 更多