Path: blob/main/py-polars/tests/unit/dataframe/test_extend.py
6939 views
from datetime import datetime12import pytest34import polars as pl5from polars.exceptions import ShapeError6from polars.testing import assert_frame_equal789def test_extend_various_dtypes() -> None:10df1 = pl.DataFrame(11{12"foo": [1, 2],13"bar": [True, False],14"ham": ["a", "b"],15"cat": ["A", "B"],16"dates": [datetime(2021, 1, 1), datetime(2021, 2, 1)],17},18schema_overrides={"cat": pl.Categorical},19)20df2 = pl.DataFrame(21{22"foo": [3, 4],23"bar": [True, None],24"ham": ["c", "d"],25"cat": ["C", "B"],26"dates": [datetime(2022, 9, 1), datetime(2021, 2, 1)],27},28schema_overrides={"cat": pl.Categorical},29)3031df1.extend(df2)3233expected = pl.DataFrame(34{35"foo": [1, 2, 3, 4],36"bar": [True, False, True, None],37"ham": ["a", "b", "c", "d"],38"cat": ["A", "B", "C", "B"],39"dates": [40datetime(2021, 1, 1),41datetime(2021, 2, 1),42datetime(2022, 9, 1),43datetime(2021, 2, 1),44],45},46schema_overrides={"cat": pl.Categorical},47)48assert_frame_equal(df1, expected)495051def test_extend_slice_offset_8745() -> None:52df = pl.DataFrame([{"age": 1}, {"age": 2}, {"age": 3}])5354df = df[:-1]55tail = pl.DataFrame([{"age": 8}])56result = df.extend(tail)5758expected = pl.DataFrame({"age": [1, 2, 8]})59assert_frame_equal(result, expected)606162def test_extend_self() -> None:63df = pl.DataFrame({"a": [1, 2], "b": [True, False]})6465df.extend(df)6667expected = pl.DataFrame({"a": [1, 2, 1, 2], "b": [True, False, True, False]})68assert_frame_equal(df, expected)697071def test_extend_column_number_mismatch() -> None:72df1 = pl.DataFrame({"a": [1, 2], "b": [True, False]})73df2 = df1.drop("a")7475with pytest.raises(ShapeError):76df1.extend(df2)777879def test_extend_column_name_mismatch() -> None:80df1 = pl.DataFrame({"a": [1, 2], "b": [True, False]})81df2 = df1.with_columns(pl.col("a").alias("c"))8283with pytest.raises(ShapeError):84df1.extend(df2)858687def test_initialize_df_18736() -> None:88# Completely empty initialization89df = pl.DataFrame()90s_0 = pl.Series([])91s_1 = pl.Series([None])92s_2 = pl.Series([None, None])93assert df.with_columns(s_0).shape == (0, 1)94assert df.with_columns(s_1).shape == (1, 1)95assert df.with_columns(s_2).shape == (2, 1)969798def test_extend_bad_input_type() -> None:99a = pl.DataFrame({"x": [1, 2, 3]})100b = pl.DataFrame({"x": [4, 5, 6]})101102with pytest.raises(103TypeError,104match="expected `other` .*to be a 'DataFrame'.* not 'Series'",105):106a.extend(pl.Series(b)) # type: ignore[arg-type]107108with pytest.raises(109TypeError,110match="expected `other` .*to be a 'DataFrame'.* not 'LazyFrame'",111):112a.extend(b.lazy()) # type: ignore[arg-type]113114class DummyDataFrameSubclass(pl.DataFrame):115pass116117b = DummyDataFrameSubclass({"x": [4, 5, 6]})118119a.extend(b)120121122