Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shifting a time series without frequency?

Tags:

python

pandas

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 can not 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'],
like image 668
Saeed Avatar asked Nov 27 '17 00:11

Saeed


People also ask

How do you shift time-series data?

A common operation on time-series data is to shift or "lag" the values back and forward in time, such as to calculate percentage change from sample to sample. The pandas method for this is . shift() , which will shift the values in the index by a specified number of units of the index's period.

How do you shift a time-series in python?

shift() function Shift index by desired number of periods with an optional time freq. This function takes a scalar parameter called the period, which represents the number of shifts to be made over the desired axis. This function is very helpful when dealing with time-series data.

What is Panda shift?

(1) shift() Pandas Shift() Function, shifts index by the desired number of periods. This function takes a scalar parameter called a period, which represents the number of shifts for the desired axis. This function is beneficial when dealing with time-series data. We can use fill_value to fill beyond boundary values.

How do I move row values in pandas?

shift() If you want to shift your column or subtract the column value with the previous row value from the DataFrame, you can do it by using the shift() function. It consists of a scalar parameter called period, which is responsible for showing the number of shifts to be made over the desired axis.


1 Answers

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)
like image 81
cs95 Avatar answered Sep 28 '22 14:09

cs95