pandas
omsampling
Sök…
Downsampling och upsampling
import pandas as pd
import numpy as np
np.random.seed(0)
rng = pd.date_range('2015-02-24', periods=10, freq='T')
df = pd.DataFrame({'Val' : np.random.randn(len(rng))}, index=rng)
print (df)
Val
2015-02-24 00:00:00 1.764052
2015-02-24 00:01:00 0.400157
2015-02-24 00:02:00 0.978738
2015-02-24 00:03:00 2.240893
2015-02-24 00:04:00 1.867558
2015-02-24 00:05:00 -0.977278
2015-02-24 00:06:00 0.950088
2015-02-24 00:07:00 -0.151357
2015-02-24 00:08:00 -0.103219
2015-02-24 00:09:00 0.410599
#downsampling with aggregating sum
print (df.resample('5Min').sum())
Val
2015-02-24 00:00:00 7.251399
2015-02-24 00:05:00 0.128833
#5Min is same as 5T
print (df.resample('5T').sum())
Val
2015-02-24 00:00:00 7.251399
2015-02-24 00:05:00 0.128833
#upsampling and fill NaN values method forward filling
print (df.resample('30S').ffill())
Val
2015-02-24 00:00:00 1.764052
2015-02-24 00:00:30 1.764052
2015-02-24 00:01:00 0.400157
2015-02-24 00:01:30 0.400157
2015-02-24 00:02:00 0.978738
2015-02-24 00:02:30 0.978738
2015-02-24 00:03:00 2.240893
2015-02-24 00:03:30 2.240893
2015-02-24 00:04:00 1.867558
2015-02-24 00:04:30 1.867558
2015-02-24 00:05:00 -0.977278
2015-02-24 00:05:30 -0.977278
2015-02-24 00:06:00 0.950088
2015-02-24 00:06:30 0.950088
2015-02-24 00:07:00 -0.151357
2015-02-24 00:07:30 -0.151357
2015-02-24 00:08:00 -0.103219
2015-02-24 00:08:30 -0.103219
2015-02-24 00:09:00 0.410599
Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow