Skip to content

Commit bf3c4d1

Browse files
committed
BUG: Allow series with same in with crosstab (pandas-dev#13279)
1 parent c6e5bf6 commit bf3c4d1

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

pandas/core/reshape/pivot.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,14 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean',
148148

149149
table = agged
150150
if table.index.nlevels > 1:
151-
to_unstack = [agged.index.names[i] or i
152-
for i in range(len(index), len(keys))]
151+
index_names = agged.index.names[:len(index)]
152+
to_unstack = []
153+
for i in range(len(index), len(keys)):
154+
name = agged.index.names[i]
155+
if name is None or name in index_names:
156+
to_unstack.append(i)
157+
else:
158+
to_unstack.append(name)
153159
table = agged.unstack(to_unstack)
154160

155161
if not dropna:

pandas/tests/reshape/test_pivot.py

+10
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,16 @@ def test_crosstab_with_numpy_size(self):
15131513
columns=expected_column)
15141514
tm.assert_frame_equal(result, expected)
15151515

1516+
def test_crosstab_dup_index_names(self):
1517+
# GH 13279
1518+
s = pd.Series(range(3), name='foo')
1519+
result = pd.crosstab(s, s)
1520+
expected_index = pd.Index(range(3), name='foo')
1521+
expected = pd.DataFrame(np.eye(3, dtype=np.int64),
1522+
index=expected_index,
1523+
columns=expected_index)
1524+
tm.assert_frame_equal(result, expected)
1525+
15161526

15171527
class TestPivotAnnual(object):
15181528
"""

0 commit comments

Comments
 (0)