-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(python): Handle
DataFrame.extend
extending by itself (#9897)
- Loading branch information
Showing
4 changed files
with
125 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from datetime import datetime | ||
|
||
import pytest | ||
|
||
import polars as pl | ||
from polars.testing import assert_frame_equal | ||
|
||
|
||
def test_extend_various_dtypes() -> None: | ||
with pl.StringCache(): | ||
df1 = pl.DataFrame( | ||
{ | ||
"foo": [1, 2], | ||
"bar": [True, False], | ||
"ham": ["a", "b"], | ||
"cat": ["A", "B"], | ||
"dates": [datetime(2021, 1, 1), datetime(2021, 2, 1)], | ||
}, | ||
schema_overrides={"cat": pl.Categorical}, | ||
) | ||
df2 = pl.DataFrame( | ||
{ | ||
"foo": [3, 4], | ||
"bar": [True, None], | ||
"ham": ["c", "d"], | ||
"cat": ["C", "B"], | ||
"dates": [datetime(2022, 9, 1), datetime(2021, 2, 1)], | ||
}, | ||
schema_overrides={"cat": pl.Categorical}, | ||
) | ||
|
||
df1.extend(df2) | ||
|
||
expected = pl.DataFrame( | ||
{ | ||
"foo": [1, 2, 3, 4], | ||
"bar": [True, False, True, None], | ||
"ham": ["a", "b", "c", "d"], | ||
"cat": ["A", "B", "C", "B"], | ||
"dates": [ | ||
datetime(2021, 1, 1), | ||
datetime(2021, 2, 1), | ||
datetime(2022, 9, 1), | ||
datetime(2021, 2, 1), | ||
], | ||
}, | ||
schema_overrides={"cat": pl.Categorical}, | ||
) | ||
assert_frame_equal(df1, expected) | ||
|
||
|
||
def test_extend_slice_offset_8745() -> None: | ||
df = pl.DataFrame([{"age": 1}, {"age": 2}, {"age": 3}]) | ||
df = df[:-1] | ||
tail = pl.DataFrame([{"age": 8}]) | ||
assert df.extend(tail).to_dict(False) == {"age": [1, 2, 8]} | ||
|
||
|
||
def test_extend_self() -> None: | ||
df = pl.DataFrame({"a": [1, 2], "b": [True, False]}) | ||
|
||
df.extend(df) | ||
|
||
expected = pl.DataFrame({"a": [1, 2, 1, 2], "b": [True, False, True, False]}) | ||
assert_frame_equal(df, expected) | ||
|
||
|
||
def test_extend_column_number_mismatch() -> None: | ||
df1 = pl.DataFrame({"a": [1, 2], "b": [True, False]}) | ||
df2 = df1.drop("a") | ||
|
||
with pytest.raises(pl.ShapeError): | ||
df1.extend(df2) | ||
|
||
|
||
def test_extend_column_name_mismatch() -> None: | ||
df1 = pl.DataFrame({"a": [1, 2], "b": [True, False]}) | ||
df2 = df1.with_columns(pl.col("a").alias("c")) | ||
|
||
with pytest.raises(pl.ShapeError): | ||
df1.extend(df2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters