2
2
3
3
from collections import namedtuple
4
4
from datetime import (
5
- date ,
6
5
datetime ,
7
6
time ,
8
7
timedelta ,
16
15
from pandas ._config import using_pyarrow_string_dtype
17
16
18
17
from pandas ._libs import index as libindex
19
- from pandas .compat .numpy import np_version_gt2
20
18
from pandas .errors import IndexingError
21
19
22
20
import pandas as pd
@@ -379,17 +377,6 @@ def test_loc_setitem_slice(self):
379
377
)
380
378
tm .assert_frame_equal (df1 , expected )
381
379
382
- # assigning a new type should get the inferred type
383
- df2 = DataFrame ({"a" : [0 , 1 , 1 ], "b" : [100 , 200 , 300 ]}, dtype = "uint64" )
384
- ix = df1 ["a" ] == 1
385
- newb2 = df2 .loc [ix , "b" ]
386
- with tm .assert_produces_warning (
387
- FutureWarning , match = "item of incompatible dtype"
388
- ):
389
- 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 )
392
-
393
380
def test_loc_setitem_dtype (self ):
394
381
# GH31340
395
382
df = DataFrame ({"id" : ["A" ], "a" : [1.2 ], "b" : [0.0 ], "c" : [- 2.5 ]})
@@ -565,62 +552,6 @@ def frame_for_consistency(self):
565
552
}
566
553
)
567
554
568
- @pytest .mark .parametrize (
569
- "val" ,
570
- [0 , np .array (0 , dtype = np .int64 ), np .array ([0 , 0 , 0 , 0 , 0 ], dtype = np .int64 )],
571
- )
572
- def test_loc_setitem_consistency (self , frame_for_consistency , val ):
573
- # GH 6149
574
- # 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
- )
581
- df = frame_for_consistency .copy ()
582
- with tm .assert_produces_warning (FutureWarning , match = "incompatible dtype" ):
583
- df .loc [:, "date" ] = val
584
- tm .assert_frame_equal (df , expected )
585
-
586
- def test_loc_setitem_consistency_dt64_to_str (self , frame_for_consistency ):
587
- # GH 6149
588
- # coerce similarly for setitem and loc when rows have a null-slice
589
-
590
- expected = DataFrame (
591
- {
592
- "date" : Series ("foo" , index = range (5 )),
593
- "val" : Series (range (5 ), dtype = np .int64 ),
594
- }
595
- )
596
- df = frame_for_consistency .copy ()
597
- with tm .assert_produces_warning (FutureWarning , match = "incompatible dtype" ):
598
- df .loc [:, "date" ] = "foo"
599
- tm .assert_frame_equal (df , expected )
600
-
601
- def test_loc_setitem_consistency_dt64_to_float (self , frame_for_consistency ):
602
- # GH 6149
603
- # 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
- )
610
- df = frame_for_consistency .copy ()
611
- with tm .assert_produces_warning (FutureWarning , match = "incompatible dtype" ):
612
- df .loc [:, "date" ] = 1.0
613
- tm .assert_frame_equal (df , expected )
614
-
615
- def test_loc_setitem_consistency_single_row (self ):
616
- # GH 15494
617
- # setting on frame with single row
618
- df = DataFrame ({"date" : Series ([Timestamp ("20180101" )])})
619
- with tm .assert_produces_warning (FutureWarning , match = "incompatible dtype" ):
620
- df .loc [:, "date" ] = "string"
621
- expected = DataFrame ({"date" : Series (["string" ])})
622
- tm .assert_frame_equal (df , expected )
623
-
624
555
def test_loc_setitem_consistency_empty (self ):
625
556
# empty (essentially noops)
626
557
# before the enforcement of #45333 in 2.0, the loc.setitem here would
@@ -675,16 +606,16 @@ def test_loc_setitem_consistency_slice_column_len(self):
675
606
- df .loc [:, ("Respondent" , "StartDate" )]
676
607
)
677
608
678
- # timedelta64[m] -> float, so this cannot be done inplace, so
679
- # no warning
680
- with tm .assert_produces_warning (FutureWarning , match = "incompatible dtype" ):
681
- df .loc [:, ("Respondent" , "Duration" )] = df .loc [
682
- :, ("Respondent" , "Duration" )
683
- ] / Timedelta (60_000_000_000 )
684
-
685
609
expected = Series (
686
- [23.0 , 12.0 , 14.0 , 36.0 ], index = df .index , name = ("Respondent" , "Duration" )
687
- )
610
+ [
611
+ timedelta (minutes = 23 ),
612
+ timedelta (minutes = 12 ),
613
+ timedelta (minutes = 14 ),
614
+ timedelta (minutes = 36 ),
615
+ ],
616
+ index = df .index ,
617
+ name = ("Respondent" , "Duration" ),
618
+ ).astype ("timedelta64[s]" )
688
619
tm .assert_series_equal (df [("Respondent" , "Duration" )], expected )
689
620
690
621
@pytest .mark .parametrize ("unit" , ["Y" , "M" , "D" , "h" , "m" , "s" , "ms" , "us" ])
@@ -1409,18 +1340,6 @@ def test_loc_setitem_listlike_with_timedelta64index(self, indexer, expected):
1409
1340
1410
1341
tm .assert_frame_equal (expected , df )
1411
1342
1412
- def test_loc_setitem_categorical_values_partial_column_slice (self ):
1413
- # Assigning a Category to parts of a int/... column uses the values of
1414
- # the Categorical
1415
- 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
- ):
1420
- df .loc [1 :2 , "a" ] = Categorical (["b" , "b" ], categories = ["a" , "b" ])
1421
- df .loc [2 :3 , "b" ] = Categorical (["b" , "b" ], categories = ["a" , "b" ])
1422
- tm .assert_frame_equal (df , exp )
1423
-
1424
1343
def test_loc_setitem_single_row_categorical (self , using_infer_string ):
1425
1344
# GH#25495
1426
1345
df = DataFrame ({"Alpha" : ["a" ], "Numeric" : [0 ]})
@@ -1440,16 +1359,6 @@ def test_loc_setitem_single_row_categorical(self, using_infer_string):
1440
1359
df ["Alpha" ] = categories
1441
1360
tm .assert_series_equal (df ["Alpha" ], Series (categories , name = "Alpha" ))
1442
1361
1443
- def test_loc_setitem_datetime_coercion (self ):
1444
- # GH#1048
1445
- df = DataFrame ({"c" : [Timestamp ("2010-10-01" )] * 3 })
1446
- df .loc [0 :1 , "c" ] = np .datetime64 ("2008-08-08" )
1447
- assert Timestamp ("2008-08-08" ) == df .loc [0 , "c" ]
1448
- assert Timestamp ("2008-08-08" ) == df .loc [1 , "c" ]
1449
- with tm .assert_produces_warning (FutureWarning , match = "incompatible dtype" ):
1450
- df .loc [2 , "c" ] = date (2005 , 5 , 5 )
1451
- assert Timestamp ("2005-05-05" ).date () == df .loc [2 , "c" ]
1452
-
1453
1362
@pytest .mark .parametrize ("idxer" , ["var" , ["var" ]])
1454
1363
def test_loc_setitem_datetimeindex_tz (self , idxer , tz_naive_fixture ):
1455
1364
# GH#11365
@@ -1459,11 +1368,8 @@ def test_loc_setitem_datetimeindex_tz(self, idxer, tz_naive_fixture):
1459
1368
# if result started off with object dtype, then the .loc.__setitem__
1460
1369
# below would retain object dtype
1461
1370
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
- ):
1465
- # See https://github.com/pandas-dev/pandas/issues/56223
1466
- result .loc [:, idxer ] = expected
1371
+ # See https://github.com/pandas-dev/pandas/issues/56223
1372
+ result .loc [:, ["var" ]] = expected
1467
1373
tm .assert_frame_equal (result , expected )
1468
1374
1469
1375
def test_loc_setitem_time_key (self ):
@@ -1605,22 +1511,6 @@ def test_loc_setitem_single_column_mixed(self, using_infer_string):
1605
1511
).values
1606
1512
tm .assert_almost_equal (df ["str" ].values , expected )
1607
1513
1608
- def test_loc_setitem_cast2 (self ):
1609
- # GH#7704
1610
- # dtype conversion on setting
1611
- df = DataFrame (np .random .default_rng (2 ).random ((30 , 3 )), columns = tuple ("ABC" ))
1612
- df ["event" ] = np .nan
1613
- with tm .assert_produces_warning (
1614
- FutureWarning , match = "item of incompatible dtype"
1615
- ):
1616
- 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 )
1623
-
1624
1514
def test_loc_setitem_cast3 (self ):
1625
1515
# Test that data type is preserved . GH#5782
1626
1516
df = DataFrame ({"one" : np .arange (6 , dtype = np .int8 )})
@@ -2969,26 +2859,6 @@ def test_loc_getitem_nullable_index_with_duplicates():
2969
2859
tm .assert_series_equal (res , expected )
2970
2860
2971
2861
2972
- @pytest .mark .parametrize ("value" , [300 , np .uint16 (300 ), np .int16 (300 )])
2973
- def test_loc_setitem_uint8_upcast (value ):
2974
- # GH#26049
2975
-
2976
- df = DataFrame ([1 , 2 , 3 , 4 ], columns = ["col1" ], dtype = "uint8" )
2977
- with tm .assert_produces_warning (FutureWarning , match = "item of incompatible dtype" ):
2978
- df .loc [2 , "col1" ] = value # value that can't be held in uint8
2979
-
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
-
2991
-
2992
2862
@pytest .mark .parametrize (
2993
2863
"fill_val,exp_dtype" ,
2994
2864
[
0 commit comments