Skip to content

Commit 24ca7c2

Browse files
author
MarcoGorelli
committed
fixup test_loc
1 parent 2dd85ab commit 24ca7c2

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

pandas/tests/indexing/test_loc.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,10 @@ def test_loc_setitem_slice(self):
382382
df2 = DataFrame({"a": [0, 1, 1], "b": [100, 200, 300]}, dtype="uint64")
383383
ix = df1["a"] == 1
384384
newb2 = df2.loc[ix, "b"]
385-
df1.loc[ix, "b"] = newb2
385+
with tm.assert_produces_warning(
386+
FutureWarning, match="item of incompatible dtype"
387+
):
388+
df1.loc[ix, "b"] = newb2
386389
expected = DataFrame({"a": [0, 1, 1], "b": [100, 200, 300]}, dtype="uint64")
387390
tm.assert_frame_equal(df2, expected)
388391

@@ -869,7 +872,10 @@ def test_loc_setitem_with_scalar_index(self, indexer, value):
869872
# elementwisely, not using "setter('A', ['Z'])".
870873

871874
df = DataFrame([[1, 2], [3, 4]], columns=["A", "B"])
872-
df.loc[0, indexer] = value
875+
with tm.assert_produces_warning(
876+
FutureWarning, match="item of incompatible dtype"
877+
):
878+
df.loc[0, indexer] = value
873879
result = df.loc[0, "A"]
874880

875881
assert is_scalar(result) and result == "Z"
@@ -1415,8 +1421,11 @@ def test_loc_setitem_categorical_values_partial_column_slice(self):
14151421
# the Categorical
14161422
df = DataFrame({"a": [1, 1, 1, 1, 1], "b": list("aaaaa")})
14171423
exp = DataFrame({"a": [1, "b", "b", 1, 1], "b": list("aabba")})
1418-
df.loc[1:2, "a"] = Categorical(["b", "b"], categories=["a", "b"])
1419-
df.loc[2:3, "b"] = Categorical(["b", "b"], categories=["a", "b"])
1424+
with tm.assert_produces_warning(
1425+
FutureWarning, match="item of incompatible dtype"
1426+
):
1427+
df.loc[1:2, "a"] = Categorical(["b", "b"], categories=["a", "b"])
1428+
df.loc[2:3, "b"] = Categorical(["b", "b"], categories=["a", "b"])
14201429
tm.assert_frame_equal(df, exp)
14211430

14221431
def test_loc_setitem_single_row_categorical(self):
@@ -1537,11 +1546,17 @@ def test_loc_setitem_2d_to_1d_raises(self):
15371546
]
15381547
)
15391548
with pytest.raises(ValueError, match=msg):
1540-
ser.loc[range(2)] = data
1549+
with tm.assert_produces_warning(
1550+
FutureWarning, match="item of incompatible dtype"
1551+
):
1552+
ser.loc[range(2)] = data
15411553

15421554
msg = r"could not broadcast input array from shape \(2,2\) into shape \(2,?\)"
15431555
with pytest.raises(ValueError, match=msg):
1544-
ser.loc[:] = data
1556+
with tm.assert_produces_warning(
1557+
FutureWarning, match="item of incompatible dtype"
1558+
):
1559+
ser.loc[:] = data
15451560

15461561
def test_loc_getitem_interval_index(self):
15471562
# GH#19977
@@ -1607,7 +1622,10 @@ def test_loc_setitem_cast2(self):
16071622
# dtype conversion on setting
16081623
df = DataFrame(np.random.rand(30, 3), columns=tuple("ABC"))
16091624
df["event"] = np.nan
1610-
df.loc[10, "event"] = "foo"
1625+
with tm.assert_produces_warning(
1626+
FutureWarning, match="item of incompatible dtype"
1627+
):
1628+
df.loc[10, "event"] = "foo"
16111629
result = df.dtypes
16121630
expected = Series(
16131631
[np.dtype("float64")] * 3 + [np.dtype("object")],
@@ -2910,7 +2928,8 @@ def test_loc_setitem_uint8_upcast(value):
29102928
# GH#26049
29112929

29122930
df = DataFrame([1, 2, 3, 4], columns=["col1"], dtype="uint8")
2913-
df.loc[2, "col1"] = value # value that can't be held in uint8
2931+
with tm.assert_produces_warning(FutureWarning, match="item of incompatible dtype"):
2932+
df.loc[2, "col1"] = value # value that can't be held in uint8
29142933

29152934
expected = DataFrame([1, 2, 300, 4], columns=["col1"], dtype="uint16")
29162935
tm.assert_frame_equal(df, expected)

0 commit comments

Comments
 (0)