Home > Net >  DataFrame Pandas datetime error: hour must be in 0..23
DataFrame Pandas datetime error: hour must be in 0..23

Time:10-22

I have the following time series and I want to convert to datetime in DataFrame using "pd.to_datetime". I am getting the following error: "hour must be in 0..23: 2017/ 01/01 24:00:00". How can I go around this error?

DateTime
0   2017/ 01/01  01:00:00
1   2017/ 01/01  02:00:00
2   2017/ 01/01  03:00:00
3   2017/ 01/01  04:00:00
...
22  2017/ 01/01  23:00:00
23  2017/ 01/01  24:00:00

CodePudding user response:

Given:

              DateTime
0  2017/01/01 01:00:00
1  2017/01/01 02:00:00
2  2017/01/01 03:00:00
3  2017/01/01 04:00:00
4  2017/01/01 23:00:00
5  2017/01/01 24:00:00

As the error says, 24:00:00 isn't a valid time. Depending on what it actually means, we can salvage it like this:

# Split up your Date and Time Values into separate Columns:
df[['Date', 'Time']] = df.DateTime.str.split(expand=True)

# Convert them separately, one as datetime, the other as timedelta.
df.Date = pd.to_datetime(df.Date)
df.Time = pd.to_timedelta(df.Time)

# Fix your DateTime Column, Drop the helper Columns:
df.DateTime = df.Date   df.Time
df = df.drop(['Date', 'Time'], axis=1)

print(df)
print(df.dtypes)

Output:

             DateTime
0 2017-01-01 01:00:00
1 2017-01-01 02:00:00
2 2017-01-01 03:00:00
3 2017-01-01 04:00:00
4 2017-01-01 23:00:00
5 2017-01-02 00:00:00

DateTime    datetime64[ns]
dtype: object

CodePudding user response:

df['DateTime'] =pd.to_datetime(df['DateTime'], format='%y-%m-%d %H:%M', errors='coerce')

Try this out!

  • Related