Skip to content

Commit

Permalink
Unit test for .nest["field"] = pd.Series()
Browse files Browse the repository at this point in the history
  • Loading branch information
hombit committed May 9, 2024
1 parent 0903a96 commit 4375c59
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/nested_pandas/series/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,6 @@ def __setitem__(self, key: str, value: ArrayLike) -> None:
return

if isinstance(value, pd.Series) and not self.get_flat_index().equals(value.index):
print(self._series.index)
print(value.index)
raise ValueError("Cannot set field with a Series of different index")

pa_array = pa.array(value, from_pandas=True)
Expand Down
35 changes: 35 additions & 0 deletions tests/nested_pandas/series/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,41 @@ def test___setitem__():
)


def test___setitem___with_series_with_index():
"""Test that the .nest["field"] = pd.Series(...) works for a single field."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), np.array([1.0, 2.0, 1.0])]),
pa.array([-np.array([4.0, 5.0, 6.0]), -np.array([3.0, 4.0, 5.0])]),
],
names=["a", "b"],
)
series = pd.Series(struct_array, dtype=NestedDtype(struct_array.type), index=[0, 1])

flat_series = pd.Series(
data=["a", "b", "c", "d", "e", "f"],
index=[0, 0, 0, 1, 1, 1],
name="a",
dtype=pd.ArrowDtype(pa.string()),
)

series.nest["a"] = flat_series

assert_series_equal(
series.nest["a"],
flat_series,
)
assert_series_equal(
series.nest.get_list_series("a"),
pd.Series(
data=[np.array(["a", "b", "c"]), np.array(["d", "e", "f"])],
dtype=pd.ArrowDtype(pa.list_(pa.string())),
index=[0, 1],
name="a",
),
)


def test___setitem___raises_for_wrong_length():
"""Test that the .nest["field"] = ... raises for a wrong length."""
struct_array = pa.StructArray.from_arrays(
Expand Down

0 comments on commit 4375c59

Please sign in to comment.