pythonpandasnumpy

equivalent of numpy.roll() for pandas Series


I have a pandas Series:

In[1]: b1.data
Out[1]: 
z        40.0
R         0.0
DET       6.9
FAEac    16.0
Name: SC3, dtype: object

and I'm trying to move the 'z' row to the bottom of the Series. If that was a numpy.array I'd simply do:

numpy.roll(b1.data, -1)

but working with pandas Series the one-liner I came up with is:

In[2]: pandas.concat([b1.data,pandas.Series(b1.data.pop('z'),index=['z'])])
Out[2]: 
R         0.0
DET       6.9
FAEac    16.0
z        40.0
dtype: object
    

Isn't there a less cumbersome way to achieve the same?


Solution

  • Code

    example

    import pandas as pd
    import numpy as np
    data = {'z': 40.0, 'R': 0.0, 'DET': 6.9, 'FAEac': 16.0}
    s = pd.Series(data)
    

    code

    Put the result of rolling the index in square brackets

    out = s[np.roll(s.index, -1)]
    

    result

    z        40.0           R         0.0
    R         0.0           DET       6.9
    DET       6.9      ->   FAEac    16.0
    FAEac    16.0           z        40.0
       s                         out