Skip to content

Commit

Permalink
Remove sort inplace (#4328)
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Aug 9, 2022
1 parent 144e57a commit fb971a2
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 82 deletions.
56 changes: 2 additions & 54 deletions py-polars/polars/internals/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2303,47 +2303,12 @@ def replace_at_idx(self, index: int, series: pli.Series) -> None:
index = len(self.columns) + index
self._df.replace_at_idx(index, series._s)

@overload
def sort(
self,
by: str | pli.Expr | list[str] | list[pli.Expr],
reverse: bool | list[bool] = ...,
nulls_last: bool = ...,
*,
in_place: Literal[False] = ...,
) -> DataFrame:
...

@overload
def sort(
self,
by: str | pli.Expr | list[str] | list[pli.Expr],
reverse: bool | list[bool] = ...,
nulls_last: bool = ...,
*,
in_place: Literal[True],
) -> None:
...

@overload
def sort(
self,
by: str | pli.Expr | list[str] | list[pli.Expr],
reverse: bool | list[bool] = ...,
nulls_last: bool = ...,
*,
in_place: bool,
) -> DataFrame | None:
...

def sort(
self,
by: str | pli.Expr | list[str] | list[pli.Expr],
reverse: bool | list[bool] = False,
nulls_last: bool = False,
*,
in_place: bool = False,
) -> DataFrame | None:
) -> DataFrame:
"""
Sort the DataFrame by column.
Expand All @@ -2353,8 +2318,6 @@ def sort(
By which column to sort. Only accepts string.
reverse
Reverse/descending sort.
in_place
Perform operation in-place.
nulls_last
Place null values last. Can only be used if sorted by a single column.
Expand Down Expand Up @@ -2408,23 +2371,8 @@ def sort(
.sort(by, reverse, nulls_last)
.collect(no_optimization=True, string_cache=False)
)
if in_place: # pragma: no cover
warnings.warn(
"in-place sorting is deprecated; please use default sorting",
DeprecationWarning,
)
self._df = df._df
return self
return df
if in_place: # pragma: no cover
warnings.warn(
"in-place sorting is deprecated; please use default sorting",
DeprecationWarning,
)
self._df.sort_in_place(by, reverse)
return None
else:
return self._from_pydf(self._df.sort(by, reverse, nulls_last))
return self._from_pydf(self._df.sort(by, reverse, nulls_last))

def frame_equal(self, other: DataFrame, null_equal: bool = True) -> bool:
"""
Expand Down
7 changes: 0 additions & 7 deletions py-polars/src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -984,13 +984,6 @@ impl PyDataFrame {
Ok(PyDataFrame::new(df))
}

pub fn sort_in_place(&mut self, by_column: &str, reverse: bool) -> PyResult<()> {
self.df
.sort_in_place([by_column], reverse)
.map_err(PyPolarsErr::from)?;
Ok(())
}

pub fn replace(&mut self, column: &str, new_col: PySeries) -> PyResult<()> {
self.df
.replace(column, new_col.series)
Expand Down
13 changes: 4 additions & 9 deletions py-polars/tests/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,10 @@ def test_dataframe_membership_operator() -> None:

def test_sort() -> None:
df = pl.DataFrame({"a": [2, 1, 3], "b": [1, 2, 3]})
with pytest.deprecated_call():
df.sort("a", in_place=True)
assert df.frame_equal(pl.DataFrame({"a": [1, 2, 3], "b": [2, 1, 3]}))

# test in-place + passing a list
df = pl.DataFrame({"a": [2, 1, 3], "b": [1, 2, 3]})
with pytest.deprecated_call():
df.sort(["a", "b"], in_place=True)
assert df.frame_equal(pl.DataFrame({"a": [1, 2, 3], "b": [2, 1, 3]}))
assert df.sort("a").frame_equal(pl.DataFrame({"a": [1, 2, 3], "b": [2, 1, 3]}))
assert df.sort(["a", "b"]).frame_equal(
pl.DataFrame({"a": [1, 2, 3], "b": [2, 1, 3]})
)


def test_replace() -> None:
Expand Down
12 changes: 0 additions & 12 deletions py-polars/tests/test_sort.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

import pytest

import polars as pl


Expand Down Expand Up @@ -57,16 +55,6 @@ def test_sort_by() -> None:
assert out["a"].to_list() == [1, 2, 3, 4, 5]


def test_sort_in_place() -> None:
df = pl.DataFrame({"a": [1, 3, 2, 4, 5]})
with pytest.deprecated_call():
ret = df.sort("a", in_place=True)
result = df["a"].to_list()
expected = [1, 2, 3, 4, 5]
assert result == expected
assert ret is None


def test_sort_by_exprs() -> None:
# make sure that the expression does not overwrite columns in the dataframe
df = pl.DataFrame({"a": [1, 2, -1, -2]})
Expand Down

0 comments on commit fb971a2

Please sign in to comment.