Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Allow Series with same name with crosstab (#13279) #16028

Merged
merged 8 commits into from
Jul 30, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ Reshaping
- Bug when using :func:`isin` on a large object series and large comparison array (:issue:`16012`)
- Fixes regression from 0.20, :func:`Series.aggregate` and :func:`DataFrame.aggregate` allow dictionaries as return values again (:issue:`16741`)
- Fixes dtype of result with integer dtype input, from :func:`pivot_table` when called with ``margins=True`` (:issue:`17013`)
- Bug in ``pd.crosstab()`` where passing two ``Series`` with the same name raised a ``KeyError`` (:issue:`13279`)

Numeric
^^^^^^^
Expand Down
12 changes: 10 additions & 2 deletions pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,16 @@ 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))]
# If index_names are integers, determine whether the integers refer
# to the level position or name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Add a period at the end of this comment.

index_names = agged.index.names[:len(index)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comment here about level names being in integer could be ambiguous (and ref the issue number about this; I think we have one)

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
10 changes: 10 additions & 0 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,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(object):
"""
Expand Down