Skip to content

Commit

Permalink
BUG: Fix series rename called with str altering name rather index (GH…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
huashuai authored and No-Stream committed Nov 28, 2017
1 parent 2e4bd7b commit 803b480
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ Indexing
- Bug in intersection of ``RangeIndex`` with negative step (:issue:`17296`)
- Bug in ``IntervalIndex`` where performing a scalar lookup fails for included right endpoints of non-overlapping monotonic decreasing indexes (:issue:`16417`, :issue:`17271`)
- Bug in :meth:`DataFrame.first_valid_index` and :meth:`DataFrame.last_valid_index` when no valid entry (:issue:`17400`)
- Bug in :func:`Series.rename` when called with a `callable`, incorrectly alters the name of the `Series`, rather than the name of the `Index`. (:issue:`17407`)

I/O
^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/dtypes/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import collections
import re
import numpy as np
from collections import Iterable
from numbers import Number
from pandas.compat import (PY2, string_types, text_type,
string_and_binary_types)
Expand Down Expand Up @@ -262,7 +263,7 @@ def is_list_like(obj):
False
"""

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


Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __getitem__(self):
def test_is_list_like():
passes = ([], [1], (1, ), (1, 2), {'a': 1}, set([1, 'a']), Series([1]),
Series([]), Series(['a']).str)
fails = (1, '2', object())
fails = (1, '2', object(), str)

for p in passes:
assert inference.is_list_like(p)
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/series/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2188,6 +2188,16 @@ def test_reindex_fill_value(self):
expected = Series([False, True, False], index=[1, 2, 3])
assert_series_equal(result, expected)

def test_rename(self):

# GH 17407
s = Series(range(1, 6), index=pd.Index(range(2, 7), name='IntIndex'))
result = s.rename(str)
expected = s.rename(lambda i: str(i))
assert_series_equal(result, expected)

assert result.name == expected.name

def test_select(self):
n = len(self.ts)
result = self.ts.select(lambda x: x >= self.ts.index[n // 2])
Expand Down

0 comments on commit 803b480

Please sign in to comment.