Search code examples
pythonpandasdatetimedataframedata-analysis

How to sum up data in specific periods of date time in Pandas?


I have a data frame with more then 1mil values. The task is, to sum it up these values in the range of every 5 minutes. In other words from 0 to first 5 minutes, then 10 minutes, then 15 and so on. But there are over 30-33 days. This is my data:

                                    Size
                        DateTime                              
2018-10-19 04:14:01.015000+00:00     2
2018-10-19 04:14:01.546000+00:00     1
2018-10-19 04:15:01.290000+00:00     1
2018-10-19 04:15:01.291000+00:00    10
2018-10-19 04:15:01.821000+00:00     1
2018-10-19 04:15:01.821000+00:00     1
2018-10-19 04:15:02.352000+00:00     1
2018-10-19 04:15:02.352000+00:00     1
2018-10-19 04:15:02.883000+00:00     1
2018-10-19 04:15:02.884000+00:00     1
2018-10-19 04:15:03.413000+00:00     1
2018-10-19 04:15:03.414000+00:00     1
2018-10-19 04:15:03.943000+00:00     1
2018-10-19 04:15:03.943000+00:00     1
2018-10-19 04:15:04.474000+00:00     1
2018-10-19 04:15:04.474000+00:00     1
2018-10-19 04:15:05.003000+00:00     1
2018-10-19 04:15:05.003000+00:00     1
2018-10-19 04:15:05.334000+00:00     1
2018-10-19 04:15:05.336000+00:00     1
...
2018-11-26 19:59:33.928000+00:00     1
2018-11-26 19:59:37.221000+00:00     1
2018-11-26 19:59:41.808000+00:00     1
2018-11-26 19:59:42.338000+00:00     1
2018-11-26 19:59:45.520000+00:00     1
2018-11-26 19:59:52.059000+00:00     1
2018-11-26 19:59:52.589000+00:00     1
2018-11-26 19:59:54.714000+00:00     1
2018-11-26 19:59:55.244000+00:00     1
2018-11-26 19:59:56.297000+00:00     1
2018-11-26 19:59:57.888000+00:00     1
2018-11-26 19:59:59.008000+00:00     1
2018-11-26 20:00:00.071000+00:00     1
2018-11-26 20:51:04.606000+00:00     1
2018-11-26 20:51:57.307000+00:00     1

As you can see, it's pretty lots of rows in there. I have some ideas about how to do it, but I'm stuck. Well, data range could be set like:

data[data.index.minute % 5 == 0]

But how could I sum values before this and in the next range ?


Solution

  • With resample:

    data.resample('5min')['Size'].sum()