Skip to content

Commit fe34e24

Browse files
authored
Merge pull request #410 from lincc-frameworks/fix_loss_of_idx_name
fix loss of index name with empty dfs
2 parents 464bffa + eeed100 commit fe34e24

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/nested_pandas/nestedframe/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ def from_lists(cls, df, base_columns=None, list_columns=None, name="nested"):
689689
# if the dataframe is empty, just return an empty nested column
690690
# since there are no iterable values to pack
691691
packed_df = NestedFrame().join_nested(df[list_columns], name=name)
692+
packed_df.index.name = df.index.name
692693
else:
693694
# Check that each column has iterable elements
694695
for col in list_columns:

tests/nested_pandas/nestedframe/test_nestedframe.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,6 +2205,17 @@ def test_nestlists_nonunique_index():
22052205
assert nf_repacked.equals(nf)
22062206

22072207

2208+
def test_nestlists_preserve_index_name():
2209+
"""Test that nest_lists preserves the index name with empty dataframes."""
2210+
2211+
# See https://github.com/lincc-frameworks/nested-pandas/issues/409 for context
2212+
nf = generate_data(5, 10, seed=1)
2213+
nf.index.name = "objectid"
2214+
nf = nf.join(nf["nested"].to_lists())
2215+
result = nf.head(0).nest_lists(["t", "flux", "band"], name="nested2").reset_index()
2216+
assert "objectid" in result.columns
2217+
2218+
22082219
def test_delitem_base_and_nested():
22092220
"""Test that __delitem__ works for both base and nested columns."""
22102221
base = NestedFrame(data={"a": [1, 2, 3], "b": [2, 4, 6]}, index=[0, 1, 2])

0 commit comments

Comments
 (0)