Skip to content

Commit bea24ca

Browse files
committed
wip
1 parent 996546f commit bea24ca

File tree

2 files changed

+39
-78
lines changed

2 files changed

+39
-78
lines changed

pandas/tests/series/indexing/test_setitem.py

Lines changed: 38 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -596,11 +596,14 @@ def test_setitem_enlarge_with_na(
596596
):
597597
# GH#32346
598598
ser = Series([1, 2], dtype=dtype)
599-
with tm.assert_produces_warning(warn, match="incompatible dtype"):
599+
if warn:
600+
with pytest.raises(TypeError, match="Invalid value"):
601+
ser[indexer] = na
602+
else:
600603
ser[indexer] = na
601-
expected_values = [1, target_na] if indexer == 1 else [1, 2, target_na]
602-
expected = Series(expected_values, dtype=target_dtype)
603-
tm.assert_series_equal(ser, expected)
604+
expected_values = [1, target_na] if indexer == 1 else [1, 2, target_na]
605+
expected = Series(expected_values, dtype=target_dtype)
606+
tm.assert_series_equal(ser, expected)
604607

605608
def test_setitem_enlargement_object_none(self, nulls_fixture, using_infer_string):
606609
# GH#48665
@@ -694,14 +697,8 @@ def test_setitem_non_bool_into_bool(self, val, indexer_sli, unique):
694697
if not unique:
695698
ser.index = [1, 1]
696699

697-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
700+
with pytest.raises(TypeError, match="Invalid value"):
698701
indexer_sli(ser)[1] = val
699-
assert type(ser.iloc[1]) == type(val)
700-
701-
expected = Series([True, val], dtype=object, index=ser.index)
702-
if not unique and indexer_sli is not tm.iloc:
703-
expected = Series([val, val], dtype=object, index=[1, 1])
704-
tm.assert_series_equal(ser, expected)
705702

706703
def test_setitem_boolean_array_into_npbool(self):
707704
# GH#45462
@@ -712,10 +709,8 @@ def test_setitem_boolean_array_into_npbool(self):
712709
ser[:2] = arr[:2] # no NAs -> can set inplace
713710
assert ser._values is values
714711

715-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
712+
with pytest.raises(TypeError, match="Invalid value"):
716713
ser[1:] = arr[1:] # has an NA -> cast to boolean dtype
717-
expected = Series(arr)
718-
tm.assert_series_equal(ser, expected)
719714

720715

721716
class SetitemCastingEquivalents:
@@ -763,36 +758,36 @@ def test_int_key(self, obj, key, expected, warn, val, indexer_sli, is_inplace):
763758
if not isinstance(key, int):
764759
pytest.skip("Not relevant for int key")
765760

766-
with tm.assert_produces_warning(warn, match="incompatible dtype"):
761+
with pytest.raises(TypeError, match="Invalid value"):
767762
self.check_indexer(obj, key, expected, val, indexer_sli, is_inplace)
768763

769764
if indexer_sli is tm.loc:
770-
with tm.assert_produces_warning(warn, match="incompatible dtype"):
765+
with pytest.raises(TypeError, match="Invalid value"):
771766
self.check_indexer(obj, key, expected, val, tm.at, is_inplace)
772767
elif indexer_sli is tm.iloc:
773-
with tm.assert_produces_warning(warn, match="incompatible dtype"):
768+
with pytest.raises(TypeError, match="Invalid value"):
774769
self.check_indexer(obj, key, expected, val, tm.iat, is_inplace)
775770

776771
rng = range(key, key + 1)
777-
with tm.assert_produces_warning(warn, match="incompatible dtype"):
772+
with pytest.raises(TypeError, match="Invalid value"):
778773
self.check_indexer(obj, rng, expected, val, indexer_sli, is_inplace)
779774

780775
if indexer_sli is not tm.loc:
781776
# Note: no .loc because that handles slice edges differently
782777
slc = slice(key, key + 1)
783-
with tm.assert_produces_warning(warn, match="incompatible dtype"):
778+
with pytest.raises(TypeError, match="Invalid value"):
784779
self.check_indexer(obj, slc, expected, val, indexer_sli, is_inplace)
785780

786781
ilkey = [key]
787-
with tm.assert_produces_warning(warn, match="incompatible dtype"):
782+
with pytest.raises(TypeError, match="Invalid value"):
788783
self.check_indexer(obj, ilkey, expected, val, indexer_sli, is_inplace)
789784

790785
indkey = np.array(ilkey)
791-
with tm.assert_produces_warning(warn, match="incompatible dtype"):
786+
with pytest.raises(TypeError, match="Invalid value"):
792787
self.check_indexer(obj, indkey, expected, val, indexer_sli, is_inplace)
793788

794789
genkey = (x for x in [key])
795-
with tm.assert_produces_warning(warn, match="incompatible dtype"):
790+
with pytest.raises(TypeError, match="Invalid value"):
796791
self.check_indexer(obj, genkey, expected, val, indexer_sli, is_inplace)
797792

798793
def test_slice_key(self, obj, key, expected, warn, val, indexer_sli, is_inplace):
@@ -801,19 +796,19 @@ def test_slice_key(self, obj, key, expected, warn, val, indexer_sli, is_inplace)
801796

802797
if indexer_sli is not tm.loc:
803798
# Note: no .loc because that handles slice edges differently
804-
with tm.assert_produces_warning(warn, match="incompatible dtype"):
799+
with pytest.raises(TypeError, match="Invalid value"):
805800
self.check_indexer(obj, key, expected, val, indexer_sli, is_inplace)
806801

807802
ilkey = list(range(len(obj)))[key]
808-
with tm.assert_produces_warning(warn, match="incompatible dtype"):
803+
with pytest.raises(TypeError, match="Invalid value"):
809804
self.check_indexer(obj, ilkey, expected, val, indexer_sli, is_inplace)
810805

811806
indkey = np.array(ilkey)
812-
with tm.assert_produces_warning(warn, match="incompatible dtype"):
807+
with pytest.raises(TypeError, match="Invalid value"):
813808
self.check_indexer(obj, indkey, expected, val, indexer_sli, is_inplace)
814809

815810
genkey = (x for x in indkey)
816-
with tm.assert_produces_warning(warn, match="incompatible dtype"):
811+
with pytest.raises(TypeError, match="Invalid value"):
817812
self.check_indexer(obj, genkey, expected, val, indexer_sli, is_inplace)
818813

819814
def test_mask_key(self, obj, key, expected, warn, val, indexer_sli):
@@ -829,7 +824,7 @@ def test_mask_key(self, obj, key, expected, warn, val, indexer_sli):
829824
indexer_sli(obj)[mask] = val
830825
return
831826

832-
with tm.assert_produces_warning(warn, match="incompatible dtype"):
827+
with pytest.raises(TypeError, match="Invalid value"):
833828
indexer_sli(obj)[mask] = val
834829
tm.assert_series_equal(obj, expected)
835830

@@ -1185,11 +1180,8 @@ def test_setitem_example(self):
11851180
obj = Series(idx)
11861181
val = Interval(0.5, 1.5)
11871182

1188-
with tm.assert_produces_warning(
1189-
FutureWarning, match="Setting an item of incompatible dtype"
1190-
):
1183+
with pytest.raises(TypeError, match="Invalid value"):
11911184
obj[0] = val
1192-
assert obj.dtype == "Interval[float64, right]"
11931185

11941186
@pytest.fixture
11951187
def obj(self):
@@ -1583,55 +1575,37 @@ def test_20643():
15831575
# closed by GH#45121
15841576
orig = Series([0, 1, 2], index=["a", "b", "c"])
15851577

1586-
expected = Series([0, 2.7, 2], index=["a", "b", "c"])
1587-
15881578
ser = orig.copy()
1589-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
1579+
with pytest.raises(TypeError, match="Invalid value"):
15901580
ser.at["b"] = 2.7
1591-
tm.assert_series_equal(ser, expected)
15921581

1593-
ser = orig.copy()
1594-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
1582+
with pytest.raises(TypeError, match="Invalid value"):
15951583
ser.loc["b"] = 2.7
1596-
tm.assert_series_equal(ser, expected)
15971584

1598-
ser = orig.copy()
1599-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
1585+
with pytest.raises(TypeError, match="Invalid value"):
16001586
ser["b"] = 2.7
1601-
tm.assert_series_equal(ser, expected)
16021587

16031588
ser = orig.copy()
1604-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
1589+
with pytest.raises(TypeError, match="Invalid value"):
16051590
ser.iat[1] = 2.7
1606-
tm.assert_series_equal(ser, expected)
16071591

1608-
ser = orig.copy()
1609-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
1592+
with pytest.raises(TypeError, match="Invalid value"):
16101593
ser.iloc[1] = 2.7
1611-
tm.assert_series_equal(ser, expected)
16121594

16131595
orig_df = orig.to_frame("A")
1614-
expected_df = expected.to_frame("A")
16151596

16161597
df = orig_df.copy()
1617-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
1598+
with pytest.raises(TypeError, match="Invalid value"):
16181599
df.at["b", "A"] = 2.7
1619-
tm.assert_frame_equal(df, expected_df)
16201600

1621-
df = orig_df.copy()
1622-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
1601+
with pytest.raises(TypeError, match="Invalid value"):
16231602
df.loc["b", "A"] = 2.7
1624-
tm.assert_frame_equal(df, expected_df)
16251603

1626-
df = orig_df.copy()
1627-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
1604+
with pytest.raises(TypeError, match="Invalid value"):
16281605
df.iloc[1, 0] = 2.7
1629-
tm.assert_frame_equal(df, expected_df)
16301606

1631-
df = orig_df.copy()
1632-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
1607+
with pytest.raises(TypeError, match="Invalid value"):
16331608
df.iat[1, 0] = 2.7
1634-
tm.assert_frame_equal(df, expected_df)
16351609

16361610

16371611
def test_20643_comment():
@@ -1653,35 +1627,23 @@ def test_15413():
16531627
# fixed by GH#45121
16541628
ser = Series([1, 2, 3])
16551629

1656-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
1630+
with pytest.raises(TypeError, match="Invalid value"):
16571631
ser[ser == 2] += 0.5
1658-
expected = Series([1, 2.5, 3])
1659-
tm.assert_series_equal(ser, expected)
16601632

1661-
ser = Series([1, 2, 3])
1662-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
1633+
with pytest.raises(TypeError, match="Invalid value"):
16631634
ser[1] += 0.5
1664-
tm.assert_series_equal(ser, expected)
16651635

1666-
ser = Series([1, 2, 3])
1667-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
1636+
with pytest.raises(TypeError, match="Invalid value"):
16681637
ser.loc[1] += 0.5
1669-
tm.assert_series_equal(ser, expected)
16701638

1671-
ser = Series([1, 2, 3])
1672-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
1639+
with pytest.raises(TypeError, match="Invalid value"):
16731640
ser.iloc[1] += 0.5
1674-
tm.assert_series_equal(ser, expected)
16751641

1676-
ser = Series([1, 2, 3])
1677-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
1642+
with pytest.raises(TypeError, match="Invalid value"):
16781643
ser.iat[1] += 0.5
1679-
tm.assert_series_equal(ser, expected)
16801644

1681-
ser = Series([1, 2, 3])
1682-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
1645+
with pytest.raises(TypeError, match="Invalid value"):
16831646
ser.at[1] += 0.5
1684-
tm.assert_series_equal(ser, expected)
16851647

16861648

16871649
def test_32878_int_itemsize():

pandas/tests/series/methods/test_fillna.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,8 @@ def test_fillna_consistency(self):
159159

160160
# assignment
161161
ser2 = ser.copy()
162-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
162+
with pytest.raises(TypeError, match="Invalid value"):
163163
ser2[1] = "foo"
164-
tm.assert_series_equal(ser2, expected)
165164

166165
def test_timedelta_fillna(self, frame_or_series, unit):
167166
# GH#3371

0 commit comments

Comments
 (0)