Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Specify time range in DatetimeIndex

I´m struggling with the different pandas index formats.

My univariate time series (data) looks like this

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

where

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

I have a function that extracts a starting point and an endpoint, say

>>>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
>>>

How can I slice the series from start to end based on the index values which seem to be of DatetimeIndex format themselves as

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

I tried this

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

which gives me

TypeError: Cannot convert input to Timestamp 

But I failed to convert the DatetimeIndex objects start and end into Timestamps...

like image 653
Pat Avatar asked Dec 05 '25 05:12

Pat


2 Answers

You needed to access the scalar value inside the single row Series and pass this to loc to slice the df:

data.loc[start.index[0] : end.index[0]]
like image 126
EdChum Avatar answered Dec 07 '25 17:12

EdChum


I think you are using references to Series in your slicing. Instead try:

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

I think your start and end are single period series.

like image 20
piRSquared Avatar answered Dec 07 '25 19:12

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!