Chunk_Ning
Chunk_Ning

Reputation: 113

set the period for DatetimeIndex to calculate and plot

I have such a series named "data_grouped".

action_time
2017-02-16 00:00:00    23
2017-02-16 00:00:01    24
2017-02-16 00:00:02    22
2017-02-16 00:00:03    18
2017-02-16 00:00:04    20
2017-02-16 00:00:05    24
2017-02-16 00:00:06    16
2017-02-16 00:00:07    13
2017-02-16 00:00:08    18
2017-02-16 00:00:09    20
2017-02-16 00:00:10    16
2017-02-16 00:00:11    22
2017-02-16 00:00:12    22
2017-02-16 00:00:13     9
2017-02-16 00:00:14    13
2017-02-16 00:00:15    23
2017-02-16 00:00:16    18
2017-02-16 00:00:17    13
2017-02-16 00:00:18    18
2017-02-16 00:00:19    19
2017-02-16 00:00:20    20
2017-02-16 00:00:21    24
2017-02-16 00:00:22    19
2017-02-16 00:00:23    24
2017-02-16 00:00:24    24
2017-02-16 00:00:25    24
2017-02-16 00:00:26    15
2017-02-16 00:00:27    28
2017-02-16 00:00:28    19
2017-02-16 00:00:29    27
                       ..
2017-02-16 23:59:30    27
2017-02-16 23:59:31    21
2017-02-16 23:59:32    27
2017-02-16 23:59:33    21
2017-02-16 23:59:34    20
2017-02-16 23:59:35    21
2017-02-16 23:59:36    18
2017-02-16 23:59:37    30
2017-02-16 23:59:38    16
2017-02-16 23:59:39    24
2017-02-16 23:59:40    19
2017-02-16 23:59:41    34
2017-02-16 23:59:42    24
2017-02-16 23:59:43    23
2017-02-16 23:59:44    29
2017-02-16 23:59:45    23
2017-02-16 23:59:46    28
2017-02-16 23:59:47    16
2017-02-16 23:59:48    32
2017-02-16 23:59:49    22
2017-02-16 23:59:50    26
2017-02-16 23:59:51    25
2017-02-16 23:59:52    27
2017-02-16 23:59:53    36
2017-02-16 23:59:54    26
2017-02-16 23:59:55    22
2017-02-16 23:59:56    22
2017-02-16 23:59:57    27
2017-02-16 23:59:58    19
2017-02-16 23:59:59    20
Name: action_type, Length: 85913, dtype: int64

The index is DatetimeIndex. The DatetimeIndex is from "2017-02-16 00:00:00" to "2017-02-16 23:59:59". I want to do a aggregate the sum for every hour. And plot it with the horizontal-axis is the time.

Upvotes: 0

Views: 37

Answers (1)

jezrael
jezrael

Reputation: 862751

I think you need Series.resample + aggregating function sum + Series.plot:

s.resample('H').sum().plot()

Or groupby + Grouper:

s.groupby(pd.Grouper(freq='H')).sum().plot()

Upvotes: 1

Related Questions