Pandas set_axis: Replacing a Series index without mutating it

Pandas set_axis: Replacing a Series index without mutating it

You can set a Python Pandas series index with set_axis. This returns a new series with the new index applied:

s = pd.Series([10, 20, 30], index=list('abc'))
(
  s
  .set_axis(list('qrs'))
  .loc[['q', 's']]
) # q=10, s=30 

s.index # still Index([‘a’, ‘b’, ‘c’], dtype=’str’)