Time series offset without frequency?

I have a time series whose indices look like this:

In [671]: indices Out[671]: DatetimeIndex(['2000-12-29', '2001-02-20', '2001-03-26', '2001-04-12', '2001-04-24', '2001-07-05', '2001-08-15', '2001-09-10', '2001-09-18', '2001-10-02', '2001-10-11', '2001-10-30', '2001-12-13', '2002-03-07', '2002-06-13', '2002-09-12', '2002-12-12', '2003-03-13', '2003-06-12', '2013-02-19', '2013-05-28', '2013-09-03', '2014-01-21', '2014-02-18', '2014-05-27', '2014-07-07', '2014-09-02', '2015-01-20', '2015-02-17', '2015-05-26', '2015-07-06', '2016-05-31', '2016-07-05', '2016-09-06', '2016-10-04', '2017-01-17', '2017-02-21', '2017-05-30', '2017-09-05'], dtype='datetime64[ns]', name='date', freq=None) 

I cannot assign a frequency since the frequency is irregular.

My goal is to get a new set of indexes that are shifted 2 lines (not two calendar dates later, but two dates later in the data).

I'm trying to:

  indices2= indices.shift(2) 

But he says:

 ValueError: Cannot shift with no freq 

My desired result is as follows:

 In [671]: indices2 Out[671]: DatetimeIndex(['2000-02-20', '2001-03-26', '2001-04-12', ...., '2017-09-05'], 
0
source share
1 answer

This works if you first load it into the pd.Series object and then shift -

 pd.Series(i).shift(-1).head() 0 2001-02-20 1 2001-03-26 2 2001-04-12 3 2001-04-24 4 2001-07-05 Name: date, dtype: datetime64[ns] 

The actual result contains NaN, which can be removed using dropna .

 pd.DatetimeIndex(pd.Series(i).shift(-1).dropna()) DatetimeIndex(['2001-02-20', '2001-03-26', '2001-04-12', '2001-04-24', '2001-07-05', '2001-08-15', '2001-09-10', '2001-09-18', '2001-10-02', '2001-10-11', '2001-10-30', '2001-12-13', '2002-03-07', '2002-06-13', '2002-09-12', '2002-12-12', '2003-03-13', '2003-06-12', '2013-02-19', '2013-05-28', '2013-09-03', '2014-01-21', '2014-02-18', '2014-05-27', '2014-07-07', '2014-09-02', '2015-01-20', '2015-02-17', '2015-05-26', '2015-07-06', '2016-05-31', '2016-07-05', '2016-09-06', '2016-10-04', '2017-01-17', '2017-02-21', '2017-05-30', '2017-09-05'], dtype='datetime64[ns]', name='date', freq=None) 
+2
source

Source: https://habr.com/ru/post/988813/


All Articles