2013-11-15 82 views
0

我在熊猫身边玩弄了一些财务时间序列数据,并且试图对某些时间戳数据进行重新取样。这是起始数据:Python熊猫时间序列重新取样函数延长时间索引

start_data 

Out[12]: 
<class 'pandas.core.frame.DataFrame'> 
DatetimeIndex: 749880 entries, 2012-07-06 03:00:00 to 2013-09-11 23:59:00 
Data columns (total 1 columns): 
TickMean 749880 non-null values 
dtypes: float64(1) 

start_data.TickMean 
Out[18]: 
2012-07-06 03:00:00 1.541194 
2012-07-06 03:01:00 1.541216 
2012-07-06 03:02:00 1.541201 
2012-07-06 03:03:00 1.541088 
2012-07-06 03:04:00 1.540999 
2012-07-06 03:05:00 1.541011 
2012-07-06 03:06:00 1.541090 
2012-07-06 03:07:00 1.541256 
2012-07-06 03:08:00 1.541341 
2012-07-06 03:09:00 1.541386 
2012-07-06 03:10:00 1.541511 
2012-07-06 03:11:00 1.541469 
2012-07-06 03:12:00 1.541506 
2012-07-06 03:13:00 1.541584 
2012-07-06 03:14:00 1.541453 
... 
2013-09-11 23:45:00 1.602015 
2013-09-11 23:46:00 1.602015 
2013-09-11 23:47:00 1.602015 
2013-09-11 23:48:00 1.602015 
2013-09-11 23:49:00 1.602015 
2013-09-11 23:50:00 1.602015 
2013-09-11 23:51:00 1.602015 
2013-09-11 23:52:00 1.602015 
2013-09-11 23:53:00 1.602015 
2013-09-11 23:54:00 1.602015 
2013-09-11 23:55:00 1.602015 
2013-09-11 23:56:00 1.602015 
2013-09-11 23:57:00 1.602015 
2013-09-11 23:58:00 1.602015 
2013-09-11 23:59:00 1.602015 
Name: TickMean, Length: 749880, dtype: float64 

,当我尝试了40分钟的重采样,时间范围扩大:

start_data = start_data.resample('40min') 

start_data 
Out[14]: 
<class 'pandas.core.frame.DataFrame'> 
DatetimeIndex: 25344 entries, 2012-01-07 00:00:00 to 2013-12-10 23:20:00 
Freq: 40T 
Data columns (total 1 columns): 
TickMean 18749 non-null values 
dtypes: float64(1) 

start_data.TickMean 

Out[15]: 
2012-01-07 00:00:00 1.5706 
2012-01-07 00:40:00 1.5706 
2012-01-07 01:20:00 1.5706 
2012-01-07 02:00:00 1.5706 
2012-01-07 02:40:00 1.5706 
2012-01-07 03:20:00 1.5706 
2012-01-07 04:00:00 1.5706 
2012-01-07 04:40:00 1.5706 
2012-01-07 05:20:00 1.5706 
2012-01-07 06:00:00 1.5706 
2012-01-07 06:40:00 1.5706 
2012-01-07 07:20:00 1.5706 
2012-01-07 08:00:00 1.5706 
2012-01-07 08:40:00 1.5706 
2012-01-07 09:20:00 1.5706 
... 
2013-12-10 14:00:00 1.594563 
2013-12-10 14:40:00 1.594796 
2013-12-10 15:20:00 1.594766 
2013-12-10 16:00:00 1.593523 
2013-12-10 16:40:00 1.593171 
2013-12-10 17:20:00 1.593702 
2013-12-10 18:00:00 1.595145 
2013-12-10 18:40:00 1.595796 
2013-12-10 19:20:00 1.595527 
2013-12-10 20:00:00 1.595099 
2013-12-10 20:40:00 1.595060 
2013-12-10 21:20:00 1.595575 
2013-12-10 22:00:00 1.595575 
2013-12-10 22:40:00 1.595575 
2013-12-10 23:20:00 1.595575 
Freq: 40T, Name: TickMean, Length: 25344, dtype: float64 

我觉得我失去了一些东西明显。它为什么这样做?

快速编辑:我知道40分钟的频率很奇怪,但其他频率有相同的效果。

编辑2:是的,这是愚蠢的。我认为索引将被排序。编辑3:作为任何遇到这样奇怪问题的人的奖励,我的日期数据是第一天,而不是第一个月。所以也扔掉了一切。这是使用dayfirst = True选项解决的。

ask_data.index = pd.to_datetime(ask_data.index, dayfirst=True) 

ask_data 
Out[34]: 
<class 'pandas.core.frame.DataFrame'> 
DatetimeIndex: 749880 entries, 2012-06-07 03:00:00 to 2013-11-09 23:59:00 
Data columns (total 5 columns): 
Open  749880 non-null values 
High  749880 non-null values 
Low  749880 non-null values 
Close  749880 non-null values 
Volume 749880 non-null values 
dtypes: float64(5) 

ask_data.index.min() 
Out[35]: Timestamp('2012-06-07 03:00:00', tz=None) 

ask_data.index.max() 
Out[36]: Timestamp('2013-11-09 23:59:00', tz=None) 

回答

1

你确定你的索引是按顺序吗?你可以检查通过:

print start_data.index.min(), start_data.index.max(), start_data.index.is_monotonic 
+0

是的,就是这样。编辑添加,谢谢! – user1644030

相关问题