Skip to content

Commit 432c672

Browse files
committed
BUG: GH3468 Fix assigning a new index to a duplicate index in a DataFrame would fail
1 parent 15bca1c commit 432c672

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

RELEASE.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pandas 0.11.1
6161
- Fix regression in a DataFrame apply with axis=1, objects were not being converted back
6262
to base dtypes correctly (GH3480_)
6363
- Fix issue when storing uint dtypes in an HDFStore. (GH3493_)
64+
- Fix assigning a new index to a duplicate index in a DataFrame would fail (GH3468_)
6465

6566
.. _GH3164: https://github.com/pydata/pandas/issues/3164
6667
.. _GH3251: https://github.com/pydata/pandas/issues/3251
@@ -75,6 +76,7 @@ pandas 0.11.1
7576
.. _GH3455: https://github.com/pydata/pandas/issues/3455
7677
.. _GH3457: https://github.com/pydata/pandas/issues/3457
7778
.. _GH3461: https://github.com/pydata/pandas/issues/3461
79+
.. _GH3468: https://github.com/pydata/pandas/issues/3468
7880
.. _GH3448: https://github.com/pydata/pandas/issues/3448
7981
.. _GH3449: https://github.com/pydata/pandas/issues/3449
8082
.. _GH3493: https://github.com/pydata/pandas/issues/3493

pandas/core/internals.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,16 @@ def _gi(self, arg):
5656
@property
5757
def ref_locs(self):
5858
if self._ref_locs is None:
59-
indexer = self.ref_items.get_indexer(self.items)
60-
indexer = com._ensure_platform_int(indexer)
61-
if (indexer == -1).any():
62-
raise AssertionError('Some block items were not in block '
63-
'ref_items')
59+
ri = self.ref_items
60+
if ri.is_unique:
61+
indexer = ri.get_indexer(self.items)
62+
indexer = com._ensure_platform_int(indexer)
63+
if (indexer == -1).any():
64+
raise AssertionError('Some block items were not in block '
65+
'ref_items')
66+
else:
67+
indexer = np.arange(len(ri))
68+
6469
self._ref_locs = indexer
6570
return self._ref_locs
6671

pandas/tests/test_frame.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9201,6 +9201,21 @@ def test_assign_columns(self):
92019201
assert_series_equal(self.frame['C'], frame['baz'])
92029202
assert_series_equal(self.frame['hi'], frame['foo2'])
92039203

9204+
def test_assign_columns_with_dups(self):
9205+
9206+
# GH 3468 related
9207+
df = DataFrame([[1,2]], columns=['a','a'])
9208+
df.columns = ['a','a.1']
9209+
9210+
expected = DataFrame([[1,2]], columns=['a','a.1'])
9211+
assert_frame_equal(df, expected)
9212+
9213+
df = DataFrame([[1,2]], columns=['a','a'])
9214+
df.columns = ['b','b']
9215+
9216+
expected = DataFrame([[1,2]], columns=['b','b'])
9217+
assert_frame_equal(df, expected)
9218+
92049219
def test_cast_internals(self):
92059220
casted = DataFrame(self.frame._data, dtype=int)
92069221
expected = DataFrame(self.frame._series, dtype=int)

0 commit comments

Comments
 (0)