Python pandas时间序列重采样函数扩展时间索引

2024-10-01 13:29:38 发布

您现在位置: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分钟重采样时,时间范围会扩大:

^{pr2}$

我觉得我错过了一些显而易见的东西。它为什么要这样做?在

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

编辑2:是的,这是件愚蠢的事。我以为索引会被分类。在

start_data

Out[23]: 
<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.index.min()
Out[24]: Timestamp('2012-01-07 00:00:00', tz=None)

start_data.index.max()
Out[25]: Timestamp('2013-12-10 23:59:00', tz=None)

编辑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)

Tags: columnsto数据dataindexoutstartnull