pandas:asfreq包含更多数据时的奇怪行为

时间:2014-06-25 12:51:36

标签: python pandas

我在使用asfreq时遇到问题。如下所示,当我从ts系列中再包含一个值来执行asfreq时,结果ts2将所有值设置为等于第一个。我的期望是,除了可能包括一个新的价值之外,根本没有变化。对我来说,这似乎是一个错误,或者我错过了什么?

In [571]: ts
Out[571]: 
2014-02-24 13:26:26.941000    1.142483
2014-02-24 14:47:11.480000   -0.900430
2014-02-24 15:09:36.490000   -0.998898
2014-02-24 15:48:31.534000    1.242197
2014-02-24 15:49:32.529000   -0.301316
2014-02-24 15:58:41.321000    0.342022
2014-02-24 16:05:42.752000   -0.876856
2014-02-24 16:10:21.501000   -1.050685
2014-02-24 16:16:47.204000    1.391424
2014-02-24 16:18:48.296000    1.048143
2014-02-24 16:19:52.346000   -0.823974
2014-02-24 16:22:13.665000   -0.689560
2014-02-24 16:24:13.760000   -0.323252
2014-02-24 16:26:15.155000   -1.095331
2014-02-24 16:29:58.235000   -0.185681
...
Length: 4455

In [572]: ts2 = ts[0:2128].asfreq('10Min',method='pad')

In [573]: ts2
Out[573]: 
2014-02-24 13:26:26.941000    1.142483
2014-02-24 13:36:26.941000    1.142483
2014-02-24 13:46:26.941000    1.142483
2014-02-24 13:56:26.941000    1.142483
2014-02-24 14:06:26.941000    1.142483
2014-02-24 14:16:26.941000    1.142483
2014-02-24 14:26:26.941000    1.142483
2014-02-24 14:36:26.941000    1.142483
2014-02-24 14:46:26.941000    1.142483
2014-02-24 14:56:26.941000   -0.900430
2014-02-24 15:06:26.941000   -0.900430
2014-02-24 15:16:26.941000   -0.998898
2014-02-24 15:26:26.941000   -0.998898
2014-02-24 15:36:26.941000   -0.998898
2014-02-24 15:46:26.941000   -0.998898
...
Freq: 10T, Length: 7076

In [574]: ts2 = ts[0:2129].asfreq('10Min',method='pad')

In [575]: ts2
Out[575]: 
2014-02-24 13:26:26.941000    1.142483
2014-02-24 13:36:26.941000    1.142483
2014-02-24 13:46:26.941000    1.142483
2014-02-24 13:56:26.941000    1.142483
2014-02-24 14:06:26.941000    1.142483
2014-02-24 14:16:26.941000    1.142483
2014-02-24 14:26:26.941000    1.142483
2014-02-24 14:36:26.941000    1.142483
2014-02-24 14:46:26.941000    1.142483
2014-02-24 14:56:26.941000    1.142483
2014-02-24 15:06:26.941000    1.142483
2014-02-24 15:16:26.941000    1.142483
2014-02-24 15:26:26.941000    1.142483
2014-02-24 15:36:26.941000    1.142483
2014-02-24 15:46:26.941000    1.142483
...
2014-04-14 14:16:26.941000    1.142483
2014-04-14 14:26:26.941000    1.142483
2014-04-14 14:36:26.941000    1.142483
Freq: 10T, Length: 7076

In [576]: ts2.max()
Out[576]: 1.1424827552686787

In [577]: ts2.min()
Out[577]: 1.1424827552686787

1 个答案:

答案 0 :(得分:0)

 data=[('2014-02-24 16:16:47.204000',    1.391424)
 ,('2014-02-24 16:18:48.296000',    1.048143)
 ,('2014-02-24 16:19:52.346000',  -0.823974)
 ,('2014-02-24 16:22:13.665000',   -0.689560)
 ,('2014-02-24 16:24:13.760000',   -0.323252)
 ,('2014-02-24 16:26:15.155000',   -1.095331)
 ,('2014-02-24 16:29:58.235000',   -0.185681)]

 df=pd.DataFrame(data,columns=['Date','Value'])
 df['Date']=pd.to_datetime(df['Date'])
 minutes=df.resample('1Min',on='Date').mean().dropna()

 print(minutes)   

输出:

      Value
  Date                         
  2014-02-24 16:16:00  1.391424
  2014-02-24 16:18:00  1.048143
  2014-02-24 16:19:00 -0.823974
  2014-02-24 16:22:00 -0.689560
  2014-02-24 16:24:00 -0.323252
  2014-02-24 16:26:00 -1.095331
  2014-02-24 16:29:00 -0.185681