My DF has a Column Time with a range of 1 hour. 19:00-20:00, 20:00-21:00, 21:00-22:00 and so on. I have another Column with a recorded Value in these Times, like 50 , 40, 10.
I want to know at what Hour of the Day is the Value the highest etc.. Is there a way I can convert the time from range to just single value like 19:00 and then extract the hour?
Df['Time'] = pd.to_datetime(Df['Time']) I tried this but got error
Out of bounds nanosecond timestamp: 1-01-01 19:00:00
First use Series.str.split
and then convert values to timedeltas by to_timedelta
:
df = pd.DataFrame({'Time':['19:00-20:00', '20:00-21:00', '21:00-22:00']})
df[['first', 'second']] = (df['Time'].str.split('-', expand=True)
.add(':00')
.apply(pd.to_timedelta))
print (df)
Time first second
0 19:00-20:00 19:00:00 20:00:00
1 20:00-21:00 20:00:00 21:00:00
2 21:00-22:00 21:00:00 22:00:00
print (df.dtypes)
Time object
first timedelta64[ns]
second timedelta64[ns]
dtype: object
Or to datetimes by to_datetime
:
df[['first', 'second']] = df['Time'].str.split('-', expand=True).apply(pd.to_datetime)
print (df)
Time first second
0 19:00-20:00 2019-08-04 19:00:00 2019-08-04 20:00:00
1 20:00-21:00 2019-08-04 20:00:00 2019-08-04 21:00:00
2 21:00-22:00 2019-08-04 21:00:00 2019-08-04 22:00:00
print (df.dtypes)
Time object
first datetime64[ns]
second datetime64[ns]
dtype: object
Here is possible easy way extract parts of datetimes, eg hours, times...:
df['hour1'] = df['first'].dt.hour
df['time1'] = df['first'].dt.time
print (df)
Time first second hour1 time1
0 19:00-20:00 2019-08-04 19:00:00 2019-08-04 20:00:00 19 19:00:00
1 20:00-21:00 2019-08-04 20:00:00 2019-08-04 21:00:00 20 20:00:00
2 21:00-22:00 2019-08-04 21:00:00 2019-08-04 22:00:00 21 21:00:00
print (df.dtypes)
Time object
first datetime64[ns]
second datetime64[ns]
hour1 int64
time1 object
dtype: object
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.