Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/dataframe/test_vstack.py
6939 views
1
import pytest
2
3
import polars as pl
4
from polars.exceptions import SchemaError, ShapeError
5
from polars.testing import assert_frame_equal
6
7
8
@pytest.fixture
9
def df1() -> pl.DataFrame:
10
return pl.DataFrame({"foo": [1, 2], "bar": [6, 7], "ham": ["a", "b"]})
11
12
13
@pytest.fixture
14
def df2() -> pl.DataFrame:
15
return pl.DataFrame({"foo": [3, 4], "bar": [8, 9], "ham": ["c", "d"]})
16
17
18
def test_vstack(df1: pl.DataFrame, df2: pl.DataFrame) -> None:
19
result = df1.vstack(df2)
20
expected = pl.DataFrame(
21
{"foo": [1, 2, 3, 4], "bar": [6, 7, 8, 9], "ham": ["a", "b", "c", "d"]}
22
)
23
assert_frame_equal(result, expected)
24
25
26
def test_vstack_in_place(df1: pl.DataFrame, df2: pl.DataFrame) -> None:
27
df1.vstack(df2, in_place=True)
28
expected = pl.DataFrame(
29
{"foo": [1, 2, 3, 4], "bar": [6, 7, 8, 9], "ham": ["a", "b", "c", "d"]}
30
)
31
assert_frame_equal(df1, expected)
32
33
34
def test_vstack_self(df1: pl.DataFrame) -> None:
35
result = df1.vstack(df1)
36
expected = pl.DataFrame(
37
{"foo": [1, 2, 1, 2], "bar": [6, 7, 6, 7], "ham": ["a", "b", "a", "b"]}
38
)
39
assert_frame_equal(result, expected)
40
41
42
def test_vstack_self_in_place(df1: pl.DataFrame) -> None:
43
df1.vstack(df1, in_place=True)
44
expected = pl.DataFrame(
45
{"foo": [1, 2, 1, 2], "bar": [6, 7, 6, 7], "ham": ["a", "b", "a", "b"]}
46
)
47
assert_frame_equal(df1, expected)
48
49
50
def test_vstack_column_number_mismatch(df1: pl.DataFrame) -> None:
51
df2 = df1.drop("ham")
52
53
with pytest.raises(ShapeError):
54
df1.vstack(df2)
55
56
57
def test_vstack_column_name_mismatch(df1: pl.DataFrame) -> None:
58
df2 = df1.with_columns(pl.col("foo").alias("oof"))
59
60
with pytest.raises(ShapeError):
61
df1.vstack(df2)
62
63
64
def test_vstack_with_null_column() -> None:
65
df1 = pl.DataFrame({"x": [3.5]}, schema={"x": pl.Float64})
66
df2 = pl.DataFrame({"x": [None]}, schema={"x": pl.Null})
67
68
result = df1.vstack(df2)
69
expected = pl.DataFrame({"x": [3.5, None]}, schema={"x": pl.Float64})
70
71
assert_frame_equal(result, expected)
72
73
with pytest.raises(SchemaError):
74
df2.vstack(df1)
75
76
77
def test_vstack_with_nested_nulls() -> None:
78
a = pl.DataFrame({"x": [[3.5]]}, schema={"x": pl.List(pl.Float32)})
79
b = pl.DataFrame({"x": [[None]]}, schema={"x": pl.List(pl.Null)})
80
81
out = a.vstack(b)
82
expected = pl.DataFrame({"x": [[3.5], [None]]}, schema={"x": pl.List(pl.Float32)})
83
assert_frame_equal(out, expected)
84
85
86
def test_vstack_bad_input_type() -> None:
87
a = pl.DataFrame({"x": [1, 2, 3]})
88
b = pl.DataFrame({"x": [4, 5, 6]})
89
90
with pytest.raises(
91
TypeError,
92
match="expected `other` .*to be a 'DataFrame'.* not 'Series'",
93
):
94
a.vstack(pl.Series(b)) # type: ignore[arg-type]
95
96
with pytest.raises(
97
TypeError,
98
match="expected `other` .*to be a 'DataFrame'.* not 'LazyFrame'",
99
):
100
a.vstack(b.lazy()) # type: ignore[arg-type]
101
102
class DummyDataFrameSubclass(pl.DataFrame):
103
pass
104
105
b = DummyDataFrameSubclass(b)
106
107
a = a.vstack(b)
108
109