Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cyclic shift of a pandas series

Tags:

python

pandas

I am using the shift method for a data series in pandas (documentation).

Is it possible do a cyclic shift, i.e. the first value become the last value, in one step?

>>> input
Out[20]: 
5     0.995232
15    0.999794
25    1.006853
35    0.997781
45    0.981553
Name: vRatio, dtype: float64

>>> input.shift()
Out[21]: 
5          NaN
15    0.995232
25    0.999794
35    1.006853
45    0.997781
Name: vRatio, dtype: float64

desired output:

Out[21]: 
5     0.981553
15    0.995232
25    0.999794
35    1.006853
45    0.997781
Name: vRatio, dtype: float64
like image 602
jka.ne Avatar asked Jul 22 '15 11:07

jka.ne


People also ask

How do you shift a series in Python?

shift() function to shift the data of the given Series object by -2 periods. Output : Now we will use Series. shift() function to shift the data in the given series object by -2 periods.

What does .shift do in pandas?

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 the best way to shift a pandas DataFrame column?

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.

Does ILOC work on series?

iloc attribute enables purely integer-location based indexing for selection by position over the given Series object. Example #1: Use Series. iloc attribute to perform indexing over the given Series object.

How to shift index by period in pandas series?

Pandas Series.shift () function shift index by desired number of periods with an optional time freq. When freq is not passed, shift the index without realigning the data. Syntax: Series.shift (periods=1, freq=None, axis=0, fill_value=None) Parameter : periods : Number of periods to shift. Can be positive or negative.

What is pandas shift () function?

Pandas Series.shift () function shift index by desired number of periods with an optional time freq. When freq is not passed, shift the index without realigning the data.

How to shift data by 2 periods in a series?

periods : Number of periods to shift. Can be positive or negative. axis : Shift direction. Returns : Copy of input object, shifted. Example #1: Use Series.shift () function to shift the data of the given Series object by 2 periods. Now we will use Series.shift () function to shift the data in the given series object by 2 periods.

What is frequency and axis in pandas?

frequency represents the multiple parameters such as data offset, timedelta, time series modules and time rule string. axis represents 0 to represent rows and 1 to represent columns. How Pandas shift () Function works?


2 Answers

You can use np.roll to cycle the index values and pass this as the values to reindex:

In [23]:
df.reindex(index=np.roll(df.index,1))

Out[23]:
         vRatio
index          
45     0.981553
5      0.995232
15     0.999794
25     1.006853
35     0.997781

If you want to preserve your index then you can just overwrite the values again using np.roll:

In [25]:
df['vRatio'] = np.roll(df['vRatio'],1)
df

Out[25]:
         vRatio
index          
5      0.981553
15     0.995232
25     0.999794
35     1.006853
45     0.997781
like image 139
EdChum Avatar answered Sep 28 '22 05:09

EdChum


Here's a slight modification of @EdChum 's great answer, which I find more useful in situations where I want to avoid an assignment:

pandas.DataFrame(np.roll(df.values, 1), index=df.index)

or for Series:

pandas.Series(np.roll(ser.values, 1), index=ser.index)
like image 43
cxrodgers Avatar answered Sep 28 '22 07:09

cxrodgers