Skip to content

Fix valerror dataframe array #59500

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

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6038,7 +6038,12 @@ def __finalize__(self, other, method: str | None = None, **kwargs) -> Self:
if all(bool(obj.attrs) for obj in objs):
# all concatenate arguments have non-empty attrs
attrs = objs[0].attrs
have_same_attrs = all(obj.attrs == attrs for obj in objs[1:])
have_same_attrs = all(
(obj.attrs == attrs).all()
if isinstance(obj.attrs, np.ndarray)
else obj.attrs == attrs
for obj in objs[1:]
)
if have_same_attrs:
self.attrs = deepcopy(attrs)

Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/generic/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
(pd.Series, ([0, 0],), operator.methodcaller("notna")),
(pd.Series, ([0, 0],), operator.methodcaller("notnull")),
(pd.Series, ([1],), operator.methodcaller("add", pd.Series([1]))),
(pd.Series, ([0],), operator.methodcaller("concat")),
# TODO: mul, div, etc.
(
pd.Series,
Expand Down Expand Up @@ -710,3 +711,15 @@ def test_finalize_frame_series_name():
df = pd.DataFrame({"name": [1, 2]})
result = pd.Series([1, 2]).__finalize__(df)
assert result.name is None


def test_finalize_attrs_ndarray():
# create two separate df's as single series as input
df1 = pd.DataFrame({"A": [1, 2, 3]})
df2 = pd.DataFrame({"A": [4, 5, 6]})

df1.attrs["array_attr"] = np.array([1, 2, 3])
df2.attrs["array_attr"] = np.array([1, 2, 3])

result = df1.__finalize__(df2, method="concat")
assert (result.attrs["array_attr"] == np.array([1, 2, 3])).all()
Loading