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

feat(python)!: Remove Groupby.pivot #6016

Merged
merged 3 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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: 0 additions & 1 deletion py-polars/docs/source/reference/dataframe/groupby.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ This namespace is available after calling :code:`DataFrame.groupby(...)`.
GroupBy.median
GroupBy.min
GroupBy.n_unique
GroupBy.pivot
GroupBy.quantile
GroupBy.sum
GroupBy.tail
1 change: 0 additions & 1 deletion py-polars/docs/source/reference/dataframe/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ This page gives an overview of all public DataFrame methods.
groupby
modify_select
miscellaneous
pivot

.. currentmodule:: polars

Expand Down
25 changes: 0 additions & 25 deletions py-polars/docs/source/reference/dataframe/pivot.rst

This file was deleted.

66 changes: 0 additions & 66 deletions py-polars/polars/internals/dataframe/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import TYPE_CHECKING, Callable, Generic, Iterator, Sequence, TypeVar

import polars.internals as pli
from polars.internals.dataframe.pivot import PivotOps
from polars.utils import _timedelta_to_pl_duration, is_str_sequence

if TYPE_CHECKING:
Expand Down Expand Up @@ -361,71 +360,6 @@ def tail(self, n: int = 5) -> DF:
)
return self._dataframe_class._from_pydf(df._df)

def pivot(
self, pivot_column: str | list[str], values_column: str | list[str]
) -> PivotOps[DF]:
"""
Do a pivot operation.

The pivot operation is based on the group key, a pivot column and an aggregation
function on the values column.

.. deprecated:: 0.13.23
`DataFrame.groupby.pivot` will be removed in favour of `DataFrame.pivot`.

Parameters
----------
pivot_column
Column to pivot.
values_column
Column that will be aggregated.

Notes
-----
Polars'/arrow memory is not ideal for transposing operations like pivots.
If you have a relatively large table, consider using a groupby over a pivot.

Examples
--------
>>> df = pl.DataFrame(
... {
... "foo": ["one", "one", "one", "two", "two", "two"],
... "bar": ["A", "B", "C", "A", "B", "C"],
... "baz": [1, 2, 3, 4, 5, 6],
... }
... )
>>> df.groupby("foo", maintain_order=True).pivot( # doctest: +SKIP
... pivot_column="bar", values_column="baz"
... ).first()
shape: (2, 4)
┌─────┬─────┬─────┬─────┐
│ foo ┆ A ┆ B ┆ C │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╪═════╡
│ one ┆ 1 ┆ 2 ┆ 3 │
│ two ┆ 4 ┆ 5 ┆ 6 │
└─────┴─────┴─────┴─────┘

"""
warnings.warn(
"`DataFrame.groupby.pivot` is deprecated and will be removed in a future"
" version. Use `DataFrame.pivot` instead.",
DeprecationWarning,
stacklevel=2,
)
if isinstance(pivot_column, str):
pivot_column = [pivot_column]
if isinstance(values_column, str):
values_column = [values_column]
return PivotOps(
self._df,
self.by, # type: ignore[arg-type]
pivot_column,
values_column,
dataframe_class=self._dataframe_class,
)

def first(self) -> pli.DataFrame:
"""
Aggregate the first values in the group.
Expand Down
81 changes: 0 additions & 81 deletions py-polars/polars/internals/dataframe/pivot.py

This file was deleted.

7 changes: 0 additions & 7 deletions py-polars/tests/unit/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -2214,13 +2214,6 @@ class SubClassedDataFrame(pl.DataFrame):
groupby = subclassed_df.groupby("a")
assert isinstance(groupby.agg(pl.count()), SubClassedDataFrame)

# Round-trips to PivotOps and back should also preserve subclass
with pytest.deprecated_call():
assert isinstance(
groupby.pivot(pivot_column="a", values_column="b").first(),
SubClassedDataFrame,
)


def test_explode_empty() -> None:
df = (
Expand Down
28 changes: 0 additions & 28 deletions py-polars/tests/unit/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,6 @@ def test_pivot_aggregate(agg_fn: PivotAgg, expected_rows: list[tuple[Any]]) -> N
assert result.rows() == expected_rows


@pytest.mark.parametrize(
("agg_fn", "expected_rows"),
[
("first", [("a", 2, None, None), ("b", None, None, 10)]),
("count", [("a", 2, None, None), ("b", None, 2, 1)]),
("min", [("a", 2, None, None), ("b", None, 8, 10)]),
("max", [("a", 4, None, None), ("b", None, 8, 10)]),
("sum", [("a", 6, None, None), ("b", None, 8, 10)]),
("mean", [("a", 3.0, None, None), ("b", None, 8.0, 10.0)]),
("median", [("a", 3.0, None, None), ("b", None, 8.0, 10.0)]),
],
)
def test_pivot_groupby_aggregate(
agg_fn: PivotAgg, expected_rows: list[tuple[Any]]
) -> None:
df = pl.DataFrame(
{
"a": [1, 1, 2, 2, 3],
"b": ["a", "a", "b", "b", "b"],
"c": [2, 4, None, 8, 10],
}
)
with pytest.deprecated_call():
pivot = df.groupby("b").pivot(pivot_column="a", values_column="c")
result = getattr(pivot, agg_fn)()
assert result.rows() == expected_rows


def test_pivot_categorical_3968() -> None:
df = pl.DataFrame(
{
Expand Down