Skip to content

BUG: DataFrame bugs in ndarray assignments #53367

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

Conversation

determ1ne
Copy link

@mroeschke mroeschke added the Indexing Related to indexing on series/frames, not to indexes themselves label May 24, 2023
@determ1ne determ1ne changed the title Fix DataFrame bugs in ndarray assignments BUG: DataFrame bugs in ndarray assignments May 26, 2023
@determ1ne
Copy link
Author

Any updates on this?

@github-actions
Copy link
Contributor

github-actions bot commented Jul 5, 2023

This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label Jul 5, 2023
@determ1ne
Copy link
Author

@mroeschke

@@ -432,6 +432,8 @@ Groupby/resample/rolling
Reshaping
^^^^^^^^^
- Bug in :func:`crosstab` when ``dropna=False`` would not keep ``np.nan`` in the result (:issue:`10772`)
- Bug in :meth:`BlockManager.insert` allows assignments for multiple-dimension ndarray into one column (:issue:`53366`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BlockManager isn't user-facing. can you write this in terms of a DataFrame or Series method?

elif (
isinstance(value, np.ndarray) and value.ndim > 1 and self.columns.is_unique
):
# TODO: a check for MultiIndex should be added
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the check on the next line not take care of this TODO?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the check on the next line not take care of this TODO?

No, the check here is just placeholder for further improvements, and the real check should be inserted between "isinstance" line and "_set_item" line. I did not figure out what and how checks for MultiIndex should be added so I skipped this. But I think the current PR is a mitigation covering and fixing frequently used scenes.

return
# squeeze 2d ndarray to 1d if possible
# this keeps the backward compatibility
if np.ndim(value) == 2 and (1 in np.shape(value)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use value.ndim and values.shape instead of the numpy methods

@@ -3966,6 +3966,24 @@ def __setitem__(self, key, value):
):
# Column to set is duplicated
self._setitem_array([key], value)
elif (
isinstance(value, np.ndarray) and value.ndim > 1 and self.columns.is_unique
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it matter what 'key' is here?

@@ -283,6 +283,11 @@ def test_constructor_cast_failure(self):
with pytest.raises(ValueError, match=msg):
df["test"] = np.ones((4, 2))

# this is not ok
msg = "Expected a 1D array, got an array with shape \\(4, 2, 3\\)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is for constructor tests. can you put a dedicated test in tests/frame/indexing/test_setitem.py

@@ -1401,7 +1401,7 @@ def insert(self, loc: int, item: Hashable, value: ArrayLike, refs=None) -> None:
# insert to the axis; this could possibly raise a TypeError
new_axis = self.items.insert(loc, item)

if value.ndim == 2:
if value.ndim >= 2:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this needed?

A ndarray where dim >= 2 will bypass the check here, but still raise an Exception when accessing the DataFrame. For an example for this bug, see #53366

@mroeschke
Copy link
Member

Thanks for the pull request, but it appears to have gone stale. If interested in continuing, please merge in the main branch, address any review comments and/or failing tests, and we can reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: DataFrame.assign accepts 2d arrays for single column value
3 participants