Skip to content

Commit

Permalink
Merge pull request #147 from lincc-frameworks/from_flat_df
Browse files Browse the repository at this point in the history
fix from_flat for pd input
  • Loading branch information
dougbrn authored Sep 23, 2024
2 parents 6101d39 + cd7ca7e commit aa34af1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/nested_pandas/nestedframe/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ def from_flat(cls, df, base_columns, nested_columns=None, index=None, name="nest
# drop duplicates on index
out_df = df[base_columns][~df.index.duplicated(keep="first")]

# Convert df to NestedFrame if needed
if not isinstance(out_df, NestedFrame):
out_df = NestedFrame(out_df)

# add nested
if nested_columns is None:
nested_columns = [col for col in df.columns if col not in base_columns]
Expand Down
18 changes: 13 additions & 5 deletions tests/nested_pandas/nestedframe/test_nestedframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,21 @@ def test_add_nested_for_empty_df():
assert_frame_equal(new_base.nested.nest.to_flat(), nested.astype(pd.ArrowDtype(pa.float64())))


@pytest.mark.parametrize("pandas", [False, True])
@pytest.mark.parametrize("index", [None, "a", "c"])
def test_from_flat(index):
def test_from_flat(index, pandas):
"""Test the NestedFrame.from_flat functionality"""
nf = NestedFrame(
{"a": [1, 1, 1, 2, 2], "b": [2, 2, 2, 4, 4], "c": [1, 2, 3, 4, 5], "d": [2, 4, 6, 8, 10]},
index=[0, 0, 0, 1, 1],
)

if pandas:
nf = pd.DataFrame(
{"a": [1, 1, 1, 2, 2], "b": [2, 2, 2, 4, 4], "c": [1, 2, 3, 4, 5], "d": [2, 4, 6, 8, 10]},
index=[0, 0, 0, 1, 1],
)
else:
nf = NestedFrame(
{"a": [1, 1, 1, 2, 2], "b": [2, 2, 2, 4, 4], "c": [1, 2, 3, 4, 5], "d": [2, 4, 6, 8, 10]},
index=[0, 0, 0, 1, 1],
)

out_nf = NestedFrame.from_flat(nf, base_columns=["a", "b"], index=index, name="new_nested")

Expand Down

0 comments on commit aa34af1

Please sign in to comment.