From 3e3e925717f1081b272ea0d642b4f54acae7aba3 Mon Sep 17 00:00:00 2001 From: Huashuai Qu Date: Sat, 23 Sep 2017 17:53:20 -0700 Subject: [PATCH 1/8] BUG: Fix series rename called with str altering the name. GH17407 --- pandas/core/dtypes/inference.py | 3 ++- pandas/tests/series/test_indexing.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pandas/core/dtypes/inference.py b/pandas/core/dtypes/inference.py index ff7e215951a1f..01ae573531bf0 100644 --- a/pandas/core/dtypes/inference.py +++ b/pandas/core/dtypes/inference.py @@ -263,7 +263,8 @@ def is_list_like(obj): """ return (hasattr(obj, '__iter__') and - not isinstance(obj, string_and_binary_types)) + not isinstance(obj, string_and_binary_types) and + isinstance(obj, collections.Iterable)) def is_nested_list_like(obj): diff --git a/pandas/tests/series/test_indexing.py b/pandas/tests/series/test_indexing.py index 2182e3fbfc212..2b7ea191439bb 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]) From 99300ddcc636c859a02b3010ee379d1adbd1678e Mon Sep 17 00:00:00 2001 From: Huashuai Qu Date: Sat, 23 Sep 2017 18:06:31 -0700 Subject: [PATCH 2/8] add whatsnew for the fix for #17407 --- doc/source/whatsnew/v0.21.0.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 4a3122a78b234..0859d392910cd 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 ``Series.rename`` when called with `str` will alter name of series rather than index of series. (:issue:`17407`) I/O ^^^ From b9ee0880092b58f88de8701084d7f9bb87f8b187 Mon Sep 17 00:00:00 2001 From: Huashuai Qu Date: Sat, 23 Sep 2017 18:12:09 -0700 Subject: [PATCH 3/8] Fix typo in whatsnew --- doc/source/whatsnew/v0.21.0.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 0859d392910cd..3334519164d07 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -588,8 +588,9 @@ Indexing - Bug in ``CategoricalIndex`` reindexing in which specified indices containing duplicates were not being respected (:issue:`17323`) - 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`) +<<<<<<< 99300ddcc636c859a02b3010ee379d1adbd1678e - Bug in :meth:`DataFrame.first_valid_index` and :meth:`DataFrame.last_valid_index` when no valid entry (:issue:`17400`) -- Bug in ``Series.rename`` when called with `str` will alter name of series rather than index of series. (:issue:`17407`) +- Bug in ``Series.rename`` when called with `str` alters name of series rather than index of series. (:issue:`17407`) I/O ^^^ From 4996e764fb0adbd5c77574eb3cb60f19c0fafba3 Mon Sep 17 00:00:00 2001 From: Huashuai Qu Date: Sat, 23 Sep 2017 18:19:00 -0700 Subject: [PATCH 4/8] remove whitespace --- pandas/tests/series/test_indexing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/series/test_indexing.py b/pandas/tests/series/test_indexing.py index 2b7ea191439bb..83d6a09d38f41 100644 --- a/pandas/tests/series/test_indexing.py +++ b/pandas/tests/series/test_indexing.py @@ -2189,7 +2189,7 @@ def test_reindex_fill_value(self): 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) From 41436c2309e855d10aa4a7c1e1b895fe7fa1a793 Mon Sep 17 00:00:00 2001 From: Huashuai Qu Date: Sun, 24 Sep 2017 16:30:19 -0700 Subject: [PATCH 5/8] Update code after @jreback's comments --- doc/source/whatsnew/v0.21.0.txt | 3 ++- pandas/core/dtypes/inference.py | 7 +++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 3334519164d07..b9fdf64d1549f 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -588,9 +588,10 @@ Indexing - Bug in ``CategoricalIndex`` reindexing in which specified indices containing duplicates were not being respected (:issue:`17323`) - 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`) +<<<<<<< 4996e764fb0adbd5c77574eb3cb60f19c0fafba3 <<<<<<< 99300ddcc636c859a02b3010ee379d1adbd1678e - Bug in :meth:`DataFrame.first_valid_index` and :meth:`DataFrame.last_valid_index` when no valid entry (:issue:`17400`) -- Bug in ``Series.rename`` when called with `str` alters name of series rather than index of series. (:issue:`17407`) +- Bug in ``Series.rename`` when called with a `callable` alters name of series rather than index of series. (:issue:`17407`) I/O ^^^ diff --git a/pandas/core/dtypes/inference.py b/pandas/core/dtypes/inference.py index 01ae573531bf0..1e0c4fe180207 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,10 +263,8 @@ def is_list_like(obj): False """ - return (hasattr(obj, '__iter__') and - not isinstance(obj, string_and_binary_types) and - isinstance(obj, collections.Iterable)) - + return ((hasattr(obj, '__iter__') or isinstance(obj, Iterable)) and + not isinstance(obj, string_and_binary_types)) def is_nested_list_like(obj): """ From f97c9bc2d44f7aa58314fe67f1fa074af3032473 Mon Sep 17 00:00:00 2001 From: Huashuai Qu Date: Mon, 25 Sep 2017 13:16:56 -0700 Subject: [PATCH 6/8] Change `or` to `and` for checking iterable --- pandas/core/dtypes/inference.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/dtypes/inference.py b/pandas/core/dtypes/inference.py index 1e0c4fe180207..9051c6749f043 100644 --- a/pandas/core/dtypes/inference.py +++ b/pandas/core/dtypes/inference.py @@ -263,9 +263,10 @@ def is_list_like(obj): False """ - return ((hasattr(obj, '__iter__') or isinstance(obj, Iterable)) and + return (hasattr(obj, '__iter__') and isinstance(obj, Iterable) and not isinstance(obj, string_and_binary_types)) + def is_nested_list_like(obj): """ Check if the object is list-like, and that all of its elements From 45d30d4906fc6780e88bba60c1b593e2c6d303b7 Mon Sep 17 00:00:00 2001 From: Huashuai Qu Date: Tue, 26 Sep 2017 10:29:30 -0700 Subject: [PATCH 7/8] Only check against Iterable in is_list_like and add test for `str` --- doc/source/whatsnew/v0.21.0.txt | 3 ++- pandas/core/dtypes/inference.py | 2 +- pandas/tests/dtypes/test_inference.py | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index b9fdf64d1549f..14a90a85045b9 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -588,10 +588,11 @@ Indexing - Bug in ``CategoricalIndex`` reindexing in which specified indices containing duplicates were not being respected (:issue:`17323`) - 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`) +<<<<<<< f97c9bc2d44f7aa58314fe67f1fa074af3032473 <<<<<<< 4996e764fb0adbd5c77574eb3cb60f19c0fafba3 <<<<<<< 99300ddcc636c859a02b3010ee379d1adbd1678e - Bug in :meth:`DataFrame.first_valid_index` and :meth:`DataFrame.last_valid_index` when no valid entry (:issue:`17400`) -- Bug in ``Series.rename`` when called with a `callable` alters name of series rather than index of series. (:issue:`17407`) +- 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 9051c6749f043..de769c69f44fd 100644 --- a/pandas/core/dtypes/inference.py +++ b/pandas/core/dtypes/inference.py @@ -263,7 +263,7 @@ def is_list_like(obj): False """ - return (hasattr(obj, '__iter__') and isinstance(obj, Iterable) 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) From a1f9565913866da08274b46426da0afb83395afb Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Sat, 30 Sep 2017 14:33:09 -0500 Subject: [PATCH 8/8] Update v0.21.0.txt --- doc/source/whatsnew/v0.21.0.txt | 3 --- 1 file changed, 3 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 14a90a85045b9..e0e0c18052550 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -588,9 +588,6 @@ Indexing - Bug in ``CategoricalIndex`` reindexing in which specified indices containing duplicates were not being respected (:issue:`17323`) - 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`) -<<<<<<< f97c9bc2d44f7aa58314fe67f1fa074af3032473 -<<<<<<< 4996e764fb0adbd5c77574eb3cb60f19c0fafba3 -<<<<<<< 99300ddcc636c859a02b3010ee379d1adbd1678e - 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`)