forked from pola-rs/polars
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
320 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
from __future__ import annotations | ||
|
||
from datetime import date | ||
|
||
import pytest | ||
|
||
import polars as pl | ||
from polars.testing import assert_frame_equal | ||
|
||
|
||
def test_df_describe() -> None: | ||
df = pl.DataFrame( | ||
{ | ||
"a": [1.0, 2.8, 3.0], | ||
"b": [4, 5, None], | ||
"c": [True, False, True], | ||
"d": [None, "b", "c"], | ||
"e": ["usd", "eur", None], | ||
"f": [date(2020, 1, 1), date(2021, 1, 1), date(2022, 1, 1)], | ||
}, | ||
schema_overrides={"e": pl.Categorical}, | ||
) | ||
|
||
result = df.describe() | ||
|
||
expected = pl.DataFrame( | ||
[ | ||
("count", 3.0, 2.0, 3.0, 2, 2, 3), | ||
("null_count", 0.0, 1.0, 0.0, 1, 1, 0), | ||
("mean", 2.266667, 4.5, 0.666667, None, None, None), | ||
("std", 1.101514, 0.707107, 0.57735, None, None, None), | ||
("min", 1.0, 4.0, 0.0, "b", None, "2020-01-01"), | ||
("25%", 1.0, 4.0, None, None, None, None), | ||
("50%", 2.8, 5.0, None, None, None, None), | ||
("75%", 3.0, 5.0, None, None, None, None), | ||
("max", 3.0, 5.0, 1.0, "c", None, "2022-01-01"), | ||
], | ||
schema=["describe"] + df.columns, | ||
schema_overrides={"e": pl.Utf8}, | ||
) | ||
assert_frame_equal(result, expected) | ||
|
||
|
||
def test_df_describe_nested() -> None: | ||
df = pl.DataFrame( | ||
{ | ||
"struct": [{"x": 1, "y": 2}, {"x": 3, "y": 4}, {"x": 1, "y": 2}, None], | ||
"list": [[1, 2], [3, 4], [1, 2], None], | ||
} | ||
) | ||
|
||
result = df.describe() | ||
|
||
expected = pl.DataFrame( | ||
[ | ||
("count", 3, 3), | ||
("null_count", 1, 1), | ||
("mean", None, None), | ||
("std", None, None), | ||
("min", None, None), | ||
("25%", None, None), | ||
("50%", None, None), | ||
("75%", None, None), | ||
("max", None, None), | ||
], | ||
schema=["describe"] + df.columns, | ||
schema_overrides={"struct": pl.Utf8, "list": pl.Utf8}, | ||
) | ||
assert_frame_equal(result, expected) | ||
|
||
|
||
def test_df_describe_custom_percentiles() -> None: | ||
df = pl.DataFrame({"numeric": [1, 2, 1, None]}) | ||
|
||
result = df.describe(percentiles=(0.2, 0.4, 0.5, 0.6, 0.8)) | ||
|
||
expected = pl.DataFrame( | ||
[ | ||
("count", 3.0), | ||
("null_count", 1.0), | ||
("mean", 1.3333333333333333), | ||
("std", 0.5773502691896257), | ||
("min", 1.0), | ||
("20%", 1.0), | ||
("40%", 1.0), | ||
("50%", 1.0), | ||
("60%", 1.0), | ||
("80%", 2.0), | ||
("max", 2.0), | ||
], | ||
schema=["describe"] + df.columns, | ||
) | ||
assert_frame_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize("pcts", [None, []]) | ||
def test_df_describe_no_percentiles(pcts: list[float] | None) -> None: | ||
df = pl.DataFrame({"numeric": [1, 2, 1, None]}) | ||
|
||
result = df.describe(percentiles=pcts) | ||
|
||
expected = pl.DataFrame( | ||
[ | ||
("count", 3.0), | ||
("null_count", 1.0), | ||
("mean", 1.3333333333333333), | ||
("std", 0.5773502691896257), | ||
("min", 1.0), | ||
("max", 2.0), | ||
], | ||
schema=["describe"] + df.columns, | ||
) | ||
assert_frame_equal(result, expected) | ||
|
||
|
||
def test_df_describe_empty_column() -> None: | ||
df = pl.DataFrame(schema={"a": pl.Int64}) | ||
|
||
result = df.describe() | ||
|
||
expected = pl.DataFrame( | ||
[ | ||
("count", 0.0), | ||
("null_count", 0.0), | ||
("mean", None), | ||
("std", None), | ||
("min", None), | ||
("25%", None), | ||
("50%", None), | ||
("75%", None), | ||
("max", None), | ||
], | ||
schema=["describe"] + df.columns, | ||
) | ||
assert_frame_equal(result, expected) | ||
|
||
|
||
def test_df_describe_empty() -> None: | ||
df = pl.DataFrame() | ||
with pytest.raises( | ||
TypeError, match="cannot describe a DataFrame without any columns" | ||
): | ||
df.describe() |
Oops, something went wrong.