3

i need to resample a df, different columns with different functions.

import pandas as pd
import numpy as np

df=pd.DataFrame(index=pd.DatetimeIndex(start='2020-01-01 00:00:00', end='2020-01-02 00:00:00', freq='3H'), 
data=np.random.rand(9,3), 
columns=['A','B','C'])

df = df.resample('1H').agg({'A': 'ffill',
                            'B': 'interpolate',
                            'C': 'max'})

Functions like 'mean', 'max', 'sum' work.

But it seems that 'interpolate' can't be used like that.

Any workarounds?

DDR
  • 459
  • 5
  • 15
  • Does this answer your question? [Pandas interpolate within a groupby](https://stackoverflow.com/questions/37057187/pandas-interpolate-within-a-groupby) – E. Zeytinci Dec 12 '19 at 11:03
  • 1
    No, question is about resampling different columns with different functions. – DDR Dec 12 '19 at 12:14

1 Answers1

1

One work-around would be to concat the different aggregated frames:

df_out = pd.concat([df[['A']].resample('1H').ffill(),
                    df[['B']].resample('1H').interpolate(),
                    df[['C']].resample('1H').max().ffill()],
                   axis=1)

[out]

                            A         B         C
2020-01-01 00:00:00  0.836547  0.436186  0.520913
2020-01-01 01:00:00  0.836547  0.315646  0.520913
2020-01-01 02:00:00  0.836547  0.195106  0.520913
2020-01-01 03:00:00  0.577291  0.074566  0.754697
2020-01-01 04:00:00  0.577291  0.346092  0.754697
2020-01-01 05:00:00  0.577291  0.617617  0.754697
2020-01-01 06:00:00  0.490666  0.889143  0.685191
2020-01-01 07:00:00  0.490666  0.677584  0.685191
2020-01-01 08:00:00  0.490666  0.466025  0.685191
2020-01-01 09:00:00  0.603678  0.254466  0.605424
2020-01-01 10:00:00  0.603678  0.358240  0.605424
2020-01-01 11:00:00  0.603678  0.462014  0.605424
2020-01-01 12:00:00  0.179458  0.565788  0.596706
2020-01-01 13:00:00  0.179458  0.477367  0.596706
2020-01-01 14:00:00  0.179458  0.388946  0.596706
2020-01-01 15:00:00  0.702992  0.300526  0.476644
2020-01-01 16:00:00  0.702992  0.516952  0.476644
2020-01-01 17:00:00  0.702992  0.733378  0.476644
2020-01-01 18:00:00  0.884276  0.949804  0.793237
2020-01-01 19:00:00  0.884276  0.907233  0.793237
2020-01-01 20:00:00  0.884276  0.864661  0.793237
2020-01-01 21:00:00  0.283859  0.822090  0.186542
2020-01-01 22:00:00  0.283859  0.834956  0.186542
2020-01-01 23:00:00  0.283859  0.847822  0.186542
2020-01-02 00:00:00  0.410897  0.860688  0.894249
Chris Adams
  • 18,389
  • 4
  • 22
  • 39