Search code examples
pythonpandasdataframegroup-byresampling

How to convert a DatetimeIndexResamplerGroupby object to a Data Frame?


I want to resampling a data frame which has a time series data at 30 second interval to 1 second interval. For this I used:

test_data=test_data.groupby('entity_id').resample('S', fill_method='ffill')

The output is: <pandas.core.resample.DatetimeIndexResamplerGroupby object at 0x1a1f64f588> How can I convert this object to a data frame?

I have tried: test_data = pd.DataFrame(test_data) after running the last command but it returns a data frame which has the index and a list of all other elements of that row.


Solution

  • Use ffill method:

    test_data = pd.DataFrame({
        'entity_id': ['a','a','a','a','b','b','b','c','d'],
        'data':range(9)}, 
         index=pd.date_range('2018-01-01', periods=9, freq='3S'))
    print (test_data)
                        entity_id  data
    2018-01-01 00:00:00         a     0
    2018-01-01 00:00:03         a     1
    2018-01-01 00:00:06         a     2
    2018-01-01 00:00:09         a     3
    2018-01-01 00:00:12         b     4
    2018-01-01 00:00:15         b     5
    2018-01-01 00:00:18         b     6
    2018-01-01 00:00:21         c     7
    2018-01-01 00:00:24         d     8
    

    test_data=test_data.groupby('entity_id')['data'].resample('S').ffill()
    print (test_data)
    entity_id                     
    a          2018-01-01 00:00:00    0
               2018-01-01 00:00:01    0
               2018-01-01 00:00:02    0
               2018-01-01 00:00:03    1
               2018-01-01 00:00:04    1
               2018-01-01 00:00:05    1
               2018-01-01 00:00:06    2
               2018-01-01 00:00:07    2
               2018-01-01 00:00:08    2
               2018-01-01 00:00:09    3
    b          2018-01-01 00:00:12    4
               2018-01-01 00:00:13    4
               2018-01-01 00:00:14    4
               2018-01-01 00:00:15    5
               2018-01-01 00:00:16    5
               2018-01-01 00:00:17    5
               2018-01-01 00:00:18    6
    c          2018-01-01 00:00:21    7
    d          2018-01-01 00:00:24    8
    Name: data, dtype: int64