From c9c34ae37bb4ce205f098d9beb096cc089dd9ad9 Mon Sep 17 00:00:00 2001 From: Ritchie Vink Date: Fri, 10 Jan 2025 13:36:04 +0100 Subject: [PATCH] fix: Fix serialization of empty DF in LazyFrame (#20658) --- crates/polars-core/src/serde/df.rs | 3 +++ py-polars/tests/unit/test_serde.py | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/crates/polars-core/src/serde/df.rs b/crates/polars-core/src/serde/df.rs index 052f606a76d8..e0fe9053bf19 100644 --- a/crates/polars-core/src/serde/df.rs +++ b/crates/polars-core/src/serde/df.rs @@ -88,6 +88,9 @@ impl DataFrame { }) .collect::>>()?; + if dfs.is_empty() { + return Ok(DataFrame::empty()); + } let mut df = accumulate_dataframes_vertical_unchecked(dfs); // Set custom metadata (fallible) diff --git a/py-polars/tests/unit/test_serde.py b/py-polars/tests/unit/test_serde.py index c1a79025690e..7331089daa94 100644 --- a/py-polars/tests/unit/test_serde.py +++ b/py-polars/tests/unit/test_serde.py @@ -217,3 +217,11 @@ def test_serde_udf() -> None: result = pl.LazyFrame.deserialize(io.BytesIO(lf.serialize())) assert_frame_equal(lf, result) + + +def test_serde_empty_df_lazy_frame() -> None: + lf = pl.LazyFrame() + f = io.BytesIO() + f.write(lf.serialize()) + f.seek(0) + assert pl.LazyFrame.deserialize(f).collect().shape == (0, 0)