-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from all commits
3e6303f
6812b9d
d3f1f69
b821bd1
a4d454c
c4d4ee9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does it matter what 'key' is here? |
||
): | ||
# TODO: a check for MultiIndex should be added | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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. |
||
if isinstance(self.columns, MultiIndex): | ||
self._set_item(key, value) | ||
return | ||
# squeeze 2d ndarray to 1d if possible | ||
# this keeps the backward compatibility | ||
if np.ndim(value) == 2 and (1 in np.shape(value)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
# if value is np.matrix, convert to np.ndarray | ||
value = np.asarray(value).flatten() | ||
self._set_item(key, value) | ||
else: | ||
# avoid assign non 1d array to column | ||
raise ValueError( | ||
f"Expected a 1D array, got an array with shape {value.shape}" | ||
) | ||
else: | ||
# set column | ||
self._set_item(key, value) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 |
||
value = value.T | ||
if len(value) > 1: | ||
raise ValueError( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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\\)" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
with pytest.raises(ValueError, match=msg): | ||
df["test"] = np.ones((4, 2, 3)) | ||
|
||
# this is ok | ||
df["foo2"] = np.ones((4, 2)).tolist() | ||
|
||
|
There was a problem hiding this comment.
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?