maxymoo
maxymoo

Reputation: 36545

Is there a function to change the name of a Pandas Series

Is there an equivalent of DataFrame.rename(columns = newcolumns) for Series? I am imagining something like

def rename(self, name = None):
    self.name = name

Edit: I'm not sure why this has recieved a downvote. I think this is a reasonable thing to wonder about. I prefer to program in a functional style, avoiding statements like myseries.name = "newname" wherever possible. This style of programming is generally well-supported in the Pandas API, (e.g. inplace=False by default in most functions), and I am just wondering whether there is a way of updating Series names in a functional way which I have just missed. (Otherwise I will submit a feature request).

Upvotes: 0

Views: 1417

Answers (2)

Gomain Hoyes
Gomain Hoyes

Reputation: 76

Update

As of most recent documentation the method Series.rename, when passed a scalar, does exactly what you want; returns a new series with new name.

What follows is was true at sometime, but not relevant anymore.

Previous post

I stumbled across this problem myself and have found it very frustrating that the API does not cover the use case of changing Series.name in a method-chaining fashion.

I have found two awkward work-around to this.

  1. If the series in question is a result of reducing columns (row values) of a data-frame, i.e., calling apply on a data frame with a function that returns a scalar,

    # this returns a series where series.name == df.index.name, not what we want.
    df.apply(lambda row: series-to-scalar(row),axis=1)
    

    you can either transform the data-frame using DataFrame.apply by having func return a single value series indexed by the new name then access the column using [].

    df.apply(lambda row: pd.Series(series-to-scalar(row),index=['new name']),axis=1)['new name']
    

    or add a new column named new name to the data-frame then access the column using [].

    df.assign(**{'new name': lambda df: df.apply(lambda row: row-to-scalar(row),axis=1)})['new name']
    
  2. If you have no control on where the series came from and you need to chain methods to change its name, use Series.pipe to create a new series frow the original series with a new name.

    sr.pipe(lambda sr: pd.Series(sr,name='new name'))
    

Hope this helps. And good luck with the feature request.

Upvotes: 2

Anton Protopopov
Anton Protopopov

Reputation: 31672

IIUC you need rename function for Pandas.Series, pd.Series has two rename methods: rename and rename_axis but none of them satisfied your wishes. You could change name of the Series in common style:

e = pd.Series(np.arange(10), name='test')
In [95]: e
Out[95]:
0    0
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
9    9
Name: test, dtype: int32

e.name = 'new'
In [97]: e
Out[97]:
0    0
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
9    9
Name: new, dtype: int32

But if you need functional style you need to do request for that feature in future realeses (it seems that it's very easy to implement, just add one keyword for .rename method).

Upvotes: 1

Related Questions