Trying to down sample of 8 weekly time points to 2 points, each represents the average over 4 weeks, I use resample(). I started by defining the rule using (60*60*24*7*4) seconds, and saw I ended up in 3 time points, latest one is dummy. Started to check it, I noticed that if I define the rule as 4W or 28D it's fine, but going down to 672H or smaller units (minutes, seconds,..) the extra faked column appears. This testing code:
import numpy as np
import pandas as pd
d = np.arange(16).reshape(2, 8)
res = []
for month in range(1,13):
start_date = str(month) + '/1/2014'
df = pd.DataFrame(data=d, index=['A', 'B'], columns=pd.date_range(start_date, periods=8, freq='7D'))
print(df, '\n')
dfw = df.resample(rule='4W', how='mean', axis=1, closed='left', label='left')
print('4 Weeks:\n', dfw, '\n')
dfd = df.resample(rule='28D', how='mean', axis=1, closed='left', label='left')
print('28 Days:\n', dfd, '\n')
dfh = df.resample(rule='672H', how='mean', axis=1, closed='left', label='left')
print('672 Hours:\n', dfh, '\n')
dfm = df.resample(rule='40320T', how='mean', axis=1, closed='left', label='left')
print('40320 Minutes:\n', dfm, '\n')
dfs = df.resample(rule='2419200S', how='mean', axis=1, closed='left', label='left')
print('2419200 Seconds:\n', dfs, '\n')
res.append(([start_date], dfh.shape[1] == dfd.shape[1]))
print('\n\n--------------------------\n\n')
[print(res[i]) for i in range(12)]
pass
is printed as (I pasted here only the printout of the last iteration):
2014-11-01 2014-11-29 2014-12-27
A 1.5 5.5 NaN
B 9.5 13.5 NaN
2014-12-01 2014-12-08 2014-12-15 2014-12-22 2014-12-29 2015-01-05 \
A 0 1 2 3 4 5
B 8 9 10 11 12 13
2015-01-12 2015-01-19
A 6 7
B 14 15
4 Weeks:
2014-11-30 2014-12-28
A 1.5 5.5
B 9.5 13.5
28 Days:
2014-12-01 2014-12-29
A 1.5 5.5
B 9.5 13.5
672 Hours:
2014-12-01 2014-12-29 2015-01-26
A 1.5 5.5 NaN
B 9.5 13.5 NaN
40320 Minutes:
2014-12-01 2014-12-29 2015-01-26
A 1.5 5.5 NaN
B 9.5 13.5 NaN
2419200 Seconds:
2014-12-01 2014-12-29 2015-01-26
A 1.5 5.5 NaN
B 9.5 13.5 NaN
--------------------------
(['1/1/2014'], False)
(['2/1/2014'], True)
(['3/1/2014'], True)
(['4/1/2014'], True)
(['5/1/2014'], False)
(['6/1/2014'], False)
(['7/1/2014'], False)
(['8/1/2014'], False)
(['9/1/2014'], False)
(['10/1/2014'], False)
(['11/1/2014'], False)
(['12/1/2014'], False)
So there is an error for date_range starting on beginning of 9 months, and no error for 3 months (February-April). Either I miss something or it's a bug, is it?
感谢@DSM和@Andy,的确我有熊猫0.15.1,升级到最新的0.15.2解决了它
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.