python - 更改时间和 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
随着时间的变化,这个时间日期在法国不存在。
所以这就是它崩溃的原因。
如何将日期转换为计数?
问候
解决方案
解决方案:更新到 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
推荐阅读
- nativescript - 处理 ListView 或 RadListView (NativeScript) 内的多个点击目标
- javascript - 电话键盘按键的字母组合
- python - 将我自己的属性添加到实例中是不好的做法吗?
- reactjs - React svg xmlns:预期的 xlink 标识符
- python - 轴方向反转下的不变坐标与变坐标
- git - 获取拉取请求的源存储库
- jestjs - 是否可以开玩笑地激活覆盖但不在控制台中打印?
- javascript - 在同一个构造函数(JS)中调用方法时“不是函数”
- c# - 如何从存储过程结果实体框架创建列表
- java - 如何在春季数据批量保存期间确定违规实体