Skip to content

Commit

Permalink
BUG: Allow series with same in with crosstab (pandas-dev#13279)
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke committed Apr 17, 2017
1 parent 5146b59 commit 3bf11cc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 10 additions & 0 deletions pandas/tests/tools/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,16 @@ def test_crosstab_with_numpy_size(self):
columns=expected_column)
tm.assert_frame_equal(result, expected)

def test_crosstab_dup_index_names(self):
# GH 13279
s = pd.Series(range(3), name='foo')
result = pd.crosstab(s, s)
expected_index = pd.Index(range(3), name='foo')
expected = pd.DataFrame(np.eye(3, dtype=np.int64),
index=expected_index,
columns=expected_index)
tm.assert_frame_equal(result, expected)


class TestPivotAnnual(tm.TestCase):
"""
Expand Down
10 changes: 8 additions & 2 deletions pandas/tools/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,14 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean',

table = agged
if table.index.nlevels > 1:
to_unstack = [agged.index.names[i] or i
for i in range(len(index), len(keys))]
index_names = agged.index.names[:len(index)]
to_unstack = []
for i in range(len(index), len(keys)):
name = agged.index.names[i]
if name is None or name in index_names:
to_unstack.append(i)
else:
to_unstack.append(name)
table = agged.unstack(to_unstack)

if not dropna:
Expand Down

0 comments on commit 3bf11cc

Please sign in to comment.