diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2650090a3f61a..a5edd121ad21f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -8250,6 +8250,12 @@ def groupby( Parameters ----------%s + columns : str or object or a list of str + Column to use to make new frame's columns. + + .. versionchanged:: 1.1.0 + Also accept list of columns names. + index : str or object or a list of str, optional Column to use to make new frame's index. If None, uses existing index. @@ -8257,12 +8263,6 @@ def groupby( .. versionchanged:: 1.1.0 Also accept list of index names. - columns : str or object or a list of str - Column to use to make new frame's columns. - - .. versionchanged:: 1.1.0 - Also accept list of columns names. - values : str, object or a list of the previous, optional Column(s) to use for populating new frame's values. If not specified, all remaining columns will be used and the result will @@ -8385,9 +8385,7 @@ def groupby( @Substitution("") @Appender(_shared_docs["pivot"]) - def pivot( - self, *, index=lib.NoDefault, columns=lib.NoDefault, values=lib.NoDefault - ) -> DataFrame: + def pivot(self, *, columns, index=lib.NoDefault, values=lib.NoDefault) -> DataFrame: from pandas.core.reshape.pivot import pivot return pivot(self, index=index, columns=columns, values=values) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index eb95bb2484ebf..91a97f743efeb 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -500,12 +500,10 @@ def _convert_by(by): def pivot( data: DataFrame, *, + columns: IndexLabel, index: IndexLabel | lib.NoDefault = lib.NoDefault, - columns: IndexLabel | lib.NoDefault = lib.NoDefault, values: IndexLabel | lib.NoDefault = lib.NoDefault, ) -> DataFrame: - if columns is lib.NoDefault: - raise TypeError("pivot() missing 1 required argument: 'columns'") columns_listlike = com.convert_to_list_like(columns) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 2406379892834..b3d2a366bf58a 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -799,7 +799,7 @@ def test_pivot_with_list_like_values_nans(self, values, method): def test_pivot_columns_none_raise_error(self): # GH 30924 df = DataFrame({"col1": ["a", "b", "c"], "col2": [1, 2, 3], "col3": [1, 2, 3]}) - msg = r"pivot\(\) missing 1 required argument: 'columns'" + msg = r"pivot\(\) missing 1 required keyword-only argument: 'columns'" with pytest.raises(TypeError, match=msg): df.pivot(index="col1", values="col3") @@ -2513,7 +2513,7 @@ def test_pivot_index_list_values_none_immutable_args(self): def test_pivot_columns_not_given(self): # GH#48293 df = DataFrame({"a": [1], "b": 1}) - with pytest.raises(TypeError, match="missing 1 required argument"): + with pytest.raises(TypeError, match="missing 1 required keyword-only argument"): df.pivot() def test_pivot_columns_is_none(self):