diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index 58f0e5bc1ad39..9eaa0d0ae6876 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -1327,32 +1327,6 @@ def test_getitem_list_duplicates(self): expected = df.iloc[:, 2:] tm.assert_frame_equal(result, expected) - def test_set_value_with_index_dtype_change(self): - df_orig = DataFrame(np.random.randn(3, 3), index=range(3), columns=list("ABC")) - - # this is actually ambiguous as the 2 is interpreted as a positional - # so column is not created - df = df_orig.copy() - df._set_value("C", 2, 1.0) - assert list(df.index) == list(df_orig.index) + ["C"] - # assert list(df.columns) == list(df_orig.columns) + [2] - - df = df_orig.copy() - df.loc["C", 2] = 1.0 - assert list(df.index) == list(df_orig.index) + ["C"] - # assert list(df.columns) == list(df_orig.columns) + [2] - - # create both new - df = df_orig.copy() - df._set_value("C", "D", 1.0) - assert list(df.index) == list(df_orig.index) + ["C"] - assert list(df.columns) == list(df_orig.columns) + ["D"] - - df = df_orig.copy() - df.loc["C", "D"] = 1.0 - assert list(df.index) == list(df_orig.index) + ["C"] - assert list(df.columns) == list(df_orig.columns) + ["D"] - # TODO: rename? remove? def test_single_element_ix_dont_upcast(self, float_frame): float_frame["E"] = 1 diff --git a/pandas/tests/frame/indexing/test_set_value.py b/pandas/tests/frame/indexing/test_set_value.py index 484e2d544197e..84def57f6b6e0 100644 --- a/pandas/tests/frame/indexing/test_set_value.py +++ b/pandas/tests/frame/indexing/test_set_value.py @@ -3,7 +3,7 @@ from pandas.core.dtypes.common import is_float_dtype -from pandas import isna +from pandas import DataFrame, isna class TestSetValue: @@ -38,3 +38,29 @@ def test_set_value_resize(self, float_frame): msg = "could not convert string to float: 'sam'" with pytest.raises(ValueError, match=msg): res._set_value("foobar", "baz", "sam") + + def test_set_value_with_index_dtype_change(self): + df_orig = DataFrame(np.random.randn(3, 3), index=range(3), columns=list("ABC")) + + # this is actually ambiguous as the 2 is interpreted as a positional + # so column is not created + df = df_orig.copy() + df._set_value("C", 2, 1.0) + assert list(df.index) == list(df_orig.index) + ["C"] + # assert list(df.columns) == list(df_orig.columns) + [2] + + df = df_orig.copy() + df.loc["C", 2] = 1.0 + assert list(df.index) == list(df_orig.index) + ["C"] + # assert list(df.columns) == list(df_orig.columns) + [2] + + # create both new + df = df_orig.copy() + df._set_value("C", "D", 1.0) + assert list(df.index) == list(df_orig.index) + ["C"] + assert list(df.columns) == list(df_orig.columns) + ["D"] + + df = df_orig.copy() + df.loc["C", "D"] = 1.0 + assert list(df.index) == list(df_orig.index) + ["C"] + assert list(df.columns) == list(df_orig.columns) + ["D"] diff --git a/pandas/tests/indexing/test_callable.py b/pandas/tests/indexing/test_callable.py deleted file mode 100644 index b98c9a3df0438..0000000000000 --- a/pandas/tests/indexing/test_callable.py +++ /dev/null @@ -1,254 +0,0 @@ -import numpy as np - -import pandas as pd -import pandas._testing as tm - - -class TestIndexingCallable: - def test_frame_loc_callable(self): - # GH 11485 - df = pd.DataFrame({"A": [1, 2, 3, 4], "B": list("aabb"), "C": [1, 2, 3, 4]}) - # iloc cannot use boolean Series (see GH3635) - - # return bool indexer - res = df.loc[lambda x: x.A > 2] - tm.assert_frame_equal(res, df.loc[df.A > 2]) - - res = df.loc[lambda x: x.A > 2] - tm.assert_frame_equal(res, df.loc[df.A > 2]) - - res = df.loc[lambda x: x.A > 2] - tm.assert_frame_equal(res, df.loc[df.A > 2]) - - res = df.loc[lambda x: x.A > 2] - tm.assert_frame_equal(res, df.loc[df.A > 2]) - - res = df.loc[lambda x: x.B == "b", :] - tm.assert_frame_equal(res, df.loc[df.B == "b", :]) - - res = df.loc[lambda x: x.B == "b", :] - tm.assert_frame_equal(res, df.loc[df.B == "b", :]) - - res = df.loc[lambda x: x.A > 2, lambda x: x.columns == "B"] - tm.assert_frame_equal(res, df.loc[df.A > 2, [False, True, False]]) - - res = df.loc[lambda x: x.A > 2, lambda x: x.columns == "B"] - tm.assert_frame_equal(res, df.loc[df.A > 2, [False, True, False]]) - - res = df.loc[lambda x: x.A > 2, lambda x: "B"] - tm.assert_series_equal(res, df.loc[df.A > 2, "B"]) - - res = df.loc[lambda x: x.A > 2, lambda x: "B"] - tm.assert_series_equal(res, df.loc[df.A > 2, "B"]) - - res = df.loc[lambda x: x.A > 2, lambda x: ["A", "B"]] - tm.assert_frame_equal(res, df.loc[df.A > 2, ["A", "B"]]) - - res = df.loc[lambda x: x.A > 2, lambda x: ["A", "B"]] - tm.assert_frame_equal(res, df.loc[df.A > 2, ["A", "B"]]) - - res = df.loc[lambda x: x.A == 2, lambda x: ["A", "B"]] - tm.assert_frame_equal(res, df.loc[df.A == 2, ["A", "B"]]) - - res = df.loc[lambda x: x.A == 2, lambda x: ["A", "B"]] - tm.assert_frame_equal(res, df.loc[df.A == 2, ["A", "B"]]) - - # scalar - res = df.loc[lambda x: 1, lambda x: "A"] - assert res == df.loc[1, "A"] - - res = df.loc[lambda x: 1, lambda x: "A"] - assert res == df.loc[1, "A"] - - def test_frame_loc_callable_mixture(self): - # GH 11485 - df = pd.DataFrame({"A": [1, 2, 3, 4], "B": list("aabb"), "C": [1, 2, 3, 4]}) - - res = df.loc[lambda x: x.A > 2, ["A", "B"]] - tm.assert_frame_equal(res, df.loc[df.A > 2, ["A", "B"]]) - - res = df.loc[lambda x: x.A > 2, ["A", "B"]] - tm.assert_frame_equal(res, df.loc[df.A > 2, ["A", "B"]]) - - res = df.loc[[2, 3], lambda x: ["A", "B"]] - tm.assert_frame_equal(res, df.loc[[2, 3], ["A", "B"]]) - - res = df.loc[[2, 3], lambda x: ["A", "B"]] - tm.assert_frame_equal(res, df.loc[[2, 3], ["A", "B"]]) - - res = df.loc[3, lambda x: ["A", "B"]] - tm.assert_series_equal(res, df.loc[3, ["A", "B"]]) - - res = df.loc[3, lambda x: ["A", "B"]] - tm.assert_series_equal(res, df.loc[3, ["A", "B"]]) - - def test_frame_loc_callable_labels(self): - # GH 11485 - df = pd.DataFrame({"X": [1, 2, 3, 4], "Y": list("aabb")}, index=list("ABCD")) - - # return label - res = df.loc[lambda x: ["A", "C"]] - tm.assert_frame_equal(res, df.loc[["A", "C"]]) - - res = df.loc[lambda x: ["A", "C"]] - tm.assert_frame_equal(res, df.loc[["A", "C"]]) - - res = df.loc[lambda x: ["A", "C"], :] - tm.assert_frame_equal(res, df.loc[["A", "C"], :]) - - res = df.loc[lambda x: ["A", "C"], lambda x: "X"] - tm.assert_series_equal(res, df.loc[["A", "C"], "X"]) - - res = df.loc[lambda x: ["A", "C"], lambda x: ["X"]] - tm.assert_frame_equal(res, df.loc[["A", "C"], ["X"]]) - - # mixture - res = df.loc[["A", "C"], lambda x: "X"] - tm.assert_series_equal(res, df.loc[["A", "C"], "X"]) - - res = df.loc[["A", "C"], lambda x: ["X"]] - tm.assert_frame_equal(res, df.loc[["A", "C"], ["X"]]) - - res = df.loc[lambda x: ["A", "C"], "X"] - tm.assert_series_equal(res, df.loc[["A", "C"], "X"]) - - res = df.loc[lambda x: ["A", "C"], ["X"]] - tm.assert_frame_equal(res, df.loc[["A", "C"], ["X"]]) - - def test_frame_loc_callable_setitem(self): - # GH 11485 - df = pd.DataFrame({"X": [1, 2, 3, 4], "Y": list("aabb")}, index=list("ABCD")) - - # return label - res = df.copy() - res.loc[lambda x: ["A", "C"]] = -20 - exp = df.copy() - exp.loc[["A", "C"]] = -20 - tm.assert_frame_equal(res, exp) - - res = df.copy() - res.loc[lambda x: ["A", "C"], :] = 20 - exp = df.copy() - exp.loc[["A", "C"], :] = 20 - tm.assert_frame_equal(res, exp) - - res = df.copy() - res.loc[lambda x: ["A", "C"], lambda x: "X"] = -1 - exp = df.copy() - exp.loc[["A", "C"], "X"] = -1 - tm.assert_frame_equal(res, exp) - - res = df.copy() - res.loc[lambda x: ["A", "C"], lambda x: ["X"]] = [5, 10] - exp = df.copy() - exp.loc[["A", "C"], ["X"]] = [5, 10] - tm.assert_frame_equal(res, exp) - - # mixture - res = df.copy() - res.loc[["A", "C"], lambda x: "X"] = np.array([-1, -2]) - exp = df.copy() - exp.loc[["A", "C"], "X"] = np.array([-1, -2]) - tm.assert_frame_equal(res, exp) - - res = df.copy() - res.loc[["A", "C"], lambda x: ["X"]] = 10 - exp = df.copy() - exp.loc[["A", "C"], ["X"]] = 10 - tm.assert_frame_equal(res, exp) - - res = df.copy() - res.loc[lambda x: ["A", "C"], "X"] = -2 - exp = df.copy() - exp.loc[["A", "C"], "X"] = -2 - tm.assert_frame_equal(res, exp) - - res = df.copy() - res.loc[lambda x: ["A", "C"], ["X"]] = -4 - exp = df.copy() - exp.loc[["A", "C"], ["X"]] = -4 - tm.assert_frame_equal(res, exp) - - def test_frame_iloc_callable(self): - # GH 11485 - df = pd.DataFrame({"X": [1, 2, 3, 4], "Y": list("aabb")}, index=list("ABCD")) - - # return location - res = df.iloc[lambda x: [1, 3]] - tm.assert_frame_equal(res, df.iloc[[1, 3]]) - - res = df.iloc[lambda x: [1, 3], :] - tm.assert_frame_equal(res, df.iloc[[1, 3], :]) - - res = df.iloc[lambda x: [1, 3], lambda x: 0] - tm.assert_series_equal(res, df.iloc[[1, 3], 0]) - - res = df.iloc[lambda x: [1, 3], lambda x: [0]] - tm.assert_frame_equal(res, df.iloc[[1, 3], [0]]) - - # mixture - res = df.iloc[[1, 3], lambda x: 0] - tm.assert_series_equal(res, df.iloc[[1, 3], 0]) - - res = df.iloc[[1, 3], lambda x: [0]] - tm.assert_frame_equal(res, df.iloc[[1, 3], [0]]) - - res = df.iloc[lambda x: [1, 3], 0] - tm.assert_series_equal(res, df.iloc[[1, 3], 0]) - - res = df.iloc[lambda x: [1, 3], [0]] - tm.assert_frame_equal(res, df.iloc[[1, 3], [0]]) - - def test_frame_iloc_callable_setitem(self): - # GH 11485 - df = pd.DataFrame({"X": [1, 2, 3, 4], "Y": list("aabb")}, index=list("ABCD")) - - # return location - res = df.copy() - res.iloc[lambda x: [1, 3]] = 0 - exp = df.copy() - exp.iloc[[1, 3]] = 0 - tm.assert_frame_equal(res, exp) - - res = df.copy() - res.iloc[lambda x: [1, 3], :] = -1 - exp = df.copy() - exp.iloc[[1, 3], :] = -1 - tm.assert_frame_equal(res, exp) - - res = df.copy() - res.iloc[lambda x: [1, 3], lambda x: 0] = 5 - exp = df.copy() - exp.iloc[[1, 3], 0] = 5 - tm.assert_frame_equal(res, exp) - - res = df.copy() - res.iloc[lambda x: [1, 3], lambda x: [0]] = 25 - exp = df.copy() - exp.iloc[[1, 3], [0]] = 25 - tm.assert_frame_equal(res, exp) - - # mixture - res = df.copy() - res.iloc[[1, 3], lambda x: 0] = -3 - exp = df.copy() - exp.iloc[[1, 3], 0] = -3 - tm.assert_frame_equal(res, exp) - - res = df.copy() - res.iloc[[1, 3], lambda x: [0]] = -5 - exp = df.copy() - exp.iloc[[1, 3], [0]] = -5 - tm.assert_frame_equal(res, exp) - - res = df.copy() - res.iloc[lambda x: [1, 3], 0] = 10 - exp = df.copy() - exp.iloc[[1, 3], 0] = 10 - tm.assert_frame_equal(res, exp) - - res = df.copy() - res.iloc[lambda x: [1, 3], [0]] = [-5, -5] - exp = df.copy() - exp.iloc[[1, 3], [0]] = [-5, -5] - tm.assert_frame_equal(res, exp) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 867941a97b598..6c80354610a78 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -778,6 +778,92 @@ def test_iloc_setitem_series_duplicate_columns(self): assert df.dtypes.iloc[2] == np.int64 +class TestILocCallable: + def test_frame_iloc_getitem_callable(self): + # GH#11485 + df = DataFrame({"X": [1, 2, 3, 4], "Y": list("aabb")}, index=list("ABCD")) + + # return location + res = df.iloc[lambda x: [1, 3]] + tm.assert_frame_equal(res, df.iloc[[1, 3]]) + + res = df.iloc[lambda x: [1, 3], :] + tm.assert_frame_equal(res, df.iloc[[1, 3], :]) + + res = df.iloc[lambda x: [1, 3], lambda x: 0] + tm.assert_series_equal(res, df.iloc[[1, 3], 0]) + + res = df.iloc[lambda x: [1, 3], lambda x: [0]] + tm.assert_frame_equal(res, df.iloc[[1, 3], [0]]) + + # mixture + res = df.iloc[[1, 3], lambda x: 0] + tm.assert_series_equal(res, df.iloc[[1, 3], 0]) + + res = df.iloc[[1, 3], lambda x: [0]] + tm.assert_frame_equal(res, df.iloc[[1, 3], [0]]) + + res = df.iloc[lambda x: [1, 3], 0] + tm.assert_series_equal(res, df.iloc[[1, 3], 0]) + + res = df.iloc[lambda x: [1, 3], [0]] + tm.assert_frame_equal(res, df.iloc[[1, 3], [0]]) + + def test_frame_iloc_setitem_callable(self): + # GH#11485 + df = DataFrame({"X": [1, 2, 3, 4], "Y": list("aabb")}, index=list("ABCD")) + + # return location + res = df.copy() + res.iloc[lambda x: [1, 3]] = 0 + exp = df.copy() + exp.iloc[[1, 3]] = 0 + tm.assert_frame_equal(res, exp) + + res = df.copy() + res.iloc[lambda x: [1, 3], :] = -1 + exp = df.copy() + exp.iloc[[1, 3], :] = -1 + tm.assert_frame_equal(res, exp) + + res = df.copy() + res.iloc[lambda x: [1, 3], lambda x: 0] = 5 + exp = df.copy() + exp.iloc[[1, 3], 0] = 5 + tm.assert_frame_equal(res, exp) + + res = df.copy() + res.iloc[lambda x: [1, 3], lambda x: [0]] = 25 + exp = df.copy() + exp.iloc[[1, 3], [0]] = 25 + tm.assert_frame_equal(res, exp) + + # mixture + res = df.copy() + res.iloc[[1, 3], lambda x: 0] = -3 + exp = df.copy() + exp.iloc[[1, 3], 0] = -3 + tm.assert_frame_equal(res, exp) + + res = df.copy() + res.iloc[[1, 3], lambda x: [0]] = -5 + exp = df.copy() + exp.iloc[[1, 3], [0]] = -5 + tm.assert_frame_equal(res, exp) + + res = df.copy() + res.iloc[lambda x: [1, 3], 0] = 10 + exp = df.copy() + exp.iloc[[1, 3], 0] = 10 + tm.assert_frame_equal(res, exp) + + res = df.copy() + res.iloc[lambda x: [1, 3], [0]] = [-5, -5] + exp = df.copy() + exp.iloc[[1, 3], [0]] = [-5, -5] + tm.assert_frame_equal(res, exp) + + class TestILocSeries: def test_iloc(self): ser = Series(np.random.randn(10), index=list(range(0, 20, 2))) diff --git a/pandas/tests/indexing/test_indexing_slow.py b/pandas/tests/indexing/test_indexing_slow.py deleted file mode 100644 index 2ffa44bec14a6..0000000000000 --- a/pandas/tests/indexing/test_indexing_slow.py +++ /dev/null @@ -1,14 +0,0 @@ -import pytest - -from pandas import DataFrame -import pandas._testing as tm - - -class TestIndexingSlow: - @pytest.mark.slow - def test_large_dataframe_indexing(self): - # GH10692 - result = DataFrame({"x": range(10 ** 6)}, dtype="int64") - result.loc[len(result)] = len(result) + 1 - expected = DataFrame({"x": range(10 ** 6 + 1)}, dtype="int64") - tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index b1c66c3c8850a..d1dcae5997b9d 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -1082,6 +1082,182 @@ def test_loc_setitem_multiindex_slice(self): tm.assert_series_equal(result, expected) +class TestLocSetitemWithExpansion: + @pytest.mark.slow + def test_loc_setitem_with_expansion_large_dataframe(self): + # GH#10692 + result = DataFrame({"x": range(10 ** 6)}, dtype="int64") + result.loc[len(result)] = len(result) + 1 + expected = DataFrame({"x": range(10 ** 6 + 1)}, dtype="int64") + tm.assert_frame_equal(result, expected) + + +class TestLocCallable: + def test_frame_loc_getitem_callable(self): + # GH#11485 + df = DataFrame({"A": [1, 2, 3, 4], "B": list("aabb"), "C": [1, 2, 3, 4]}) + # iloc cannot use boolean Series (see GH3635) + + # return bool indexer + res = df.loc[lambda x: x.A > 2] + tm.assert_frame_equal(res, df.loc[df.A > 2]) + + res = df.loc[lambda x: x.A > 2] + tm.assert_frame_equal(res, df.loc[df.A > 2]) + + res = df.loc[lambda x: x.A > 2] + tm.assert_frame_equal(res, df.loc[df.A > 2]) + + res = df.loc[lambda x: x.A > 2] + tm.assert_frame_equal(res, df.loc[df.A > 2]) + + res = df.loc[lambda x: x.B == "b", :] + tm.assert_frame_equal(res, df.loc[df.B == "b", :]) + + res = df.loc[lambda x: x.B == "b", :] + tm.assert_frame_equal(res, df.loc[df.B == "b", :]) + + res = df.loc[lambda x: x.A > 2, lambda x: x.columns == "B"] + tm.assert_frame_equal(res, df.loc[df.A > 2, [False, True, False]]) + + res = df.loc[lambda x: x.A > 2, lambda x: x.columns == "B"] + tm.assert_frame_equal(res, df.loc[df.A > 2, [False, True, False]]) + + res = df.loc[lambda x: x.A > 2, lambda x: "B"] + tm.assert_series_equal(res, df.loc[df.A > 2, "B"]) + + res = df.loc[lambda x: x.A > 2, lambda x: "B"] + tm.assert_series_equal(res, df.loc[df.A > 2, "B"]) + + res = df.loc[lambda x: x.A > 2, lambda x: ["A", "B"]] + tm.assert_frame_equal(res, df.loc[df.A > 2, ["A", "B"]]) + + res = df.loc[lambda x: x.A > 2, lambda x: ["A", "B"]] + tm.assert_frame_equal(res, df.loc[df.A > 2, ["A", "B"]]) + + res = df.loc[lambda x: x.A == 2, lambda x: ["A", "B"]] + tm.assert_frame_equal(res, df.loc[df.A == 2, ["A", "B"]]) + + res = df.loc[lambda x: x.A == 2, lambda x: ["A", "B"]] + tm.assert_frame_equal(res, df.loc[df.A == 2, ["A", "B"]]) + + # scalar + res = df.loc[lambda x: 1, lambda x: "A"] + assert res == df.loc[1, "A"] + + res = df.loc[lambda x: 1, lambda x: "A"] + assert res == df.loc[1, "A"] + + def test_frame_loc_getitem_callable_mixture(self): + # GH#11485 + df = DataFrame({"A": [1, 2, 3, 4], "B": list("aabb"), "C": [1, 2, 3, 4]}) + + res = df.loc[lambda x: x.A > 2, ["A", "B"]] + tm.assert_frame_equal(res, df.loc[df.A > 2, ["A", "B"]]) + + res = df.loc[lambda x: x.A > 2, ["A", "B"]] + tm.assert_frame_equal(res, df.loc[df.A > 2, ["A", "B"]]) + + res = df.loc[[2, 3], lambda x: ["A", "B"]] + tm.assert_frame_equal(res, df.loc[[2, 3], ["A", "B"]]) + + res = df.loc[[2, 3], lambda x: ["A", "B"]] + tm.assert_frame_equal(res, df.loc[[2, 3], ["A", "B"]]) + + res = df.loc[3, lambda x: ["A", "B"]] + tm.assert_series_equal(res, df.loc[3, ["A", "B"]]) + + res = df.loc[3, lambda x: ["A", "B"]] + tm.assert_series_equal(res, df.loc[3, ["A", "B"]]) + + def test_frame_loc_getitem_callable_labels(self): + # GH#11485 + df = DataFrame({"X": [1, 2, 3, 4], "Y": list("aabb")}, index=list("ABCD")) + + # return label + res = df.loc[lambda x: ["A", "C"]] + tm.assert_frame_equal(res, df.loc[["A", "C"]]) + + res = df.loc[lambda x: ["A", "C"]] + tm.assert_frame_equal(res, df.loc[["A", "C"]]) + + res = df.loc[lambda x: ["A", "C"], :] + tm.assert_frame_equal(res, df.loc[["A", "C"], :]) + + res = df.loc[lambda x: ["A", "C"], lambda x: "X"] + tm.assert_series_equal(res, df.loc[["A", "C"], "X"]) + + res = df.loc[lambda x: ["A", "C"], lambda x: ["X"]] + tm.assert_frame_equal(res, df.loc[["A", "C"], ["X"]]) + + # mixture + res = df.loc[["A", "C"], lambda x: "X"] + tm.assert_series_equal(res, df.loc[["A", "C"], "X"]) + + res = df.loc[["A", "C"], lambda x: ["X"]] + tm.assert_frame_equal(res, df.loc[["A", "C"], ["X"]]) + + res = df.loc[lambda x: ["A", "C"], "X"] + tm.assert_series_equal(res, df.loc[["A", "C"], "X"]) + + res = df.loc[lambda x: ["A", "C"], ["X"]] + tm.assert_frame_equal(res, df.loc[["A", "C"], ["X"]]) + + def test_frame_loc_setitem_callable(self): + # GH#11485 + df = DataFrame({"X": [1, 2, 3, 4], "Y": list("aabb")}, index=list("ABCD")) + + # return label + res = df.copy() + res.loc[lambda x: ["A", "C"]] = -20 + exp = df.copy() + exp.loc[["A", "C"]] = -20 + tm.assert_frame_equal(res, exp) + + res = df.copy() + res.loc[lambda x: ["A", "C"], :] = 20 + exp = df.copy() + exp.loc[["A", "C"], :] = 20 + tm.assert_frame_equal(res, exp) + + res = df.copy() + res.loc[lambda x: ["A", "C"], lambda x: "X"] = -1 + exp = df.copy() + exp.loc[["A", "C"], "X"] = -1 + tm.assert_frame_equal(res, exp) + + res = df.copy() + res.loc[lambda x: ["A", "C"], lambda x: ["X"]] = [5, 10] + exp = df.copy() + exp.loc[["A", "C"], ["X"]] = [5, 10] + tm.assert_frame_equal(res, exp) + + # mixture + res = df.copy() + res.loc[["A", "C"], lambda x: "X"] = np.array([-1, -2]) + exp = df.copy() + exp.loc[["A", "C"], "X"] = np.array([-1, -2]) + tm.assert_frame_equal(res, exp) + + res = df.copy() + res.loc[["A", "C"], lambda x: ["X"]] = 10 + exp = df.copy() + exp.loc[["A", "C"], ["X"]] = 10 + tm.assert_frame_equal(res, exp) + + res = df.copy() + res.loc[lambda x: ["A", "C"], "X"] = -2 + exp = df.copy() + exp.loc[["A", "C"], "X"] = -2 + tm.assert_frame_equal(res, exp) + + res = df.copy() + res.loc[lambda x: ["A", "C"], ["X"]] = -4 + exp = df.copy() + exp.loc[["A", "C"], ["X"]] = -4 + tm.assert_frame_equal(res, exp) + + def test_series_loc_getitem_label_list_missing_values(): # gh-11428 key = np.array(