Skip to content

Commit 3bf11cc

Browse files
committed
BUG: Allow series with same in with crosstab (#13279)
1 parent 5146b59 commit 3bf11cc

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

pandas/tests/tools/test_pivot.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,16 @@ def test_crosstab_with_numpy_size(self):
13551355
columns=expected_column)
13561356
tm.assert_frame_equal(result, expected)
13571357

1358+
def test_crosstab_dup_index_names(self):
1359+
# GH 13279
1360+
s = pd.Series(range(3), name='foo')
1361+
result = pd.crosstab(s, s)
1362+
expected_index = pd.Index(range(3), name='foo')
1363+
expected = pd.DataFrame(np.eye(3, dtype=np.int64),
1364+
index=expected_index,
1365+
columns=expected_index)
1366+
tm.assert_frame_equal(result, expected)
1367+
13581368

13591369
class TestPivotAnnual(tm.TestCase):
13601370
"""

pandas/tools/pivot.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,14 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean',
136136

137137
table = agged
138138
if table.index.nlevels > 1:
139-
to_unstack = [agged.index.names[i] or i
140-
for i in range(len(index), len(keys))]
139+
index_names = agged.index.names[:len(index)]
140+
to_unstack = []
141+
for i in range(len(index), len(keys)):
142+
name = agged.index.names[i]
143+
if name is None or name in index_names:
144+
to_unstack.append(i)
145+
else:
146+
to_unstack.append(name)
141147
table = agged.unstack(to_unstack)
142148

143149
if not dropna:

0 commit comments

Comments
 (0)