Skip to content

Commit

Permalink
Merge pull request #128 from lincc-frameworks/empty_query_fix
Browse files Browse the repository at this point in the history
fix for empty query results
  • Loading branch information
dougbrn authored Aug 9, 2024
2 parents 5a6ab4a + 743de7b commit ac40d0f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/nested_pandas/series/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,11 @@ def query_flat(self, query: str) -> pd.Series:
The filtered series.
"""
flat = self.to_flat().query(query)

if len(flat) == 0:
return pd.Series([], dtype=self._series.dtype)
return pd.Series(
[], dtype=self._series.dtype, index=pd.Index([], dtype=flat.index.dtype, name=flat.index.name)
)
return pack_sorted_df_into_struct(flat)

def get_flat_index(self) -> pd.Index:
Expand Down
12 changes: 12 additions & 0 deletions tests/nested_pandas/series/test_accessor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import nested_pandas as npd
import numpy as np
import pandas as pd
import pyarrow as pa
Expand Down Expand Up @@ -487,6 +488,17 @@ def test_query_flat_empty_rows():
assert_series_equal(filtered, desired)


def test_query_flat_with_empty_result():
"""Make sure the index is properly set for empty result cases"""
base = npd.NestedFrame({"a": []}, index=pd.Index([], dtype=np.float64))
nested = npd.NestedFrame({"b": []}, index=pd.Index([], dtype=np.float64))

ndf = base.add_nested(nested, "nested")

res = ndf.nested.nest.query_flat("b > 2")
assert res.index.dtype == np.float64


@pytest.mark.parametrize(
"df",
[
Expand Down

0 comments on commit ac40d0f

Please sign in to comment.