@@ -531,49 +531,38 @@ def test_astype_assignment(self, using_infer_string):
531
531
df_orig = DataFrame (
532
532
[["1" , "2" , "3" , ".4" , 5 , 6.0 , "foo" ]], columns = list ("ABCDEFG" )
533
533
)
534
+ df_orig [list ("ABCDG" )] = df_orig [list ("ABCDG" )].astype (object )
534
535
535
536
df = df_orig .copy ()
536
537
537
538
# with the enforcement of GH#45333 in 2.0, this setting is attempted inplace,
538
539
# so object dtype is retained
539
- if using_infer_string :
540
- with pytest .raises (TypeError , match = "Invalid value" ):
541
- df .iloc [:, 0 ] = df .iloc [:, 0 ].astype (np .int64 )
542
- else :
543
- df .iloc [:, 0 :2 ] = df .iloc [:, 0 :2 ].astype (np .int64 )
544
- expected = DataFrame (
545
- [[1 , 2 , "3" , ".4" , 5 , 6.0 , "foo" ]], columns = list ("ABCDEFG" )
546
- )
547
- expected ["A" ] = expected ["A" ].astype (object )
548
- expected ["B" ] = expected ["B" ].astype (object )
549
- tm .assert_frame_equal (df , expected )
540
+ df .iloc [:, 0 :2 ] = df .iloc [:, 0 :2 ].astype (np .int64 )
541
+ expected = DataFrame (
542
+ [[1 , 2 , "3" , ".4" , 5 , 6.0 , "foo" ]], columns = list ("ABCDEFG" )
543
+ )
544
+ expected [list ("CDG" )] = expected [list ("CDG" )].astype (object )
545
+ expected ["A" ] = expected ["A" ].astype (object )
546
+ expected ["B" ] = expected ["B" ].astype (object )
547
+ tm .assert_frame_equal (df , expected )
550
548
551
549
# GH5702 (loc)
552
550
df = df_orig .copy ()
553
- if using_infer_string :
554
- with pytest .raises (TypeError , match = "Invalid value" ):
555
- df .loc [:, "A" ] = df .loc [:, "A" ].astype (np .int64 )
556
- else :
557
- df .loc [:, "A" ] = df .loc [:, "A" ].astype (np .int64 )
558
- expected = DataFrame (
559
- [[1 , "2" , "3" , ".4" , 5 , 6.0 , "foo" ]], columns = list ("ABCDEFG" )
560
- )
561
- expected ["A" ] = expected ["A" ].astype (object )
562
- tm .assert_frame_equal (df , expected )
551
+ df .loc [:, "A" ] = df .loc [:, "A" ].astype (np .int64 )
552
+ expected = DataFrame (
553
+ [[1 , "2" , "3" , ".4" , 5 , 6.0 , "foo" ]], columns = list ("ABCDEFG" )
554
+ )
555
+ expected [list ("ABCDG" )] = expected [list ("ABCDG" )].astype (object )
556
+ tm .assert_frame_equal (df , expected )
563
557
564
558
df = df_orig .copy ()
565
559
566
- if using_infer_string :
567
- with pytest .raises (TypeError , match = "Invalid value" ):
568
- df .loc [:, ["B" , "C" ]] = df .loc [:, ["B" , "C" ]].astype (np .int64 )
569
- else :
570
- df .loc [:, ["B" , "C" ]] = df .loc [:, ["B" , "C" ]].astype (np .int64 )
571
- expected = DataFrame (
572
- [["1" , 2 , 3 , ".4" , 5 , 6.0 , "foo" ]], columns = list ("ABCDEFG" )
573
- )
574
- expected ["B" ] = expected ["B" ].astype (object )
575
- expected ["C" ] = expected ["C" ].astype (object )
576
- tm .assert_frame_equal (df , expected )
560
+ df .loc [:, ["B" , "C" ]] = df .loc [:, ["B" , "C" ]].astype (np .int64 )
561
+ expected = DataFrame (
562
+ [["1" , 2 , 3 , ".4" , 5 , 6.0 , "foo" ]], columns = list ("ABCDEFG" )
563
+ )
564
+ expected [list ("ABCDG" )] = expected [list ("ABCDG" )].astype (object )
565
+ tm .assert_frame_equal (df , expected )
577
566
578
567
def test_astype_assignment_full_replacements (self ):
579
568
# full replacements / no nans
0 commit comments