diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 4a3122a78b234..e0e0c18052550 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -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 ^^^ diff --git a/pandas/core/dtypes/inference.py b/pandas/core/dtypes/inference.py index ff7e215951a1f..de769c69f44fd 100644 --- a/pandas/core/dtypes/inference.py +++ b/pandas/core/dtypes/inference.py @@ -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) @@ -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)) diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index dbde7ae5081d4..857f7a283aa95 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -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) diff --git a/pandas/tests/series/test_indexing.py b/pandas/tests/series/test_indexing.py index 2182e3fbfc212..83d6a09d38f41 100644 --- a/pandas/tests/series/test_indexing.py +++ b/pandas/tests/series/test_indexing.py @@ -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])