0

Code

df = pd.DataFrame(
    data = {'A': [1, 1, 2], 'B': [None, None, None]},
    index = pd.DatetimeIndex([
        '1990-01-01 00:00:00',
        '1990-01-01 12:00:00',
        '1990-01-02 12:00:00'
    ])
)
print(df.resample('1d').aggregate('mean'))

Output

              A
1990-01-01  1.0
1990-01-02  2.0

Desired output

              A     B
1990-01-01  1.0  None 
1990-01-02  2.0  None 

I don't care whether there's None, np.nan or pd.NA in column B of the output, the problem is that B is dropped.

actual_panda
  • 1,178
  • 9
  • 27
  • Does this answer your question? [Resampling pandas dataframe is deleting column](https://stackoverflow.com/questions/34257069/resampling-pandas-dataframe-is-deleting-column) – Nicholas Hansen-Feruch Jul 13 '22 at 14:31

2 Answers2

1

resample will drop the non numeric columns when using a numeric aggregation. You can reindex after aggregation:

df.resample('1d').aggregate('mean').reindex(df.columns, axis=1)

output:

              A   B
1990-01-01  1.0 NaN
1990-01-02  2.0 NaN
mozway
  • 194,879
  • 13
  • 39
  • 75
1
df.resample('d').agg({'A':'mean','B':'mean'})

          A   B
1990-01-01  1.0 NaN
1990-01-02  2.0 NaN
G.G
  • 639
  • 1
  • 5