Skip to content

Commit

Permalink
Merge pull request #105 from lincc-frameworks/dropna_layer_as_base
Browse files Browse the repository at this point in the history
allow dropna on nested columns as base columns
  • Loading branch information
dougbrn authored Jun 6, 2024
2 parents 6152580 + de46a67 commit d9c2e6e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/nested_pandas/nestedframe/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ def _resolve_dropna_target(self, on_nested, subset):
"""resolves the target layer for a given set of dropna kwargs"""

nested_cols = self.nested_columns
columns = self.columns

# first check the subset kwarg input
subset_target = []
Expand All @@ -210,13 +209,15 @@ def _resolve_dropna_target(self, on_nested, subset):
subset = [subset]

for col in subset:
col = col.split(".")[0]
if col in nested_cols:
subset_target.append(col)
elif col in columns:
# Without a ".", always assume base layer
if "." not in col:
subset_target.append("base")
else:
raise ValueError(f"Column name {col} not found in any base or nested columns")
layer, col = col.split(".")
if layer in nested_cols:
subset_target.append(layer)
else:
raise ValueError(f"layer '{layer}' not found in the base columns")

# Check for 1 target
subset_target = np.unique(subset_target)
Expand Down
10 changes: 10 additions & 0 deletions tests/nested_pandas/nestedframe/test_nestedframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pyarrow as pa
import pytest
from nested_pandas import NestedFrame
from nested_pandas.datasets import generate_data
from pandas.testing import assert_frame_equal


Expand Down Expand Up @@ -203,6 +204,15 @@ def test_dropna():
assert len(dn_hierarchical["nested"].nest.to_flat() == 8)


def test_dropna_layer_as_base_column():
"""Test that a nested column still works as a top level column for dropna"""
nf = generate_data(10, 100, seed=1).query("nested.t>19.75")
nf = nf.dropna(subset=["nested"])

# make sure rows have been dropped as expected
assert len(nf) == 6


def test_dropna_inplace_base():
"""Test in-place behavior of dropna"""

Expand Down

0 comments on commit d9c2e6e

Please sign in to comment.