Skip to content

Commit 46c9656

Browse files
charlie0389jreback
authored andcommitted
BUG: Fixed 19497 - previously, renaming an index changed its type if … (#21029)
1 parent bd14ea3 commit 46c9656

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,7 @@ Reshaping
13641364
- Improved error message for :func:`DataFrame.merge` when there is no common merge key (:issue:`19427`)
13651365
- 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`)
13661366
- :func:`Series.rename` now accepts ``axis`` as a kwarg (:issue:`18589`)
1367+
- Bug in :func:`~DataFrame.rename` where an Index of same-length tuples was converted to a MultiIndex (:issue:`19497`)
13671368
- Comparisons between :class:`Series` and :class:`Index` would return a ``Series`` with an incorrect name, ignoring the ``Index``'s name attribute (:issue:`19582`)
13681369
- Bug in :func:`qcut` where datetime and timedelta data with ``NaT`` present raised a ``ValueError`` (:issue:`19768`)
13691370
- Bug in :func:`DataFrame.iterrows`, which would infers strings not compliant to `ISO8601 <https://en.wikipedia.org/wiki/ISO_8601>`_ to datetimes (:issue:`19671`)

pandas/core/internals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5296,7 +5296,7 @@ def _transform_index(index, func, level=None):
52965296
return MultiIndex.from_tuples(items, names=index.names)
52975297
else:
52985298
items = [func(x) for x in index]
5299-
return Index(items, name=index.name)
5299+
return Index(items, name=index.name, tupleize_cols=False)
53005300

53015301

53025302
def _putmask_smart(v, m, n):

pandas/tests/frame/test_alter_axes.py

+11
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,17 @@ def test_rename_bug(self):
579579
columns=['2001-01-01'])
580580
assert_frame_equal(df, expected)
581581

582+
def test_rename_bug2(self):
583+
# GH 19497
584+
# rename was changing Index to MultiIndex if Index contained tuples
585+
586+
df = DataFrame(data=np.arange(3), index=[(0, 0), (1, 1), (2, 2)],
587+
columns=["a"])
588+
df = df.rename({(1, 1): (5, 4)}, axis="index")
589+
expected = DataFrame(data=np.arange(3), index=[(0, 0), (5, 4), (2, 2)],
590+
columns=["a"])
591+
assert_frame_equal(df, expected)
592+
582593
def test_reorder_levels(self):
583594
index = MultiIndex(levels=[['bar'], ['one', 'two', 'three'], [0, 1]],
584595
labels=[[0, 0, 0, 0, 0, 0],

0 commit comments

Comments
 (0)