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

test: Add various tests #20768

Merged
merged 1 commit into from
Jan 17, 2025
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
7 changes: 7 additions & 0 deletions py-polars/tests/unit/dataframe/test_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,10 @@ def test_df_serialize_invalid_type() -> None:
ComputeError, match="serializing data of type Object is not supported"
):
df.serialize()


def test_df_serde_list_of_null_17230() -> None:
df = pl.Series([[]], dtype=pl.List(pl.Null)).to_frame()
ser = df.serialize(format="json")
result = pl.DataFrame.deserialize(io.StringIO(ser), format="json")
assert_frame_equal(result, df)
25 changes: 25 additions & 0 deletions py-polars/tests/unit/datatypes/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,31 @@ def test_nested_enum_concat() -> None:
assert_series_equal(s1.extend(s2), expected)


def test_nested_enum_agg_sort_18026() -> None:
df = (
pl.DataFrame({"a": [1, 1, 2, 2], "b": ["Y", "Z", "Z", "Y"]})
.cast({"b": pl.Enum(["Z", "Y"])})
.with_columns(pl.struct("b", "a").alias("c"))
)
result = df.group_by("a").agg("c").sort("a")
expected = pl.DataFrame(
{
"a": [1, 2],
"c": [
[{"b": "Y", "a": 1}, {"b": "Z", "a": 1}],
[{"b": "Z", "a": 2}, {"b": "Y", "a": 2}],
],
},
schema={
"a": pl.Int64,
"c": pl.List(
pl.Struct([pl.Field("b", pl.Enum(["Z", "Y"])), pl.Field("a", pl.Int64)])
),
},
)
assert_frame_equal(result, expected)


def test_casting_to_an_enum_from_utf() -> None:
dtype = pl.Enum(["a", "b", "c"])
s = pl.Series([None, "a", "b", "c"])
Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/unit/io/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2501,3 +2501,10 @@ def test_trailing_separator_8240() -> None:

result = pl.scan_csv(io.StringIO(csv), separator="|", has_header=False).collect()
assert_frame_equal(result, expected)


def test_header_only_column_selection_17173() -> None:
csv = "A,B"
result = pl.read_csv(io.StringIO(csv), columns=["B"])
expected = pl.Series("B", [], pl.String()).to_frame()
assert_frame_equal(result, expected)
7 changes: 7 additions & 0 deletions py-polars/tests/unit/operations/test_fill_null.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,10 @@ def test_fill_null_lit_() -> None:
df.fill_null(pl.lit(0)).select(pl.all().null_count()).transpose().sum().item()
== 0
)


def test_fill_null_decimal_with_int_14331() -> None:
s = pl.Series("a", ["1.1", None], dtype=pl.Decimal(precision=None, scale=5))
result = s.fill_null(0)
expected = pl.Series("a", ["1.1", "0.0"], dtype=pl.Decimal(precision=None, scale=5))
assert_series_equal(result, expected)
8 changes: 8 additions & 0 deletions py-polars/tests/unit/series/test_item.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import datetime

import pytest

import polars as pl
Expand Down Expand Up @@ -36,3 +38,9 @@ def test_series_item_with_index(index: int, expected: int, s: pl.Series) -> None
def test_df_item_out_of_bounds(index: int, s: pl.Series) -> None:
with pytest.raises(IndexError, match="out of bounds"):
s.item(index)


def test_series_item_out_of_range_date() -> None:
s = pl.Series([datetime.date(9999, 12, 31)]).dt.offset_by("1d")
with pytest.raises(ValueError, match="out of range"):
s.item()
Loading