Skip to content

Commit 030e374

Browse files
huashuaiTomAugspurger
authored andcommitted
BUG: Fix series rename called with str altering name rather index (GH17407) (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
1 parent b8467c0 commit 030e374

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

Diff for: doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ Indexing
589589
- Bug in intersection of ``RangeIndex`` with negative step (:issue:`17296`)
590590
- Bug in ``IntervalIndex`` where performing a scalar lookup fails for included right endpoints of non-overlapping monotonic decreasing indexes (:issue:`16417`, :issue:`17271`)
591591
- Bug in :meth:`DataFrame.first_valid_index` and :meth:`DataFrame.last_valid_index` when no valid entry (:issue:`17400`)
592+
- 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`)
592593

593594
I/O
594595
^^^

Diff for: pandas/core/dtypes/inference.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import collections
44
import re
55
import numpy as np
6+
from collections import Iterable
67
from numbers import Number
78
from pandas.compat import (PY2, string_types, text_type,
89
string_and_binary_types)
@@ -262,7 +263,7 @@ def is_list_like(obj):
262263
False
263264
"""
264265

265-
return (hasattr(obj, '__iter__') and
266+
return (isinstance(obj, Iterable) and
266267
not isinstance(obj, string_and_binary_types))
267268

268269

Diff for: pandas/tests/dtypes/test_inference.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __getitem__(self):
5858
def test_is_list_like():
5959
passes = ([], [1], (1, ), (1, 2), {'a': 1}, set([1, 'a']), Series([1]),
6060
Series([]), Series(['a']).str)
61-
fails = (1, '2', object())
61+
fails = (1, '2', object(), str)
6262

6363
for p in passes:
6464
assert inference.is_list_like(p)

Diff for: pandas/tests/series/test_indexing.py

+10
Original file line numberDiff line numberDiff line change
@@ -2188,6 +2188,16 @@ def test_reindex_fill_value(self):
21882188
expected = Series([False, True, False], index=[1, 2, 3])
21892189
assert_series_equal(result, expected)
21902190

2191+
def test_rename(self):
2192+
2193+
# GH 17407
2194+
s = Series(range(1, 6), index=pd.Index(range(2, 7), name='IntIndex'))
2195+
result = s.rename(str)
2196+
expected = s.rename(lambda i: str(i))
2197+
assert_series_equal(result, expected)
2198+
2199+
assert result.name == expected.name
2200+
21912201
def test_select(self):
21922202
n = len(self.ts)
21932203
result = self.ts.select(lambda x: x >= self.ts.index[n // 2])

0 commit comments

Comments
 (0)