Skip to content

Commit

Permalink
chore(python): Use Dataframe.item internally and in tests (#6109)
Browse files Browse the repository at this point in the history
  • Loading branch information
zundertj committed Jan 8, 2023
1 parent 2f7f212 commit 345156d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 18 deletions.
6 changes: 3 additions & 3 deletions py-polars/polars/internals/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ def nan_max(self) -> int | float | date | datetime | timedelta | str:
whereas polars defaults to ignoring them.
"""
return self.to_frame().select(pli.col(self.name).nan_max())[0, 0]
return self.to_frame().select(pli.col(self.name).nan_max()).item()

def nan_min(self) -> int | float | date | datetime | timedelta | str:
"""
Expand All @@ -1293,7 +1293,7 @@ def nan_min(self) -> int | float | date | datetime | timedelta | str:
whereas polars defaults to ignoring them.
"""
return self.to_frame().select(pli.col(self.name).nan_min())[0, 0]
return self.to_frame().select(pli.col(self.name).nan_min()).item()

def std(self, ddof: int = 1) -> float | None:
"""
Expand Down Expand Up @@ -2095,7 +2095,7 @@ def search_sorted(
"""
if isinstance(element, (int, float)):
return pli.select(pli.lit(self).search_sorted(element, side))[0, 0]
return pli.select(pli.lit(self).search_sorted(element, side)).item()
element = Series(element)
return pli.select(pli.lit(self).search_sorted(element, side)).to_series()

Expand Down
8 changes: 4 additions & 4 deletions py-polars/tests/unit/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1771,12 +1771,12 @@ def test_from_dict_tu_consistency() -> None:

def test_date_parse_omit_day() -> None:
df = pl.DataFrame({"month": ["2022-01"]})
assert df.select(pl.col("month").str.strptime(pl.Date, fmt="%Y-%m"))[0, 0] == date(
assert df.select(pl.col("month").str.strptime(pl.Date, fmt="%Y-%m")).item() == date(
2022, 1, 1
)
assert df.select(pl.col("month").str.strptime(pl.Datetime, fmt="%Y-%m"))[
0, 0
] == datetime(2022, 1, 1)
assert df.select(
pl.col("month").str.strptime(pl.Datetime, fmt="%Y-%m")
).item() == datetime(2022, 1, 1)


def test_shift_and_fill_group_logicals() -> None:
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/test_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_count_expr() -> None:

out = df.select(pl.count())
assert out.shape == (1, 1)
assert cast(int, out[0, 0]) == 5
assert cast(int, out.item()) == 5

out = df.groupby("b", maintain_order=True).agg(pl.count())
assert out["b"].to_list() == ["a", "b"]
Expand Down
4 changes: 2 additions & 2 deletions py-polars/tests/unit/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ def test_null_handling_correlation() -> None:
df1 = pl.DataFrame({"a": [None, 1, 2], "b": [None, 2, 1]})
df2 = pl.DataFrame({"a": [np.nan, 1, 2], "b": [np.nan, 2, 1]})

assert np.isclose(df1.select(pl.spearman_rank_corr("a", "b"))[0, 0], -1.0)
assert np.isclose(df1.select(pl.spearman_rank_corr("a", "b")).item(), -1.0)
assert (
str(df2.select(pl.spearman_rank_corr("a", "b", propagate_nans=True))[0, 0])
str(df2.select(pl.spearman_rank_corr("a", "b", propagate_nans=True)).item())
== "nan"
)

Expand Down
14 changes: 7 additions & 7 deletions py-polars/tests/unit/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def test_is_finite_is_infinite() -> None:

def test_len() -> None:
df = pl.DataFrame({"nrs": [1, 2, 3]})
assert cast(int, df.select(col("nrs").len())[0, 0]) == 3
assert cast(int, df.select(col("nrs").len()).item()) == 3


def test_cum_agg() -> None:
Expand Down Expand Up @@ -534,7 +534,7 @@ def test_round() -> None:

def test_dot() -> None:
df = pl.DataFrame({"a": [1.8, 1.2, 3.0], "b": [3.2, 1, 2]})
assert cast(float, df.select(pl.col("a").dot(pl.col("b")))[0, 0]) == 12.96
assert cast(float, df.select(pl.col("a").dot(pl.col("b"))).item()) == 12.96


def test_sort() -> None:
Expand Down Expand Up @@ -1055,16 +1055,16 @@ def test_join_suffix() -> None:
def test_str_concat() -> None:
df = pl.DataFrame({"foo": [1, None, 2]})
df = df.select(pl.col("foo").str.concat("-"))
assert cast(str, df[0, 0]) == "1-null-2"
assert cast(str, df.item()) == "1-null-2"


@pytest.mark.parametrize("no_optimization", [False, True])
def test_collect_all(df: pl.DataFrame, no_optimization: bool) -> None:
lf1 = df.lazy().select(pl.col("int").sum())
lf2 = df.lazy().select((pl.col("floats") * 2).sum())
out = pl.collect_all([lf1, lf2], no_optimization=no_optimization)
assert cast(int, out[0][0, 0]) == 6
assert cast(float, out[1][0, 0]) == 12.0
assert cast(int, out[0].item()) == 6
assert cast(float, out[1].item()) == 12.0


def test_spearman_corr() -> None:
Expand Down Expand Up @@ -1120,9 +1120,9 @@ def test_pearson_corr() -> None:


def test_cov(fruits_cars: pl.DataFrame) -> None:
assert cast(float, fruits_cars.select(pl.cov("A", "B"))[0, 0]) == -2.5
assert cast(float, fruits_cars.select(pl.cov("A", "B")).item()) == -2.5
assert (
cast(float, fruits_cars.select(pl.cov(pl.col("A"), pl.col("B")))[0, 0]) == -2.5
cast(float, fruits_cars.select(pl.cov(pl.col("A"), pl.col("B"))).item()) == -2.5
)


Expand Down
3 changes: 2 additions & 1 deletion py-polars/tests/unit/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def test_bool_numeric_supertype() -> None:
pl.Int64,
]:
assert (
df.select([(pl.col("v") < 3).sum().cast(dt) / pl.count()])[0, 0] - 0.3333333
df.select([(pl.col("v") < 3).sum().cast(dt) / pl.count()]).item()
- 0.3333333
<= 0.00001
)

Expand Down

0 comments on commit 345156d

Please sign in to comment.