Skip to content

Commit 6bfbb53

Browse files
committed
wip
1 parent 4ca7945 commit 6bfbb53

File tree

2 files changed

+19
-104
lines changed

2 files changed

+19
-104
lines changed

pandas/tests/indexing/test_indexing.py

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,8 @@ def test_setitem_dtype_upcast(self):
180180
df["c"] = np.nan
181181
assert df["c"].dtype == np.float64
182182

183-
with tm.assert_produces_warning(
184-
FutureWarning, match="item of incompatible dtype"
185-
):
183+
with pytest.raises(TypeError, match="Invalid value"):
186184
df.loc[0, "c"] = "foo"
187-
expected = DataFrame(
188-
{"a": [1, 3], "b": [np.nan, 2], "c": Series(["foo", np.nan], dtype=object)}
189-
)
190-
tm.assert_frame_equal(df, expected)
191185

192186
@pytest.mark.parametrize("val", [3.14, "wxyz"])
193187
def test_setitem_dtype_upcast2(self, val):
@@ -199,41 +193,18 @@ def test_setitem_dtype_upcast2(self, val):
199193
)
200194

201195
left = df.copy()
202-
with tm.assert_produces_warning(
203-
FutureWarning, match="item of incompatible dtype"
204-
):
196+
with pytest.raises(TypeError, match="Invalid value"):
205197
left.loc["a", "bar"] = val
206-
right = DataFrame(
207-
[[0, val, 2], [3, 4, 5]],
208-
index=list("ab"),
209-
columns=["foo", "bar", "baz"],
210-
)
211-
212-
tm.assert_frame_equal(left, right)
213-
assert is_integer_dtype(left["foo"])
214-
assert is_integer_dtype(left["baz"])
215198

216199
def test_setitem_dtype_upcast3(self):
217200
left = DataFrame(
218201
np.arange(6, dtype="int64").reshape(2, 3) / 10.0,
219202
index=list("ab"),
220203
columns=["foo", "bar", "baz"],
221204
)
222-
with tm.assert_produces_warning(
223-
FutureWarning, match="item of incompatible dtype"
224-
):
205+
with pytest.raises(TypeError, match="Invalid value"):
225206
left.loc["a", "bar"] = "wxyz"
226207

227-
right = DataFrame(
228-
[[0, "wxyz", 0.2], [0.3, 0.4, 0.5]],
229-
index=list("ab"),
230-
columns=["foo", "bar", "baz"],
231-
)
232-
233-
tm.assert_frame_equal(left, right)
234-
assert is_float_dtype(left["foo"])
235-
assert is_float_dtype(left["baz"])
236-
237208
def test_dups_fancy_indexing(self):
238209
# GH 3455
239210

@@ -728,7 +699,7 @@ def run_tests(df, rhs, right_loc, right_iloc):
728699
frame["jolie"] = frame["jolie"].map(lambda x: f"@{x}")
729700
right_iloc["joe"] = [1.0, "@-28", "@-20", "@-12", 17.0]
730701
right_iloc["jolie"] = ["@2", -26.0, -18.0, -10.0, "@18"]
731-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
702+
with pytest.raises(TypeError, match="Invalid value"):
732703
run_tests(df, rhs, right_loc, right_iloc)
733704

734705
@pytest.mark.parametrize(

pandas/tests/indexing/test_loc.py

Lines changed: 15 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from pandas._config import using_pyarrow_string_dtype
1717

1818
from pandas._libs import index as libindex
19-
from pandas.compat.numpy import np_version_gt2
2019
from pandas.errors import IndexingError
2120

2221
import pandas as pd
@@ -383,12 +382,8 @@ def test_loc_setitem_slice(self):
383382
df2 = DataFrame({"a": [0, 1, 1], "b": [100, 200, 300]}, dtype="uint64")
384383
ix = df1["a"] == 1
385384
newb2 = df2.loc[ix, "b"]
386-
with tm.assert_produces_warning(
387-
FutureWarning, match="item of incompatible dtype"
388-
):
385+
with pytest.raises(TypeError, match="Invalid value"):
389386
df1.loc[ix, "b"] = newb2
390-
expected = DataFrame({"a": [0, 1, 1], "b": [100, 200, 300]}, dtype="uint64")
391-
tm.assert_frame_equal(df2, expected)
392387

393388
def test_loc_setitem_dtype(self):
394389
# GH31340
@@ -572,54 +567,31 @@ def frame_for_consistency(self):
572567
def test_loc_setitem_consistency(self, frame_for_consistency, val):
573568
# GH 6149
574569
# coerce similarly for setitem and loc when rows have a null-slice
575-
expected = DataFrame(
576-
{
577-
"date": Series(0, index=range(5), dtype=np.int64),
578-
"val": Series(range(5), dtype=np.int64),
579-
}
580-
)
581570
df = frame_for_consistency.copy()
582-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
571+
with pytest.raises(TypeError, match="Invalid value"):
583572
df.loc[:, "date"] = val
584-
tm.assert_frame_equal(df, expected)
585573

586574
def test_loc_setitem_consistency_dt64_to_str(self, frame_for_consistency):
587575
# GH 6149
588576
# coerce similarly for setitem and loc when rows have a null-slice
589577

590-
expected = DataFrame(
591-
{
592-
"date": Series("foo", index=range(5)),
593-
"val": Series(range(5), dtype=np.int64),
594-
}
595-
)
596578
df = frame_for_consistency.copy()
597-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
579+
with pytest.raises(TypeError, match="Invalid value"):
598580
df.loc[:, "date"] = "foo"
599-
tm.assert_frame_equal(df, expected)
600581

601582
def test_loc_setitem_consistency_dt64_to_float(self, frame_for_consistency):
602583
# GH 6149
603584
# coerce similarly for setitem and loc when rows have a null-slice
604-
expected = DataFrame(
605-
{
606-
"date": Series(1.0, index=range(5)),
607-
"val": Series(range(5), dtype=np.int64),
608-
}
609-
)
610585
df = frame_for_consistency.copy()
611-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
586+
with pytest.raises(TypeError, match="Invalid value"):
612587
df.loc[:, "date"] = 1.0
613-
tm.assert_frame_equal(df, expected)
614588

615589
def test_loc_setitem_consistency_single_row(self):
616590
# GH 15494
617591
# setting on frame with single row
618592
df = DataFrame({"date": Series([Timestamp("20180101")])})
619-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
593+
with pytest.raises(TypeError, match="Invalid value"):
620594
df.loc[:, "date"] = "string"
621-
expected = DataFrame({"date": Series(["string"])})
622-
tm.assert_frame_equal(df, expected)
623595

624596
def test_loc_setitem_consistency_empty(self):
625597
# empty (essentially noops)
@@ -677,16 +649,11 @@ def test_loc_setitem_consistency_slice_column_len(self):
677649

678650
# timedelta64[m] -> float, so this cannot be done inplace, so
679651
# no warning
680-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
652+
with pytest.raises(TypeError, match="Invalid value"):
681653
df.loc[:, ("Respondent", "Duration")] = df.loc[
682654
:, ("Respondent", "Duration")
683655
] / Timedelta(60_000_000_000)
684656

685-
expected = Series(
686-
[23.0, 12.0, 14.0, 36.0], index=df.index, name=("Respondent", "Duration")
687-
)
688-
tm.assert_series_equal(df[("Respondent", "Duration")], expected)
689-
690657
@pytest.mark.parametrize("unit", ["Y", "M", "D", "h", "m", "s", "ms", "us"])
691658
def test_loc_assign_non_ns_datetime(self, unit):
692659
# GH 27395, non-ns dtype assignment via .loc should work
@@ -1413,13 +1380,9 @@ def test_loc_setitem_categorical_values_partial_column_slice(self):
14131380
# Assigning a Category to parts of a int/... column uses the values of
14141381
# the Categorical
14151382
df = DataFrame({"a": [1, 1, 1, 1, 1], "b": list("aaaaa")})
1416-
exp = DataFrame({"a": [1, "b", "b", 1, 1], "b": list("aabba")})
1417-
with tm.assert_produces_warning(
1418-
FutureWarning, match="item of incompatible dtype"
1419-
):
1383+
with pytest.raises(TypeError, match="Invalid value"):
14201384
df.loc[1:2, "a"] = Categorical(["b", "b"], categories=["a", "b"])
14211385
df.loc[2:3, "b"] = Categorical(["b", "b"], categories=["a", "b"])
1422-
tm.assert_frame_equal(df, exp)
14231386

14241387
def test_loc_setitem_single_row_categorical(self, using_infer_string):
14251388
# GH#25495
@@ -1446,9 +1409,8 @@ def test_loc_setitem_datetime_coercion(self):
14461409
df.loc[0:1, "c"] = np.datetime64("2008-08-08")
14471410
assert Timestamp("2008-08-08") == df.loc[0, "c"]
14481411
assert Timestamp("2008-08-08") == df.loc[1, "c"]
1449-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
1412+
with pytest.raises(TypeError, match="Invalid value"):
14501413
df.loc[2, "c"] = date(2005, 5, 5)
1451-
assert Timestamp("2005-05-05").date() == df.loc[2, "c"]
14521414

14531415
@pytest.mark.parametrize("idxer", ["var", ["var"]])
14541416
def test_loc_setitem_datetimeindex_tz(self, idxer, tz_naive_fixture):
@@ -1459,12 +1421,13 @@ def test_loc_setitem_datetimeindex_tz(self, idxer, tz_naive_fixture):
14591421
# if result started off with object dtype, then the .loc.__setitem__
14601422
# below would retain object dtype
14611423
result = DataFrame(index=idx, columns=["var"], dtype=np.float64)
1462-
with tm.assert_produces_warning(
1463-
FutureWarning if idxer == "var" else None, match="incompatible dtype"
1464-
):
1424+
if idxer == "var":
1425+
with pytest.raises(TypeError, match="Invalid value"):
1426+
result.loc[:, idxer] = expected
1427+
else:
14651428
# See https://github.com/pandas-dev/pandas/issues/56223
14661429
result.loc[:, idxer] = expected
1467-
tm.assert_frame_equal(result, expected)
1430+
tm.assert_frame_equal(result, expected)
14681431

14691432
def test_loc_setitem_time_key(self):
14701433
index = date_range("2012-01-01", "2012-01-05", freq="30min")
@@ -1610,16 +1573,8 @@ def test_loc_setitem_cast2(self):
16101573
# dtype conversion on setting
16111574
df = DataFrame(np.random.default_rng(2).random((30, 3)), columns=tuple("ABC"))
16121575
df["event"] = np.nan
1613-
with tm.assert_produces_warning(
1614-
FutureWarning, match="item of incompatible dtype"
1615-
):
1576+
with pytest.raises(TypeError, match="Invalid value"):
16161577
df.loc[10, "event"] = "foo"
1617-
result = df.dtypes
1618-
expected = Series(
1619-
[np.dtype("float64")] * 3 + [np.dtype("object")],
1620-
index=["A", "B", "C", "event"],
1621-
)
1622-
tm.assert_series_equal(result, expected)
16231578

16241579
def test_loc_setitem_cast3(self):
16251580
# Test that data type is preserved . GH#5782
@@ -2974,20 +2929,9 @@ def test_loc_setitem_uint8_upcast(value):
29742929
# GH#26049
29752930

29762931
df = DataFrame([1, 2, 3, 4], columns=["col1"], dtype="uint8")
2977-
with tm.assert_produces_warning(FutureWarning, match="item of incompatible dtype"):
2932+
with pytest.raises(TypeError, match="Invalid value"):
29782933
df.loc[2, "col1"] = value # value that can't be held in uint8
29792934

2980-
if np_version_gt2 and isinstance(value, np.int16):
2981-
# Note, result type of uint8 + int16 is int16
2982-
# in numpy < 2, though, numpy would inspect the
2983-
# value and see that it could fit in an uint16, resulting in a uint16
2984-
dtype = "int16"
2985-
else:
2986-
dtype = "uint16"
2987-
2988-
expected = DataFrame([1, 2, 300, 4], columns=["col1"], dtype=dtype)
2989-
tm.assert_frame_equal(df, expected)
2990-
29912935

29922936
@pytest.mark.parametrize(
29932937
"fill_val,exp_dtype",

0 commit comments

Comments
 (0)