From bcf67532bbddb51722d649b7b536342f4ee82fc8 Mon Sep 17 00:00:00 2001 From: Luke Manley Date: Fri, 17 Jan 2025 14:29:22 -0500 Subject: [PATCH] test: Add various tests (#20768) --- py-polars/tests/unit/dataframe/test_serde.py | 7 ++++++ py-polars/tests/unit/datatypes/test_enum.py | 25 +++++++++++++++++++ py-polars/tests/unit/io/test_csv.py | 7 ++++++ .../tests/unit/operations/test_fill_null.py | 7 ++++++ py-polars/tests/unit/series/test_item.py | 8 ++++++ 5 files changed, 54 insertions(+) diff --git a/py-polars/tests/unit/dataframe/test_serde.py b/py-polars/tests/unit/dataframe/test_serde.py index 64dd42255dfe..91b7f6e3a20f 100644 --- a/py-polars/tests/unit/dataframe/test_serde.py +++ b/py-polars/tests/unit/dataframe/test_serde.py @@ -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) diff --git a/py-polars/tests/unit/datatypes/test_enum.py b/py-polars/tests/unit/datatypes/test_enum.py index 95e522f5c906..37f0ceb904ac 100644 --- a/py-polars/tests/unit/datatypes/test_enum.py +++ b/py-polars/tests/unit/datatypes/test_enum.py @@ -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"]) diff --git a/py-polars/tests/unit/io/test_csv.py b/py-polars/tests/unit/io/test_csv.py index 22276a1854b4..7cdc1793ad5c 100644 --- a/py-polars/tests/unit/io/test_csv.py +++ b/py-polars/tests/unit/io/test_csv.py @@ -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) diff --git a/py-polars/tests/unit/operations/test_fill_null.py b/py-polars/tests/unit/operations/test_fill_null.py index 754e98f3834a..609fc847f316 100644 --- a/py-polars/tests/unit/operations/test_fill_null.py +++ b/py-polars/tests/unit/operations/test_fill_null.py @@ -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) diff --git a/py-polars/tests/unit/series/test_item.py b/py-polars/tests/unit/series/test_item.py index 8dd715ca43ab..7d8be87ee946 100644 --- a/py-polars/tests/unit/series/test_item.py +++ b/py-polars/tests/unit/series/test_item.py @@ -1,5 +1,7 @@ from __future__ import annotations +import datetime + import pytest import polars as pl @@ -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()