0

I wonder if you could point me in the right direction?

I have a dataframe with a dateindex and corresponding values:


date|value

2010-10-16 | 485

2010-10-17 | 486

... ...

2013-10-12 | 8588

2013-10-12 | 8589


and I want to split this dataframe into individual dataframes by 6 month date chunks, named period_1, period_2 and so on:

period_1 contains values from 2010-10-18 to (2010-10-18 + 6 months)

period_2 contains values from (2010-10-18 + 6 months) to (2010-10-18 + 6*2 months) and so on.

Is there an elegant way to do this?

I've done this manually by doing

period_1 = df.loc['2010-10-18':'2011-04-01'] and so on but it's a bit tricky...

Thank you.

Deng-guy
  • 45
  • 5

1 Answers1

0

I prefer here dictionary of DataFrames:

start_date = df['date'].min()
end_date = df['date'].max() 

months = (end_date.year - start_date.year) * 12 + (end_date.month - start_date.month)

dfs = {f'period_{i+1}': df[df['date'].between(start_date + pd.DateOffset(months=i),
                                              start_date + pd.DateOffset(months=i+1))] for i in range(months + 1)}
# print (dfs)


print (dfs['period_1'])
        date  value
0 2010-10-16    485
1 2010-10-17    486

It is not recommended, but possible create DataFrames by variables:

for i in range(months + 1):
    df1 = df[df['date'].between(start_date + pd.DateOffset(months=i), start_date + pd.DateOffset(months=i+1))] 
    globals()[f'period_{i+1}'] =  df1
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252