Path: blob/main/py-polars/tests/unit/series/test_append.py
6939 views
import io12import pytest34import polars as pl5from polars.exceptions import SchemaError6from polars.testing import assert_series_equal789def test_append() -> None:10a = pl.Series("a", [1, 2])11b = pl.Series("b", [8, 9, None])1213result = a.append(b)1415expected = pl.Series("a", [1, 2, 8, 9, None])16assert_series_equal(a, expected)17assert_series_equal(result, expected)18assert a.n_chunks() == 2192021def test_append_self_3915() -> None:22a = pl.Series("a", [1, 2])2324a.append(a)2526expected = pl.Series("a", [1, 2, 1, 2])27assert_series_equal(a, expected)28assert a.n_chunks() == 2293031def test_append_bad_input() -> None:32a = pl.Series("a", [1, 2])33b = a.to_frame()3435with pytest.raises(36TypeError,37match="expected `other` .*to be a 'Series'.* not 'DataFrame'",38):39a.append(b) # type: ignore[arg-type]4041with pytest.raises(42TypeError,43match="expected `other` .*to be a 'Series'.* not 'LazyFrame'",44):45a.append(b.lazy()) # type: ignore[arg-type]464748def test_struct_schema_on_append_extend_3452() -> None:49housing1_data = [50{51"city": "Chicago",52"address": "100 Main St",53"price": 250000,54"nbr_bedrooms": 3,55},56{57"city": "New York",58"address": "100 First Ave",59"price": 450000,60"nbr_bedrooms": 2,61},62]6364housing2_data = [65{66"address": "303 Mockingbird Lane",67"city": "Los Angeles",68"nbr_bedrooms": 2,69"price": 450000,70},71{72"address": "404 Moldave Dr",73"city": "Miami Beach",74"nbr_bedrooms": 1,75"price": 250000,76},77]78housing1, housing2 = pl.Series(housing1_data), pl.Series(housing2_data)79with pytest.raises(80SchemaError,81):82housing1.append(housing2)8384with pytest.raises(85SchemaError,86):87housing1.extend(housing2)888990def test_append_mismatching_struct_with_null_22639() -> None:91a = pl.Series([{"x": "foo", "y": "bar"}])92b = pl.Series([{"z": "baz"}])93c = pl.Series([{"z": None}])94with pytest.raises(95SchemaError,96):97a.append(b)98with pytest.raises(99SchemaError,100):101a.append(c)102103assert_series_equal(b.append(c), pl.Series([{"z": "baz"}, {"z": None}]))104105106def test_append_null_series() -> None:107a = pl.Series("a", [1, 2], pl.Int64)108b = pl.Series("b", [None, None], pl.Null)109110result = a.append(b)111112expected = pl.Series("a", [1, 2, None, None], pl.Int64)113assert_series_equal(a, expected)114assert_series_equal(result, expected)115assert a.n_chunks() == 2116117118def test_append_enum_22764() -> None:119f = io.BytesIO()120g = io.BytesIO()121pl.DataFrame({"someletter": ["A", "B"]}).write_csv(f)122123schema = pl.Schema(124{125"someletter": pl.Enum(["A", "B"]),126}127)128pl.scan_csv(f, schema=schema).sink_parquet(g)129130131