Skip to content

Commit f792d8c

Browse files
charlesdong1991jreback
authored andcommitted
BUG: correct wrong error message in df.pivot when columns=None (#30925)
1 parent 55cfabb commit f792d8c

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ Reshaping
142142

143143
-
144144
- Bug in :meth:`DataFrame.pivot_table` when only MultiIndexed columns is set (:issue:`17038`)
145+
- Fix incorrect error message in :meth:`DataFrame.pivot` when ``columns`` is set to ``None``. (:issue:`30924`)
145146
- Bug in :func:`crosstab` when inputs are two Series and have tuple names, the output will keep dummy MultiIndex as columns. (:issue:`18321`)
146147

147148

pandas/core/reshape/pivot.py

+3
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,9 @@ def _convert_by(by):
429429
@Substitution("\ndata : DataFrame")
430430
@Appender(_shared_docs["pivot"], indents=1)
431431
def pivot(data: "DataFrame", index=None, columns=None, values=None) -> "DataFrame":
432+
if columns is None:
433+
raise TypeError("pivot() missing 1 required argument: 'columns'")
434+
432435
if values is None:
433436
cols = [columns] if index is None else [index, columns]
434437
append = index is None

pandas/tests/reshape/test_pivot.py

+9
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,15 @@ def test_pivot_with_list_like_values_nans(self, values, method):
781781
expected = DataFrame(data=data, index=index, columns=columns, dtype="object")
782782
tm.assert_frame_equal(result, expected)
783783

784+
def test_pivot_columns_none_raise_error(self):
785+
# GH 30924
786+
df = pd.DataFrame(
787+
{"col1": ["a", "b", "c"], "col2": [1, 2, 3], "col3": [1, 2, 3]}
788+
)
789+
msg = r"pivot\(\) missing 1 required argument: 'columns'"
790+
with pytest.raises(TypeError, match=msg):
791+
df.pivot(index="col1", values="col3")
792+
784793
@pytest.mark.xfail(
785794
reason="MultiIndexed unstack with tuple names fails with KeyError GH#19966"
786795
)

0 commit comments

Comments
 (0)