25
25
Timestamp ,
26
26
date_range ,
27
27
isna ,
28
- notna ,
29
28
to_datetime ,
30
29
)
31
30
import pandas ._testing as tm
@@ -833,13 +832,8 @@ def test_setitem_single_column_mixed_datetime(self):
833
832
tm .assert_series_equal (result , expected )
834
833
835
834
# GH#16674 iNaT is treated as an integer when given by the user
836
- with tm .assert_produces_warning (
837
- FutureWarning , match = "Setting an item of incompatible dtype"
838
- ):
835
+ with pytest .raises (TypeError , match = "Invalid value" ):
839
836
df .loc ["b" , "timestamp" ] = iNaT
840
- assert not isna (df .loc ["b" , "timestamp" ])
841
- assert df ["timestamp" ].dtype == np .object_
842
- assert df .loc ["b" , "timestamp" ] == iNaT
843
837
844
838
# allow this syntax (as of GH#3216)
845
839
df .loc ["c" , "timestamp" ] = np .nan
@@ -851,35 +845,11 @@ def test_setitem_single_column_mixed_datetime(self):
851
845
852
846
def test_setitem_mixed_datetime (self ):
853
847
# GH 9336
854
- expected = DataFrame (
855
- {
856
- "a" : [0 , 0 , 0 , 0 , 13 , 14 ],
857
- "b" : [
858
- datetime (2012 , 1 , 1 ),
859
- 1 ,
860
- "x" ,
861
- "y" ,
862
- datetime (2013 , 1 , 1 ),
863
- datetime (2014 , 1 , 1 ),
864
- ],
865
- }
866
- )
867
848
df = DataFrame (0 , columns = list ("ab" ), index = range (6 ))
868
849
df ["b" ] = pd .NaT
869
850
df .loc [0 , "b" ] = datetime (2012 , 1 , 1 )
870
- with tm .assert_produces_warning (
871
- FutureWarning , match = "Setting an item of incompatible dtype"
872
- ):
851
+ with pytest .raises (TypeError , match = "Invalid value" ):
873
852
df .loc [1 , "b" ] = 1
874
- df .loc [[2 , 3 ], "b" ] = "x" , "y"
875
- A = np .array (
876
- [
877
- [13 , np .datetime64 ("2013-01-01T00:00:00" )],
878
- [14 , np .datetime64 ("2014-01-01T00:00:00" )],
879
- ]
880
- )
881
- df .loc [[4 , 5 ], ["a" , "b" ]] = A
882
- tm .assert_frame_equal (df , expected )
883
853
884
854
def test_setitem_frame_float (self , float_frame ):
885
855
piece = float_frame .loc [float_frame .index [:2 ], ["A" , "B" ]]
@@ -936,12 +906,8 @@ def test_setitem_frame_upcast(self):
936
906
# needs upcasting
937
907
df = DataFrame ([[1 , 2 , "foo" ], [3 , 4 , "bar" ]], columns = ["A" , "B" , "C" ])
938
908
df2 = df .copy ()
939
- with tm . assert_produces_warning ( FutureWarning , match = "incompatible dtype " ):
909
+ with pytest . raises ( TypeError , match = "Invalid value " ):
940
910
df2 .loc [:, ["A" , "B" ]] = df .loc [:, ["A" , "B" ]] + 0.5
941
- expected = df .reindex (columns = ["A" , "B" ])
942
- expected += 0.5
943
- expected ["C" ] = df ["C" ]
944
- tm .assert_frame_equal (df2 , expected )
945
911
946
912
def test_setitem_frame_align (self , float_frame ):
947
913
piece = float_frame .loc [float_frame .index [:2 ], ["A" , "B" ]]
@@ -1366,12 +1332,8 @@ def test_loc_setitem_rhs_frame(self, idxr, val):
1366
1332
# GH#47578
1367
1333
df = DataFrame ({"a" : [1 , 2 ]})
1368
1334
1369
- with tm .assert_produces_warning (
1370
- FutureWarning , match = "Setting an item of incompatible dtype"
1371
- ):
1335
+ with pytest .raises (TypeError , match = "Invalid value" ):
1372
1336
df .loc [:, idxr ] = DataFrame ({"a" : [val , 11 ]}, index = [1 , 2 ])
1373
- expected = DataFrame ({"a" : [np .nan , val ]})
1374
- tm .assert_frame_equal (df , expected )
1375
1337
1376
1338
def test_iloc_setitem_enlarge_no_warning (self ):
1377
1339
# GH#47381
@@ -1579,18 +1541,9 @@ def test_setitem(self):
1579
1541
# With NaN: because uint64 has no NaN element,
1580
1542
# the column should be cast to object.
1581
1543
df2 = df .copy ()
1582
- with tm . assert_produces_warning ( FutureWarning , match = "incompatible dtype " ):
1544
+ with pytest . raises ( TypeError , match = "Invalid value " ):
1583
1545
df2 .iloc [1 , 1 ] = pd .NaT
1584
1546
df2 .iloc [1 , 2 ] = pd .NaT
1585
- result = df2 ["B" ]
1586
- tm .assert_series_equal (notna (result ), Series ([True , False , True ], name = "B" ))
1587
- tm .assert_series_equal (
1588
- df2 .dtypes ,
1589
- Series (
1590
- [np .dtype ("uint64" ), np .dtype ("O" ), np .dtype ("O" )],
1591
- index = ["A" , "B" , "C" ],
1592
- ),
1593
- )
1594
1547
1595
1548
1596
1549
def test_object_casting_indexing_wraps_datetimelike ():
@@ -1926,22 +1879,30 @@ def test_add_new_column_infer_string():
1926
1879
class TestSetitemValidation :
1927
1880
# This is adapted from pandas/tests/arrays/masked/test_indexing.py
1928
1881
# but checks for warnings instead of errors.
1929
- def _check_setitem_invalid (self , df , invalid , indexer , warn ):
1930
- msg = "Setting an item of incompatible dtype is deprecated"
1931
- msg = re .escape (msg )
1932
-
1882
+ def _check_setitem_invalid (self , df , invalid , indexer ):
1933
1883
orig_df = df .copy ()
1934
1884
1935
1885
# iloc
1936
- with tm . assert_produces_warning ( warn , match = msg ):
1886
+ with pytest . raises ( TypeError , match = "Invalid value" ):
1937
1887
df .iloc [indexer , 0 ] = invalid
1938
1888
df = orig_df .copy ()
1939
1889
1940
1890
# loc
1941
- with tm . assert_produces_warning ( warn , match = msg ):
1891
+ with pytest . raises ( TypeError , match = "Invalid value" ):
1942
1892
df .loc [indexer , "a" ] = invalid
1943
1893
df = orig_df .copy ()
1944
1894
1895
+ def _check_setitem_valid (self , df , value , indexer ):
1896
+ orig_df = df .copy ()
1897
+
1898
+ # iloc
1899
+ df .iloc [indexer , 0 ] = value
1900
+ df = orig_df .copy ()
1901
+
1902
+ # loc
1903
+ df .loc [indexer , "a" ] = value
1904
+ df = orig_df .copy ()
1905
+
1945
1906
_invalid_scalars = [
1946
1907
1 + 2j ,
1947
1908
"True" ,
@@ -1959,20 +1920,19 @@ def _check_setitem_invalid(self, df, invalid, indexer, warn):
1959
1920
@pytest .mark .parametrize ("indexer" , _indexers )
1960
1921
def test_setitem_validation_scalar_bool (self , invalid , indexer ):
1961
1922
df = DataFrame ({"a" : [True , False , False ]}, dtype = "bool" )
1962
- self ._check_setitem_invalid (df , invalid , indexer , FutureWarning )
1923
+ self ._check_setitem_invalid (df , invalid , indexer )
1963
1924
1964
1925
@pytest .mark .parametrize ("invalid" , _invalid_scalars + [True , 1.5 , np .float64 (1.5 )])
1965
1926
@pytest .mark .parametrize ("indexer" , _indexers )
1966
1927
def test_setitem_validation_scalar_int (self , invalid , any_int_numpy_dtype , indexer ):
1967
1928
df = DataFrame ({"a" : [1 , 2 , 3 ]}, dtype = any_int_numpy_dtype )
1968
1929
if isna (invalid ) and invalid is not pd .NaT and not np .isnat (invalid ):
1969
- warn = None
1930
+ self . _check_setitem_valid ( df , invalid , indexer )
1970
1931
else :
1971
- warn = FutureWarning
1972
- self ._check_setitem_invalid (df , invalid , indexer , warn )
1932
+ self ._check_setitem_invalid (df , invalid , indexer )
1973
1933
1974
1934
@pytest .mark .parametrize ("invalid" , _invalid_scalars + [True ])
1975
1935
@pytest .mark .parametrize ("indexer" , _indexers )
1976
1936
def test_setitem_validation_scalar_float (self , invalid , float_numpy_dtype , indexer ):
1977
1937
df = DataFrame ({"a" : [1 , 2 , None ]}, dtype = float_numpy_dtype )
1978
- self ._check_setitem_invalid (df , invalid , indexer , FutureWarning )
1938
+ self ._check_setitem_invalid (df , invalid , indexer )
0 commit comments