I am working on a DataFrame looking at baseball games Date and their Attendance so I can create a Calendar Heatmap.
Date Attendance
1 Apr 7 44723.0
2 Apr 8 42719.0
3 Apr 9 36139.0
4 Apr 10 41253.0
5 Apr 11 20480.0
I've tried different solutions that I've come across...
- df['Date'] = df['Date'].astype('datetime64[ns]')
- df['Date'] = pd.to_datetime(df['Date'])
but I'll get the error of
'Out of bounds nanosecond timestamp: 1-04-07 00:00:00'.
From looking at my data, I don't even have a date that goes with that timestamp. I also looked at other posts on this site, and 1 potential problem is that my Dates are NOT zero padded? Could that be the cause?
CodePudding user response:
you can convert to datetime if you supply a format
; Ex:
df
Out[33]:
Date Attendance
1 Apr 7 44723.0
2 Apr 8 42719.0
3 Apr 9 36139.0
4 Apr 10 41253.0
5 Apr 11 20480.0
pd.to_datetime(df["Date"], format="%b %d")
Out[35]:
1 1900-04-07
2 1900-04-08
3 1900-04-09
4 1900-04-10
5 1900-04-11
Name: Date, dtype: datetime64[ns]
If you're unhappy with the base year 1900, you can add a date offset, for example
df["datetime"] = pd.to_datetime(df["Date"], format="%b %d")
df["datetime"] = pd.tseries.offsets.DateOffset(years=100)
df["datetime"]
1 2000-04-07
2 2000-04-08
3 2000-04-09
4 2000-04-10
5 2000-04-11
Name: datetime, dtype: datetime64[ns]