From e3af7cd730464108f26317439775e9c16113c5bd Mon Sep 17 00:00:00 2001 From: Lyle Collins Date: Mon, 14 May 2018 16:35:06 +1000 Subject: [PATCH 1/2] BUG: Fixed 19497 - previously, renaming an index changed its type if index values were tuples. --- pandas/core/internals.py | 2 +- pandas/tests/frame/test_alter_axes.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pandas/core/internals.py b/pandas/core/internals.py index e7b2576ca1eae..fe508dc1bb0bc 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -5296,7 +5296,7 @@ def _transform_index(index, func, level=None): return MultiIndex.from_tuples(items, names=index.names) else: items = [func(x) for x in index] - return Index(items, name=index.name) + return Index(items, name=index.name, tupleize_cols=False) def _putmask_smart(v, m, n): diff --git a/pandas/tests/frame/test_alter_axes.py b/pandas/tests/frame/test_alter_axes.py index 95b952892c93d..164d6746edec0 100644 --- a/pandas/tests/frame/test_alter_axes.py +++ b/pandas/tests/frame/test_alter_axes.py @@ -579,6 +579,17 @@ def test_rename_bug(self): columns=['2001-01-01']) assert_frame_equal(df, expected) + def test_rename_bug2(self): + # GH 19497 + # rename was changing Index to MultiIndex if Index contained tuples + + df = DataFrame(data=np.arange(3), index=[(0, 0), (1, 1), (2, 2)], + columns=["a"]) + df = df.rename({(1, 1): (5, 4)}, axis="index") + expected = DataFrame(data=np.arange(3), index=[(0, 0), (5, 4), (2, 2)], + columns=["a"]) + assert_frame_equal(df, expected) + def test_reorder_levels(self): index = MultiIndex(levels=[['bar'], ['one', 'two', 'three'], [0, 1]], labels=[[0, 0, 0, 0, 0, 0], From 40a53fbad6547c0970a5590647f92a3e26e03b13 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Mon, 14 May 2018 14:02:03 -0500 Subject: [PATCH 2/2] Added whatsnew --- doc/source/whatsnew/v0.23.0.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index 1660c8d9fcdc5..f441e6976bd89 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -1364,6 +1364,7 @@ Reshaping - Improved error message for :func:`DataFrame.merge` when there is no common merge key (:issue:`19427`) - Bug in :func:`DataFrame.join` which does an ``outer`` instead of a ``left`` join when being called with multiple DataFrames and some have non-unique indices (:issue:`19624`) - :func:`Series.rename` now accepts ``axis`` as a kwarg (:issue:`18589`) +- Bug in :func:`~DataFrame.rename` where an Index of same-length tuples was converted to a MultiIndex (:issue:`19497`) - Comparisons between :class:`Series` and :class:`Index` would return a ``Series`` with an incorrect name, ignoring the ``Index``'s name attribute (:issue:`19582`) - Bug in :func:`qcut` where datetime and timedelta data with ``NaT`` present raised a ``ValueError`` (:issue:`19768`) - Bug in :func:`DataFrame.iterrows`, which would infers strings not compliant to `ISO8601 `_ to datetimes (:issue:`19671`)