Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate index name in to_flat #27

Merged
merged 5 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/nested_pandas/series/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ def to_flat(self, fields: list[str] | None = None) -> pd.DataFrame:
name=field,
copy=False,
)
return pd.DataFrame(flat_series)
# Note that index should now not be none since we iterated over at least one field
return pd.DataFrame(flat_series, index=pd.Series(index, name=self._series.index.name))
wilsonbb marked this conversation as resolved.
Show resolved Hide resolved

@property
def flat_length(self) -> int:
Expand Down
7 changes: 6 additions & 1 deletion tests/nested_pandas/series/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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])
Expand Down