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.
feat: Implement `unique/n_unique/unique_counts/is_unique/is_duplicate…
…d` for `Null` series (pola-rs#13307)
- Loading branch information
Showing
17 changed files
with
310 additions
and
217 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
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
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 was deleted.
Oops, something went wrong.
Empty file.
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,87 @@ | ||
import polars as pl | ||
from polars.testing import assert_series_equal | ||
|
||
|
||
def test_is_unique_series() -> None: | ||
s = pl.Series("a", [1, 2, 2, 3]) | ||
assert_series_equal(s.is_unique(), pl.Series("a", [True, False, False, True])) | ||
|
||
# str | ||
assert pl.Series(["a", "b", "c", "a"]).is_duplicated().to_list() == [ | ||
True, | ||
False, | ||
False, | ||
True, | ||
] | ||
assert pl.Series(["a", "b", "c", "a"]).is_unique().to_list() == [ | ||
False, | ||
True, | ||
True, | ||
False, | ||
] | ||
|
||
|
||
def test_is_unique() -> None: | ||
df = pl.DataFrame({"foo": [1, 2, 2], "bar": [6, 7, 7]}) | ||
|
||
assert_series_equal(df.is_unique(), pl.Series("", [True, False, False])) | ||
assert df.unique(maintain_order=True).rows() == [(1, 6), (2, 7)] | ||
assert df.n_unique() == 2 | ||
|
||
|
||
def test_is_unique2() -> None: | ||
df = pl.DataFrame({"a": [4, 1, 4]}) | ||
result = df.select(pl.col("a").is_unique())["a"] | ||
assert_series_equal(result, pl.Series("a", [False, True, False])) | ||
|
||
|
||
def test_is_unique_null() -> None: | ||
s = pl.Series([]) | ||
expected = pl.Series([], dtype=pl.Boolean) | ||
assert_series_equal(s.is_unique(), expected) | ||
|
||
s = pl.Series([None]) | ||
expected = pl.Series([True], dtype=pl.Boolean) | ||
assert_series_equal(s.is_unique(), expected) | ||
|
||
s = pl.Series([None, None, None]) | ||
expected = pl.Series([False, False, False], dtype=pl.Boolean) | ||
assert_series_equal(s.is_unique(), expected) | ||
|
||
|
||
def test_is_unique_struct() -> None: | ||
assert pl.Series( | ||
[{"a": 1, "b": 1}, {"a": 2, "b": 1}, {"a": 1, "b": 1}] | ||
).is_unique().to_list() == [False, True, False] | ||
assert pl.Series( | ||
[{"a": 1, "b": 1}, {"a": 2, "b": 1}, {"a": 1, "b": 1}] | ||
).is_duplicated().to_list() == [True, False, True] | ||
|
||
|
||
def test_is_duplicated_series() -> None: | ||
s = pl.Series("a", [1, 2, 2, 3]) | ||
assert_series_equal(s.is_duplicated(), pl.Series("a", [False, True, True, False])) | ||
|
||
|
||
def test_is_duplicated_df() -> None: | ||
df = pl.DataFrame({"foo": [1, 2, 2], "bar": [6, 7, 7]}) | ||
assert_series_equal(df.is_duplicated(), pl.Series("", [False, True, True])) | ||
|
||
|
||
def test_is_duplicated_lf() -> None: | ||
ldf = pl.LazyFrame({"a": [4, 1, 4]}).select(pl.col("a").is_duplicated()) | ||
assert_series_equal(ldf.collect()["a"], pl.Series("a", [True, False, True])) | ||
|
||
|
||
def test_is_duplicated_null() -> None: | ||
s = pl.Series([]) | ||
expected = pl.Series([], dtype=pl.Boolean) | ||
assert_series_equal(s.is_duplicated(), expected) | ||
|
||
s = pl.Series([None]) | ||
expected = pl.Series([False], dtype=pl.Boolean) | ||
assert_series_equal(s.is_duplicated(), expected) | ||
|
||
s = pl.Series([None, None, None]) | ||
expected = pl.Series([True, True, True], dtype=pl.Boolean) | ||
assert_series_equal(s.is_duplicated(), expected) |
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,32 @@ | ||
import polars as pl | ||
|
||
|
||
def test_n_unique() -> None: | ||
s = pl.Series("s", [11, 11, 11, 22, 22, 33, None, None, None]) | ||
assert s.n_unique() == 4 | ||
|
||
|
||
def test_n_unique_subsets() -> None: | ||
df = pl.DataFrame( | ||
{ | ||
"a": [1, 1, 2, 3, 4, 5], | ||
"b": [0.5, 0.5, 1.0, 2.0, 3.0, 3.0], | ||
"c": [True, True, True, False, True, True], | ||
} | ||
) | ||
# omitting 'subset' counts unique rows | ||
assert df.n_unique() == 5 | ||
|
||
# providing it counts unique col/expr subsets | ||
assert df.n_unique(subset=["b", "c"]) == 4 | ||
assert df.n_unique(subset=pl.col("c")) == 2 | ||
assert ( | ||
df.n_unique(subset=[(pl.col("a") // 2), (pl.col("c") | (pl.col("b") >= 2))]) | ||
== 3 | ||
) | ||
|
||
|
||
def test_n_unique_null() -> None: | ||
assert pl.Series([]).n_unique() == 0 | ||
assert pl.Series([None]).n_unique() == 1 | ||
assert pl.Series([None, None]).n_unique() == 1 |
Oops, something went wrong.