Skip to content

Fix pivot index bug #37771

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

Merged
merged 11 commits into from
Nov 14, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ Reshaping
- Bug in :meth:`DataFrame.agg` with ``func={'name':<FUNC>}`` incorrectly raising ``TypeError`` when ``DataFrame.columns==['Name']`` (:issue:`36212`)
- Bug in :meth:`Series.transform` would give incorrect results or raise when the argument ``func`` was dictionary (:issue:`35811`)
- Bug in :meth:`DataFrame.pivot` did not preserve :class:`MultiIndex` level names for columns when rows and columns both multiindexed (:issue:`36360`)
- Bug in :meth:``DataFrame.pivot`` modified caller's ``index`` parameter when ``columns`` was passed but ``values`` was not (:issue:`37635`)
- Bug in :func:`join` returned a non deterministic level-order for the resulting :class:`MultiIndex` (:issue:`36910`)
- Bug in :meth:`DataFrame.combine_first()` caused wrong alignment with dtype ``string`` and one level of ``MultiIndex`` containing only ``NA`` (:issue:`37591`)
- Fixed regression in :func:`merge` on merging DatetimeIndex with empty DataFrame (:issue:`36895`)
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,13 +447,12 @@ def pivot(

if values is None:
if index is not None:
cols = com.convert_to_list_like(index)
index = com.convert_to_list_like(index)
else:
cols = []
cols.extend(columns)
index = []

append = index is None
indexed = data.set_index(cols, append=append)
indexed = data.set_index(index + columns, append=append)
else:
if index is None:
index = [Series(data.index, name=data.index.name)]
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2153,3 +2153,20 @@ def test_pivot_index_none(self):

expected.columns.name = "columns"
tm.assert_frame_equal(result, expected)

def test_pivot_index_list_values_none(self):
Copy link
Member

Choose a reason for hiding this comment

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

I might be wrong but I don't think this is testing the OP...?

I'd use the example from the issue. Do the pivot and test the result of the pivot operation, then test that the arguments have not changed

Copy link
Member

Choose a reason for hiding this comment

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

Actually definitely use that. This isn't quite the same use case

Copy link
Member

Choose a reason for hiding this comment

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

call it test_pivot_index_list_values_none_modifies_args

# Tests when index is a list and values None. See GH#37635
Copy link
Member

Choose a reason for hiding this comment

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

just do

# GH37635

data = {
"index": ["A", "B", "C", "C", "B", "A"],
"columns": ["One", "One", "One", "Two", "Two", "Two"],
"values": [1.0, 2.0, 3.0, 3.0, 2.0, 1.0],
}
index = ["index"]
columns = ["columns"]
frame = DataFrame(data)
frame.pivot(index=index, columns=columns, values=None)

Copy link
Member

Choose a reason for hiding this comment

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

check here that you got the correct result from the pivot (might need to hard-code)

expected_ser = Series(["index"])
Copy link
Member

Choose a reason for hiding this comment

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

Why are you converting to Series? you want to check that index itself (the list) did not change, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was trying to use the assert functions in pandas._testing. Should I just use standard assert?

Copy link
Member

Choose a reason for hiding this comment

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

yes use a standard assert here

result_ser = Series(index)

tm.assert_series_equal(result_ser, expected_ser)