Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename_axis(None) now (0.24) does nothing. Previously it removed the axis name #25034

Closed
tdpetrou opened this issue Jan 30, 2019 · 6 comments · Fixed by #25069
Closed

rename_axis(None) now (0.24) does nothing. Previously it removed the axis name #25034

tdpetrou opened this issue Jan 30, 2019 · 6 comments · Fixed by #25069
Labels
Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@tdpetrou
Copy link
Contributor

Code Sample, a copy-pastable example if possible

In 0.23 rename_axis was able to remove the axis name like this:

>>> s = pd.Series([1, 2, 3])
>>> s = s.rename_axis('asdf')
>>> s
asdf
0    1
1    2
2    3
dtype: int64

>>> s.rename_axis(None)
0    1
1    2
2    3
dtype: int64

In 0.24, the mapper parameter is defaulted to None so this does not work. The only way to remove an axis label is to use del s.index.name or s.index.name = None

Problem description

Seems like a regression. I have some code that relies on axis name removal with s.rename_axis(None). Seems like there is no clean way to remove an axis name now.

Expected Output

s.rename_axis(None) should work as it did in 0.23.

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Jan 30, 2019 via email

@tdpetrou
Copy link
Contributor Author

@TomAugspurger That renames the Series name attribute. My example involves renaming the axis name. This would go for both the Series and the DataFrame.

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Jan 30, 2019 via email

@jschendel
Copy link
Member

The only way to remove an axis label is to use del s.index.name or s.index.name = None

You can also pass lambda x: None for the index argument of rename_axis.

Still should fix the regression of course, but at least this provides method chaining compatible workaround in the meantime:

In [2]: s = pd.Series([1, 2, 3], index=pd.Index(list('abc'), name='foo'))

In [3]: s
Out[3]:
foo
a    1
b    2
c    3
dtype: int64

In [4]: s.rename_axis(index=lambda x: None)
Out[4]:
a    1
b    2
c    3
dtype: int64

@jschendel jschendel added the Regression Functionality that used to work in a prior pandas version label Jan 30, 2019
@jschendel jschendel added this to the Contributions Welcome milestone Jan 30, 2019
@tdpetrou
Copy link
Contributor Author

@jschendel Thank you for pointing out an alternative. Seems like one way to correct this is to have all the axis level names set equal to None when calling s.rename_axis() or df.rename_axis(). I'm not even sure what semantics would work here.

@jschendel
Copy link
Member

jschendel commented Feb 1, 2019

Seems like one way to correct this is to have all the axis level names set equal to None when calling s.rename_axis() or df.rename_axis()

I think it could probably be implemented this way, though would require some care in handling all the params (mapper, index, columns, axis) without unnecesarily removing an axis name, e.g. not removing the column name when mapper=None and axis=0.

The PR I opened basically follows @TomAugspurger's suggestion of using a sentinel value, like object(), and using is comparisons, so even if for some crazy reason someone wanted to rename their axis to object() it'd work, as it'd be a different instance and have a different id.

Basically:

In [1]: sentinel = object()

In [2]: def foo(bar=sentinel):
   ...:     if bar is sentinel:
   ...:         return 'sentinel'
   ...:     return bar

In [3]: foo()
Out[3]: 'sentinel'

In [4]: foo(None) is None
Out[4]: True

In [5]: foo(object())
Out[5]: <object at 0x7ff8cde2d8e0>

This implementation has the advantage that df.rename_axis() does nothing, so users have to be explicit about what they want to change, and minimizes the potential for surprising results from a user perspective.

@jschendel jschendel modified the milestones: Contributions Welcome, 0.24.1 Feb 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants