Saeed
Saeed

Reputation: 2109

Shifting a Pandas time series without frequency

I have a time series whose indices looks 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 frequency is irregular.

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

I try:

   indices2 = indices.shift(2)

But it says:

ValueError: Cannot shift with no freq

My desired output looks like:

In [671]: indices2
Out[671]: 

    DatetimeIndex(['2000-02-20', '2001-03-26', '2001-04-12', ...., '2017-09-05'],

Upvotes: 7

Views: 2056

Answers (1)

cs95
cs95

Reputation: 403278

This works if you load it into a pd.Series object first, 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 NaNs, which you can remove 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)

Upvotes: 10

Related Questions