Skip to content

Commit 79e0ed5

Browse files
committed
Fix to_flatten_inner for missing values
1 parent f27877f commit 79e0ed5

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/nested_pandas/series/accessor.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,12 @@ def to_flatten_inner(self, field: str) -> pd.Series:
653653
# Assign back the "outer" ordinal index and pack on it
654654
result = pack_flat(inner_flatten, name=self._series.name)
655655

656+
# Some indexes may be missed if the original series had some NULLs
657+
if len(result) < len(series):
658+
nulls = pd.Series(None, index=series.index, dtype=result.dtype)
659+
nulls[result.index] = result
660+
result = nulls
661+
656662
# And put back the original index
657663
result.index = self._series.index
658664
return result

tests/nested_pandas/series/test_accessor.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pandas as pd
44
import pyarrow as pa
55
import pytest
6-
from nested_pandas import NestedDtype, NestedFrame
6+
from nested_pandas import NestedDtype, NestedFrame, read_parquet
77
from nested_pandas.datasets import generate_data
88
from nested_pandas.series.ext_array import NestedExtensionArray
99
from nested_pandas.series.packer import pack_flat, pack_seq
@@ -1100,7 +1100,24 @@ def test_to_flatten_inner():
11001100
assert_frame_equal(actual.nest.to_flat(), desired.nest.to_flat(), check_like=True)
11011101

11021102

1103-
def test_to_flatten_outer_wrong_field():
1103+
def test_to_flatten_inner_empty_inner():
1104+
"""Test .nest.to_flatten_inner for the case when inner frames are empty"""
1105+
nf = generate_data(10, 2)
1106+
nf["nested"][2:4] = [pd.DataFrame({"t": [], "flux": [], "band": []})] * 2
1107+
nf = nf.assign(id=np.repeat(np.r_[0:5], 2))
1108+
nf = nf.rename(columns={"nested": "inner"})
1109+
nnf = NestedFrame.from_flat(nf, base_columns=[], on="id", name="outer")
1110+
1111+
_actual = nnf["outer"].nest.to_flatten_inner("inner")
1112+
1113+
1114+
def test_to_flatten_inner_none_nested():
1115+
"""Test .nest.to_flatten_inner with vsx-x-ztfdr22_lc-m31.parquet file"""
1116+
nnf = read_parquet("tests/test_data/vsx-x-ztfdr22_lc-m31.parquet")
1117+
_actual = nnf["ztf"].nest.to_flatten_inner("lc")
1118+
1119+
1120+
def test_to_flatten_inner_wrong_field():
11041121
"""Test an exception is raised when .nest.to_flatten_inner() called for a wrong field."""
11051122
nf = generate_data(10, 2)
11061123
with pytest.raises(ValueError):
9.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)