From d4eb8fc062efc27e44db07b9fa7f0b302acd9534 Mon Sep 17 00:00:00 2001 From: Konstantin Malanchev Date: Wed, 29 May 2024 10:47:42 -0400 Subject: [PATCH] Fix ndf.add_nested() for empty frames --- src/nested_pandas/nestedframe/core.py | 4 +++- .../nested_pandas/nestedframe/test_nestedframe.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/nested_pandas/nestedframe/core.py b/src/nested_pandas/nestedframe/core.py index 87e059c..416ca8d 100644 --- a/src/nested_pandas/nestedframe/core.py +++ b/src/nested_pandas/nestedframe/core.py @@ -99,7 +99,9 @@ def add_nested( # Add sources to objects packed = packer.pack(obj, name=name, dtype=dtype) label = packed.name - return self.assign(**{f"{label}": packed}) + new_df = self.copy() + new_df[label] = packed + return new_df def _split_query(self, expr) -> dict: """Splits a pandas query into multiple subqueries for nested and base layers""" diff --git a/tests/nested_pandas/nestedframe/test_nestedframe.py b/tests/nested_pandas/nestedframe/test_nestedframe.py index 4bfd710..b9afca9 100644 --- a/tests/nested_pandas/nestedframe/test_nestedframe.py +++ b/tests/nested_pandas/nestedframe/test_nestedframe.py @@ -1,5 +1,6 @@ import numpy as np import pandas as pd +import pyarrow as pa import pytest from nested_pandas import NestedFrame from pandas.testing import assert_frame_equal @@ -130,6 +131,19 @@ def test_add_nested_with_series_and_mismatched_index(): assert pd.isna(base.loc[1]["nested"]) +def test_add_nested_for_empty_df(): + """Test that .add_nested() works for empty frame and empty input""" + base = NestedFrame(data={"a": [], "b": []}, index=[]) + nested = pd.DataFrame(data={"c": []}, index=[]) + new_base = base.add_nested(nested, "nested") + + # Check original frame is unchanged + assert_frame_equal(base, NestedFrame(data={"a": [], "b": []}, index=[])) + + assert "nested" in new_base.columns + assert_frame_equal(new_base.nested.nest.to_flat(), nested.astype(pd.ArrowDtype(pa.float64()))) + + def test_query(): """Test that NestedFrame.query handles nested queries correctly"""