Home > OS >  Resampled series with timedeltas does not start at zero
Resampled series with timedeltas does not start at zero

Time:11-02

I would like to resample a pandas series with timedelta starting with the zero interval (timedelta(seconds=0)) instead of the first occurrence.

For example, for

from datetime import timedelta
from random import sample
s = pd.Series(1, index=[timedelta(minutes=m) for m in sample(range(1, 100), 10)])
s.resample('1h').count()

I get

0 days 00:12:00    9
0 days 01:12:00    1

But I need

0 days 00:00:00    9
0 days 01:00:00    0    
0 days 02:00:00    1

CodePudding user response:

You should use to_offset.

from pandas.tseries.frequencies import to_offset
s_res = s.resample('1h').count()

We compute the current minute index start

minute_start = (s.index.seconds600//60).min()

Then modify the index

s_res.index = s_res.index - to_offset(f'{minute_start)}min')

CodePudding user response:

You need to have an item 0 days 00:00:00 in your timedelta series, which serves as the bottom boundary. If you look at the examples in the pandas documentation for resample() method you will realize that the range in all their examples starts from 0 (at least for the type of groupby results you are looking for).

import pandas as pd
from datetime import timedelta
from random import sample

my_sample = sample(range(1, 100), 10)
my_sample.append(0)

s = pd.Series(1, index=[timedelta(minutes=m) for m in my_sample])
print(s.min)
s.resample('1h').count()
  • Related