3

Here is my code

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()

The problem is that i have an hpur that don't exist :

 2019-03-31 01:50:24.455000

With the changing hour this timedate does not exist in france.

So that's why it crash.

How to convert the date taking that in count ?

Regards

Bussiere
  • 500
  • 13
  • 60
  • 119
  • Possible duplicate of https://stackoverflow.com/questions/12203676/daylight-savings-time-in-python – LoneWanderer Apr 02 '19 at 19:51
  • Are the dates and times in the "Created" column initially in UTC? What error message do you get when running your code? – Sheldon Apr 07 '19 at 13:24

1 Answers1

4

Solution: update to pandas 0.24.2, then use the nonexistent argument to tz_localize, as in here:

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')

See here for more nonexistent shifting options: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.tz_localize.html

Yishai E
  • 481
  • 3
  • 9