From 2886bf284aa524e8446219ed2d1ec473ec75e62c Mon Sep 17 00:00:00 2001 From: Doug Branton Date: Fri, 2 Aug 2024 15:33:17 -0700 Subject: [PATCH 1/3] fix for empty query results --- src/nested_pandas/series/accessor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nested_pandas/series/accessor.py b/src/nested_pandas/series/accessor.py index f6253bd..dd49e94 100644 --- a/src/nested_pandas/series/accessor.py +++ b/src/nested_pandas/series/accessor.py @@ -223,8 +223,8 @@ 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) + #if len(flat) == 0: + # return pd.Series([], dtype=self._series.dtype) return pack_sorted_df_into_struct(flat) def get_flat_index(self) -> pd.Index: From 9838d9a5a5aa65167e71a0329132581fa5b52655 Mon Sep 17 00:00:00 2001 From: Doug Branton Date: Fri, 9 Aug 2024 11:29:46 -0700 Subject: [PATCH 2/3] reintroduce the len=0 series catch, with proper index dtype propagation --- src/nested_pandas/series/accessor.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/nested_pandas/series/accessor.py b/src/nested_pandas/series/accessor.py index dd49e94..b7936bb 100644 --- a/src/nested_pandas/series/accessor.py +++ b/src/nested_pandas/series/accessor.py @@ -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) + + if len(flat) == 0: + 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: From 743de7b6bf3f4f0ccb4e8154b6b08b4af865201d Mon Sep 17 00:00:00 2001 From: Doug Branton Date: Fri, 9 Aug 2024 12:09:17 -0700 Subject: [PATCH 3/3] add accessor test for query_flat index dtype --- tests/nested_pandas/series/test_accessor.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/nested_pandas/series/test_accessor.py b/tests/nested_pandas/series/test_accessor.py index a4ac4db..b63c879 100644 --- a/tests/nested_pandas/series/test_accessor.py +++ b/tests/nested_pandas/series/test_accessor.py @@ -1,3 +1,4 @@ +import nested_pandas as npd import numpy as np import pandas as pd import pyarrow as pa @@ -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", [