Skip to content

Commit 82846c3

Browse files
authored
Merge pull request #352 from lincc-frameworks/fix_348
fix add_nested out dtype for empty dataframes
2 parents 42eb06e + 35a58f8 commit 82846c3

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/nested_pandas/nestedframe/core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,12 @@ def add_nested(
408408
packed = pack(obj, name=name, on=on, dtype=dtype)
409409
new_df = self.copy()
410410
res = new_df.join(packed, how=how, on=on)
411+
412+
# In some cases join returns a DataFrame, so convert back to NestedFrame
413+
# For example, with empty dataframes
414+
if not isinstance(res, NestedFrame):
415+
res = NestedFrame(res)
416+
411417
return res
412418

413419
def nest_lists(self, columns: list[str], name: str) -> NestedFrame:

tests/nested_pandas/nestedframe/test_nestedframe.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,6 +2201,16 @@ def test_issue294():
22012201
nf["nested.mag"] = -2.5 * np.log10(nf["nested.flux"])
22022202

22032203

2204+
def test_issue348():
2205+
"""https://github.com/lincc-frameworks/nested-pandas/issues/348"""
2206+
new = NestedFrame.from_flat(
2207+
pd.DataFrame({"x": np.array([], dtype=float)}),
2208+
base_columns=[],
2209+
name="new",
2210+
)
2211+
assert isinstance(new, NestedFrame)
2212+
2213+
22042214
def test_issue350():
22052215
"""https://github.com/lincc-frameworks/nested-pandas/issues/350"""
22062216
nf = generate_data(3, 2)

0 commit comments

Comments
 (0)