-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
REGR: fix regression in scalar setitem with setting a length-1 array-like #48057
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
Merged
mroeschke
merged 2 commits into
pandas-dev:main
from
jorisvandenbossche:regr-setitem-scalar-with-nested
Aug 19, 2022
+98
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
""" test fancy indexing & misc """ | ||
|
||
import array | ||
from datetime import datetime | ||
import re | ||
import weakref | ||
|
@@ -1019,3 +1020,95 @@ def test_ser_list_indexer_exceeds_dimensions(indexer_li): | |
res = indexer_li(ser)[[0, 0]] | ||
exp = Series([10, 10], index=Index([0, 0])) | ||
tm.assert_series_equal(res, exp) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"value", [(0, 1), [0, 1], np.array([0, 1]), array.array("b", [0, 1])] | ||
) | ||
def test_scalar_setitem_with_nested_value(value): | ||
# For numeric data, we try to unpack and thus raise for mismatching length | ||
df = DataFrame({"A": [1, 2, 3]}) | ||
msg = "|".join( | ||
[ | ||
"Must have equal len keys and value", | ||
"setting an array element with a sequence", | ||
] | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
df.loc[0, "B"] = value | ||
|
||
# TODO For object dtype this happens as well, but should we rather preserve | ||
# the nested data and set as such? | ||
df = DataFrame({"A": [1, 2, 3], "B": np.array([1, "a", "b"], dtype=object)}) | ||
with pytest.raises(ValueError, match="Must have equal len keys and value"): | ||
df.loc[0, "B"] = value | ||
# if isinstance(value, np.ndarray): | ||
# assert (df.loc[0, "B"] == value).all() | ||
# else: | ||
# assert df.loc[0, "B"] == value | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"value", [(0, 1), [0, 1], np.array([0, 1]), array.array("b", [0, 1])] | ||
) | ||
def test_scalar_setitem_series_with_nested_value(value, indexer_sli): | ||
# For numeric data, we try to unpack and thus raise for mismatching length | ||
ser = Series([1, 2, 3]) | ||
with pytest.raises(ValueError, match="setting an array element with a sequence"): | ||
indexer_sli(ser)[0] = value | ||
|
||
# but for object dtype we preserve the nested data and set as such | ||
ser = Series([1, "a", "b"], dtype=object) | ||
indexer_sli(ser)[0] = value | ||
if isinstance(value, np.ndarray): | ||
assert (ser.loc[0] == value).all() | ||
else: | ||
assert ser.loc[0] == value | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"value", [(0.0,), [0.0], np.array([0.0]), array.array("d", [0.0])] | ||
) | ||
def test_scalar_setitem_with_nested_value_length1(value): | ||
# https://github.com/pandas-dev/pandas/issues/46268 | ||
|
||
# For numeric data, assigning length-1 array to scalar position gets unpacked | ||
df = DataFrame({"A": [1, 2, 3]}) | ||
df.loc[0, "B"] = value | ||
expected = DataFrame({"A": [1, 2, 3], "B": [0.0, np.nan, np.nan]}) | ||
tm.assert_frame_equal(df, expected) | ||
|
||
# but for object dtype we preserve the nested data | ||
df = DataFrame({"A": [1, 2, 3], "B": np.array([1, "a", "b"], dtype=object)}) | ||
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. If we do the above, this is correct I think |
||
df.loc[0, "B"] = value | ||
if isinstance(value, np.ndarray): | ||
assert (df.loc[0, "B"] == value).all() | ||
else: | ||
assert df.loc[0, "B"] == value | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"value", [(0.0,), [0.0], np.array([0.0]), array.array("d", [0.0])] | ||
) | ||
def test_scalar_setitem_series_with_nested_value_length1(value, indexer_sli): | ||
# For numeric data, assigning length-1 array to scalar position gets unpacked | ||
# TODO this only happens in case of ndarray, should we make this consistent | ||
# for all list-likes? (as happens for DataFrame.(i)loc, see test above) | ||
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. Yes, I think this should unpack for everything. The DataFrame case already does |
||
ser = Series([1.0, 2.0, 3.0]) | ||
if isinstance(value, np.ndarray): | ||
indexer_sli(ser)[0] = value | ||
expected = Series([0.0, 2.0, 3.0]) | ||
tm.assert_series_equal(ser, expected) | ||
else: | ||
with pytest.raises( | ||
ValueError, match="setting an array element with a sequence" | ||
): | ||
indexer_sli(ser)[0] = value | ||
|
||
# but for object dtype we preserve the nested data | ||
ser = Series([1, "a", "b"], dtype=object) | ||
indexer_sli(ser)[0] = value | ||
if isinstance(value, np.ndarray): | ||
assert (ser.loc[0] == value).all() | ||
else: | ||
assert ser.loc[0] == value |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think this sound good