diff --git a/src/nested_pandas/series/accessor.py b/src/nested_pandas/series/accessor.py index 30b5175..c4dbd5f 100644 --- a/src/nested_pandas/series/accessor.py +++ b/src/nested_pandas/series/accessor.py @@ -95,10 +95,11 @@ def to_flat(self, fields: list[str] | None = None) -> pd.DataFrame: index = np.repeat(self._series.index.values, np.diff(list_array.offsets)) flat_series[field] = pd.Series( list_array.flatten(), - index=index, + index=pd.Series(index, name=self._series.index.name), name=field, copy=False, ) + return pd.DataFrame(flat_series) @property diff --git a/tests/nested_pandas/series/test_accessor.py b/tests/nested_pandas/series/test_accessor.py index 04164df..23537e2 100644 --- a/tests/nested_pandas/series/test_accessor.py +++ b/tests/nested_pandas/series/test_accessor.py @@ -87,7 +87,10 @@ def test_to_flat(): ], names=["a", "b"], ) - series = pd.Series(struct_array, dtype=NestedDtype(struct_array.type), index=[0, 1]) + + series = pd.Series( + struct_array, dtype=NestedDtype(struct_array.type), index=pd.Series([0, 1], name="idx") + ) flat = series.nest.to_flat() @@ -106,10 +109,12 @@ def test_to_flat(): copy=False, ), }, + index=pd.Index([0, 0, 0, 1, 1, 1], name="idx"), ) assert_array_equal(flat.dtypes, desired.dtypes) assert_array_equal(flat.index, desired.index) + assert flat.index.name == desired.index.name for column in flat.columns: assert_array_equal(flat[column], desired[column])