Reputation: 3
I want my function to resample the pandas Series that is passed into the various frequencies - samples, I feel like I am almost there except it seems to be keeping the old index instead of creating a resampled index and produces a lot of NaN values:
index=pd.date_range('2015-10-1 00:00:00', '2018-12-31 23:50:00', freq='30min')
df=pd.DataFrame(np.random.randn(len(index),2).cumsum(axis=0),columns=['A','B'],index=index)
def resample(ts):
samples = ['60m','4h','D','1h','W']
counter = 0
resampled = {}
while counter < len(samples):
for i in samples:
ts = ts.resample(i).mean()
resampled[i]=ts
counter+=1
return resampled
data = resample(df.A)
data['W']
2015-11-01 21.396793
2015-11-08 NaN
2015-11-15 NaN
2015-11-22 NaN
So basically I want 5 new arrays of re sampled time series.
Thanks.
Upvotes: 0
Views: 465
Reputation: 19124
index=pd.date_range('2015-10-1 00:00:00', '2018-12-31 23:50:00', freq='30min')
df=pd.DataFrame(np.random.randn(len(index),2).cumsum(axis=0),columns=['A','B'],index=index)
The rest of your code is largely extraneous:
data = {freq: df['A'].resample(freq).mean() for freq in ['60m','4h','D','1h','W']}
data
now has 5 elements in it where each is a resampled DataFrame.
Upvotes: 1
Reputation: 863531
I think you need upsample your data, so need change mean
to ffill
or bfill
functions, also need 60T
for 60 minutes
:
def resample(ts):
samples = ['60T','4h','D','1h','W']
resampled = {}
for i in samples:
ts = ts.resample(i).ffill()
resampled[i]=ts
return resampled
Upvotes: 0