Skip to content

Series#rename(str) change the name of Series. #17407

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

Closed
Eastsun opened this issue Sep 1, 2017 · 5 comments · Fixed by #17654
Closed

Series#rename(str) change the name of Series. #17407

Eastsun opened this issue Sep 1, 2017 · 5 comments · Fixed by #17654
Labels
Milestone

Comments

@Eastsun
Copy link

Eastsun commented Sep 1, 2017

Code Sample, a copy-pastable example if possible

import pandas as pd

ser = pd.Series(range(1, 6), index=pd.Index(range(2, 7), name='IntIndex'))

ser2 = ser.rename(str)

ser2.index
Out[5]: RangeIndex(start=2, stop=7, step=1, name='IntIndex')

ser2.name
Out[6]: str

Problem description

I deep into the code and find that pandas.core.series#is_list_like return True for str:

In [22]: from pandas.core import series

In [23]: series.is_list_like(str)
Out[23]: True

The implementation of is_list_like:

def is_list_like(obj):
    """
    Check if the object is list-like.
    """

    return (hasattr(obj, '__iter__') and
            not isinstance(obj, string_and_binary_types))

may should change to:

def is_list_like(obj):
    """
    Check if the object is list-like.
    """

    return (hasattr(obj , '__iter__') and
            not isinstance(obj, string_and_binary_types) and not isinstance(obj, type))

Expected Output

ser2.index
Out[20]: Index(['2', '3', '4', '5', '6'], dtype='object', name='IntIndex')

ser2.name 

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.2.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.20.3
pytest: 3.2.1
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.26
numpy: 1.13.1
scipy: 0.19.1
xarray: None
IPython: 6.1.0
sphinx: 1.6.3
patsy: 0.4.1
dateutil: 2.6.1

@jreback
Copy link
Contributor

jreback commented Sep 1, 2017

pls read the doc-string. you are renaming to a callable which is valid but weird. you generally need to use a string.

In [10]: ser = pd.Series(range(1, 6), index=pd.Index(range(2, 7), name='IntIndex'))

In [11]: ser
Out[11]: 
IntIndex
2    1
3    2
4    3
5    4
6    5
dtype: int64

In [12]: ser.rename('foo')
Out[12]: 
IntIndex
2    1
3    2
4    3
5    4
6    5
Name: foo, dtype: int64

@jreback jreback closed this as completed Sep 1, 2017
@jreback jreback added this to the No action milestone Sep 1, 2017
@Eastsun
Copy link
Author

Eastsun commented Sep 1, 2017

@jreback
However, I want to rename the index rather than the name of series.
I just use ser.rename(str) instead of ser.rename(lambda i: str(i)) for short.
My expected output is Series with Index:
Index(['2', '3', '4', '5', '6'], dtype='object', name='IntIndex')

@jreback
Copy link
Contributor

jreback commented Sep 1, 2017

ser.index.name = 'foo'

this is clearly documented and does not interact with the index.

@TomAugspurger TomAugspurger reopened this Sep 1, 2017
@TomAugspurger
Copy link
Contributor

I think this is a bug. str, is a callable, so it should be altering the labels, not the name. FYI @Eastsun we're trying to clean up this API before 0.21, but for now this should work.

@TomAugspurger
Copy link
Contributor

@Eastsun changing the check in is_list_like to check isinstance(obj, Iterable)..., works in this case. Iterable from the collections module. Would you mind seeing making that change breaks anything else, and submitting a PR with a fix + a test case?

huashuai added a commit to huashuai/pandas that referenced this issue Sep 24, 2017
@jreback jreback modified the milestones: No action, 0.21.0 Sep 24, 2017
huashuai added a commit to huashuai/pandas that referenced this issue Sep 29, 2017
TomAugspurger pushed a commit that referenced this issue Sep 30, 2017
…17407) (#17654)

* BUG: Fix series rename called with str altering the name. GH17407

* add whatsnew for the fix for #17407

* Fix typo in whatsnew

* remove whitespace

* Update code after @jreback's comments

* Change `or` to `and` for checking iterable

* Only check against Iterable in is_list_like and add test for `str`

* Update v0.21.0.txt
alanbato pushed a commit to alanbato/pandas that referenced this issue Nov 10, 2017
…17407) (pandas-dev#17654)

* BUG: Fix series rename called with str altering the name. GH17407

* add whatsnew for the fix for pandas-dev#17407

* Fix typo in whatsnew

* remove whitespace

* Update code after @jreback's comments

* Change `or` to `and` for checking iterable

* Only check against Iterable in is_list_like and add test for `str`

* Update v0.21.0.txt
No-Stream pushed a commit to No-Stream/pandas that referenced this issue Nov 28, 2017
…17407) (pandas-dev#17654)

* BUG: Fix series rename called with str altering the name. GH17407

* add whatsnew for the fix for pandas-dev#17407

* Fix typo in whatsnew

* remove whitespace

* Update code after @jreback's comments

* Change `or` to `and` for checking iterable

* Only check against Iterable in is_list_like and add test for `str`

* Update v0.21.0.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants