Pandas在DatetimeIndex

时间:2016-05-09 21:39:31

标签: python pandas indexing timestamp

我正在努力使用不同的pandas索引格式。

我的单变量时间序列(数据)看起来像这样

2015-01-01 22:00:01.973    1.210525
2015-01-01 22:00:03.297    1.210490
2015-01-01 22:00:23.922    1.210485
2015-01-01 22:00:24.507    1.210480
2015-01-01 22:01:05.979    1.210490
2015-01-01 22:01:08.390    1.210525
2015-01-01 22:01:09.899    1.210520
2015-01-01 22:01:09.950    1.210505
2015-01-01 22:01:13.576    1.210505
2015-01-01 22:01:19.984    1.210485
2015-01-01 22:01:27.936    1.210510

,其中

>>> type(data)
<class 'pandas.core.series.Series'>
>>> type(data.index)
<class 'pandas.tseries.index.DatetimeIndex'>
>>> 

我有一个提取起点和终点的函数,比如说

>>>start
textdate
2015-01-01 22:00:03.297    1.210490
Name: mean, dtype: float64
>>>

>>>end
textdate
2015-01-01 22:01:19.984    1.210485
Name: mean, dtype: float64
>>>

如何根据看起来像DatetimeIndex格式的索引值从头到尾切片系列

>>> start.index
DatetimeIndex(['2015-01-01 22:00:03.297'], dtype='datetime64[ns]', name=u'textdate', freq=None)
>>> 

我试过这个

series = data[start.index : end.index]

给了我

TypeError: Cannot convert input to Timestamp 

但我无法将DatetimeIndex对象的开始和结束转换为时间戳......

2 个答案:

答案 0 :(得分:1)

我认为你在切片中使用Series的引用。而是尝试:

series = data[start.index[0] : end.index[0]]

我认为您的startend是单期系列。

答案 1 :(得分:1)

您需要访问单行Series内的标量值并将其传递给loc以对df进行切片:

data.loc[start.index[0] : end.index[0]]
相关问题