0

I have tried a lot to found a method that allow me to obtain all the groups of a resampling or of a group by without any aggregation for example starting from:

import pandas as pd
import datetime
import numpy as np

fir_date=pd.to_datetime('1991/12/25')
days = pd.date_range(fir_date, fir_date + datetime.timedelta(100), freq='30T')
data=[]
for i in range(3):
    np.random.seed(seed=i)
    data.append(np.random.randint(1, high=100, size=len(days)))
    
df = pd.DataFrame({'Datetime': days, 'feature1': data[0],'feature2': data[1],'feature3': data[2]})
df = df.set_index('Datetime')
df.index = df.index.map(lambda x: x.replace(second=0,microsecond=0))

I am able to obtain every group of a certain resample with:

df.resample('D').groups

> {Timestamp('1991-12-25 00:00:00', freq='D'): 48, 
> Timestamp('1991-12-26 00:00:00', freq='D'): 96,
> Timestamp('1991-12-27 00:00:00', freq='D'): 144,  
> Timestamp('1991-12-28 00:00:00', freq='D'): 192,  
> Timestamp('1991-12-29 00:00:00', freq='D'): 240,  ...}

The output is a dict an so i can access to a specific element with:

df.resample('D').get_group('1991-12-25 00:00:00')

But this seems not so smart.

There is a better way to, for example, get 1 DataFrame for every group of the resample??

I know is possible to cycle on the resample as:

for i in df.resample('D'):
    print(i)
    break

But this not allow me to comprare not consecutive group, or at least not easily.

Any good tips to handle this problem?

mat
  • 181
  • 14

2 Answers2

1

Use selection in combination with unique

import pandas as pd
import datetime
import numpy as np

fir_date=pd.to_datetime('1991/12/25')
days = pd.date_range(fir_date, fir_date + datetime.timedelta(100), freq='30T')
data=[]
for i in range(3):
    np.random.seed(seed=i)
    data.append(np.random.randint(1, high=100, size=len(days)))
    
df = pd.DataFrame({'Datetime': days, 'feature1': data[0],'feature2': data[1],'feature3': data[2]})
df['Days'] = df['Datetime'].dt.date # put in your resample step length here, e.g. days, month, ...
df = df.set_index('Datetime')
df.index = df.index.map(lambda x: x.replace(second=0,microsecond=0))
for i in df['Days'].unique():
    selection = df[df['Days'] = i]
RaJa
  • 1,471
  • 13
  • 17
1

If need list of DataFrames use list comprehension:

dfs = [x for i, x in df.resample('D')]

If need dictionary is possible convert resample object to tuples and dict:

d = dict(tuple(df.resample('D')))
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252