Python Forum
Python Resample: How do I keep NaN as NaN?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Resample: How do I keep NaN as NaN?
#4
I think that answers your question. To use resample().sum() your only choices is to ignore NaN's. Looks like you'll have to do most of the work yourself.

resample() is really just a special version of groupby(). The primary difference that resample only groups by date/time. Calling sequence.resample() returns a DatetimeIndexResampler object which you can use to access the groups. Each group has a timestamp index and a series of values. The series can be summed, and when summing a series you can set skipna=False.
import pandas as pd
from numpy import nan
series = pd.Series(range(6), index=pd.date_range('1/1/2013', periods=6, freq='T'))
series[5] = nan

groups = series.resample('2T')
for x in groups:
    print(x[1].sum(skipna=False))
Output:
1.0 5.0 nan
Using this info it is easy to write a resampler that doesn't ignore NaN's.
import pandas as pd
import numpy as np

def no_skip_resampler(series, period):
    groups = series.resample(period)
    sums = [x[1].sum(skipna=False) for x in groups]
    return pd.Series(sums, groups.indices)

series = pd.Series(range(6), index=pd.date_range('1/1/2013', periods=6, freq='T'))
series[5] = np.nan

print("Resampled")
print(series.resample('2T').sum())
print("\n Reconstructed")
print(no_skip_resampler(series, '2T'))
Output:
Resampled 2013-01-01 00:00:00 1.0 2013-01-01 00:02:00 5.0 2013-01-01 00:04:00 4.0 Freq: 2T, dtype: float64 Reconstructed 2013-01-01 00:00:00 1.0 2013-01-01 00:02:00 5.0 2013-01-01 00:04:00 NaN dtype: float64
JaneTan likes this post
Reply


Messages In This Thread
RE: Python Resample: How do I keep NaN as NaN? - by deanhystad - Dec-08-2022, 10:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python resample by day and get weekstart data JaneTan 0 1,602 Dec-15-2022, 03:38 AM
Last Post: JaneTan
  Resample from monthly to weekly works, but not vice versa JaneTan 0 1,200 Dec-14-2022, 12:58 AM
Last Post: JaneTan
  resample grouping pr0blem olufemig 1 2,759 Nov-06-2019, 10:45 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020