|
1 | 1 | """ test fancy indexing & misc """
|
2 | 2 |
|
| 3 | +import array |
3 | 4 | from datetime import datetime
|
4 | 5 | import re
|
5 | 6 | import weakref
|
@@ -985,3 +986,95 @@ def test_extension_array_cross_section_converts():
|
985 | 986 |
|
986 | 987 | result = df.iloc[0]
|
987 | 988 | tm.assert_series_equal(result, expected)
|
| 989 | + |
| 990 | + |
| 991 | +@pytest.mark.parametrize( |
| 992 | + "value", [(0, 1), [0, 1], np.array([0, 1]), array.array("b", [0, 1])] |
| 993 | +) |
| 994 | +def test_scalar_setitem_with_nested_value(value): |
| 995 | + # For numeric data, we try to unpack and thus raise for mismatching length |
| 996 | + df = DataFrame({"A": [1, 2, 3]}) |
| 997 | + msg = "|".join( |
| 998 | + [ |
| 999 | + "Must have equal len keys and value", |
| 1000 | + "setting an array element with a sequence", |
| 1001 | + ] |
| 1002 | + ) |
| 1003 | + with pytest.raises(ValueError, match=msg): |
| 1004 | + df.loc[0, "B"] = value |
| 1005 | + |
| 1006 | + # TODO For object dtype this happens as well, but should we rather preserve |
| 1007 | + # the nested data and set as such? |
| 1008 | + df = DataFrame({"A": [1, 2, 3], "B": np.array([1, "a", "b"], dtype=object)}) |
| 1009 | + with pytest.raises(ValueError, match="Must have equal len keys and value"): |
| 1010 | + df.loc[0, "B"] = value |
| 1011 | + # if isinstance(value, np.ndarray): |
| 1012 | + # assert (df.loc[0, "B"] == value).all() |
| 1013 | + # else: |
| 1014 | + # assert df.loc[0, "B"] == value |
| 1015 | + |
| 1016 | + |
| 1017 | +@pytest.mark.parametrize( |
| 1018 | + "value", [(0, 1), [0, 1], np.array([0, 1]), array.array("b", [0, 1])] |
| 1019 | +) |
| 1020 | +def test_scalar_setitem_series_with_nested_value(value, indexer_sli): |
| 1021 | + # For numeric data, we try to unpack and thus raise for mismatching length |
| 1022 | + ser = Series([1, 2, 3]) |
| 1023 | + with pytest.raises(ValueError, match="setting an array element with a sequence"): |
| 1024 | + indexer_sli(ser)[0] = value |
| 1025 | + |
| 1026 | + # but for object dtype we preserve the nested data and set as such |
| 1027 | + ser = Series([1, "a", "b"], dtype=object) |
| 1028 | + indexer_sli(ser)[0] = value |
| 1029 | + if isinstance(value, np.ndarray): |
| 1030 | + assert (ser.loc[0] == value).all() |
| 1031 | + else: |
| 1032 | + assert ser.loc[0] == value |
| 1033 | + |
| 1034 | + |
| 1035 | +@pytest.mark.parametrize( |
| 1036 | + "value", [(0.0,), [0.0], np.array([0.0]), array.array("d", [0.0])] |
| 1037 | +) |
| 1038 | +def test_scalar_setitem_with_nested_value_length1(value): |
| 1039 | + # https://github.com/pandas-dev/pandas/issues/46268 |
| 1040 | + |
| 1041 | + # For numeric data, assigning length-1 array to scalar position gets unpacked |
| 1042 | + df = DataFrame({"A": [1, 2, 3]}) |
| 1043 | + df.loc[0, "B"] = value |
| 1044 | + expected = DataFrame({"A": [1, 2, 3], "B": [0.0, np.nan, np.nan]}) |
| 1045 | + tm.assert_frame_equal(df, expected) |
| 1046 | + |
| 1047 | + # but for object dtype we preserve the nested data |
| 1048 | + df = DataFrame({"A": [1, 2, 3], "B": np.array([1, "a", "b"], dtype=object)}) |
| 1049 | + df.loc[0, "B"] = value |
| 1050 | + if isinstance(value, np.ndarray): |
| 1051 | + assert (df.loc[0, "B"] == value).all() |
| 1052 | + else: |
| 1053 | + assert df.loc[0, "B"] == value |
| 1054 | + |
| 1055 | + |
| 1056 | +@pytest.mark.parametrize( |
| 1057 | + "value", [(0.0,), [0.0], np.array([0.0]), array.array("d", [0.0])] |
| 1058 | +) |
| 1059 | +def test_scalar_setitem_series_with_nested_value_length1(value, indexer_sli): |
| 1060 | + # For numeric data, assigning length-1 array to scalar position gets unpacked |
| 1061 | + # TODO this only happens in case of ndarray, should we make this consistent |
| 1062 | + # for all list-likes? (as happens for DataFrame.(i)loc, see test above) |
| 1063 | + ser = Series([1.0, 2.0, 3.0]) |
| 1064 | + if isinstance(value, np.ndarray): |
| 1065 | + indexer_sli(ser)[0] = value |
| 1066 | + expected = Series([0.0, 2.0, 3.0]) |
| 1067 | + tm.assert_series_equal(ser, expected) |
| 1068 | + else: |
| 1069 | + with pytest.raises( |
| 1070 | + ValueError, match="setting an array element with a sequence" |
| 1071 | + ): |
| 1072 | + indexer_sli(ser)[0] = value |
| 1073 | + |
| 1074 | + # but for object dtype we preserve the nested data |
| 1075 | + ser = Series([1, "a", "b"], dtype=object) |
| 1076 | + indexer_sli(ser)[0] = value |
| 1077 | + if isinstance(value, np.ndarray): |
| 1078 | + assert (ser.loc[0] == value).all() |
| 1079 | + else: |
| 1080 | + assert ser.loc[0] == value |
0 commit comments