Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resample data inside multiindex dataframe

I have the following dataframe:enter image description here

I need to resample the data to calculate the weekly pct_change(). How can i get the weekly change ?

Something like data['pct_week'] = data['Adj Close'].resample('W').ffill().pct_change() but the data need to groupby data.groupby(['month', 'week'])

This way every month would yield 4 values for weekly change.Which i can graph then

What i did was df['pct_week'] = data['Adj Close'].groupby(['week', 'day']).pct_change() but i got this error TypeError: 'type' object does not support item assignment

like image 784
Slartibartfast Avatar asked Nov 04 '19 15:11

Slartibartfast


People also ask

How do I resample data in pandas?

Resample Pandas time-series dataThe resample() function is used to resample time-series data. Convenience method for frequency conversion and resampling of time series. Object must have a datetime-like index (DatetimeIndex, PeriodIndex, or TimedeltaIndex), or pass datetime-like values to the on or level keyword.

What is the difference between resample and Asfreq?

resample is more general than asfreq . For example, using resample I can pass an arbitrary function to perform binning over a Series or DataFrame object in bins of arbitrary size. asfreq is a concise way of changing the frequency of a DatetimeIndex object. It also provides padding functionality.

How do you resample data in Python?

Resample Hourly Data to Daily Dataresample() method. To aggregate or temporal resample the data for a time period, you can take all of the values for each day and summarize them. In this case, you want total daily rainfall, so you will use the resample() method together with . sum() .

What is resampling time series data?

The resampling recipe transforms time series data occurring in irregular time intervals into equispaced data. The recipe is also useful for transforming equispaced data from one frequency level to another (for example, minutes to hours).


2 Answers

If want grouping with resample first is necessary DatetimeIndex only, so added DataFrame.reset_index by all levels without first, then grouping and resample with custom function, because pct_change for resample is not implemented:

def percent_change(x):
    return pd.Series(x).pct_change()

Another idea is use numpy solution for pct_change:

def percent_change(x):
    return x / np.concatenate(([np.nan], x[:-1])) - 1

df1 = (df.reset_index(level=[1,2,3])
        .groupby(['month', 'week'])['Adj Close']
        .resample('W')
        .apply(percent_change))

this way every month would yield 4 values for weekly change

So it seems there is no groupby, only necessary downsample like sum and chain Series.pct_change:

df2 = (df.reset_index(level=[1,2,3])
        .resample('W')['Adj Close']
        .sum()
        .pct_change())
like image 113
jezrael Avatar answered Sep 28 '22 01:09

jezrael


Drop unwanted indexes. The datetime index is enough for re-sampling / grouping

df.index = df.index.droplevel(['month', 'week', 'day'])

Re-sample by week, select the column needed, add a aggregation function and then calculate percentage change.

df.resample('W')['Adj Close'].mean().pct_change()
like image 27
Vishnudev Avatar answered Sep 28 '22 01:09

Vishnudev