1

Having the following DF:

    time_of_day
0   19:52:48
1   11:05:30
2   14:08:15
4   19:20:02
5   19:30:26
13  01:32:35
14  10:37:55
21  08:35:08
22  14:10:12
23  20:14:10

Is it possible to use pandas to draw a histogram of 24 hours?

Shlomi Schwartz
  • 8,693
  • 29
  • 109
  • 186

1 Answers1

2

I would use pd.cut(right=False) to produce a consistent number of bins. Is this roughly what you want?

Histogram Data

df["time_of_day"] = pd.to_datetime(df["time_of_day"])
sr = df.groupby(pd.cut(df["time_of_day"].dt.hour, range(24+1), right=False)).size()

print(sr)
time_of_day
[0, 1)      0
[1, 2)      1
[2, 3)      0
[3, 4)      0
[4, 5)      0
[5, 6)      0
[6, 7)      0
[7, 8)      0
[8, 9)      1
[9, 10)     0
[10, 11)    1
[11, 12)    1
[12, 13)    0
[13, 14)    0
[14, 15)    2
[15, 16)    0
[16, 17)    0
[17, 18)    0
[18, 19)    0
[19, 20)    3
[20, 21)    1
[21, 22)    0
[22, 23)    0
[23, 24)    0
dtype: int64

Plot

Quick and dirty

sr.to_frame().plot.bar(legend=False)
plt.show()

Imgur

Bill Huang
  • 4,491
  • 2
  • 13
  • 31