I have a dataframe df
user timestamp amount
us67 15:59:07 87
us90 17:12:19 10
us12 03:23:16 17
print(df.timestamp[0])
>>> datetime.time(15,59,7)
I want to bin all the times into 1 hour intervals, so 24 intervals in total. However, I get a TypeError
df['timestamp'] = pd.cut(x=df['timestamp'], bins=24)
>>> TypeError: unsupported operand type(s) for +: 'datetime.time' and 'float'
The method does work, however, if the date is included in the timestamp
column, but I want to ignore the date and only keep the times (for plotting later on):
user timestamp amount
us67 2018-04-29 15:59:07.455 87
us90 2018-04-29 17:12:19.128 10
us12 2018-04-29 03:23:16.890 17
print(df.timestamo[0])
>>> Timestamp('2018-04-29 15:59:07.455000')
df['timestamp'] = pd.cut(x=df['timestamp'], bins=24)
With the format above for timestamp
, the binning works. However I don't want the year and date in the timestamp or in the intervals. I want to focus only on the time of day.
Is there a way I can bin timestamp
using only time of day? Ultimately the goal here is to plot a timeseries of df
(timestamp
vs. amount
) using the hour of day only and not the date - so if there is a better way to do that, please suggest it.
I would create a column with my binned hours using dt.hour
So
df["binned_hours"] = pd.cut(df.timestamp.dt.hour, bins=24)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With