@@ -588,32 +588,19 @@ def test_loc_modify_datetime(self):
588
588
589
589
tm .assert_frame_equal (df , expected )
590
590
591
- def test_loc_setitem_frame (self ):
592
- df = DataFrame (np .random .randn (4 , 4 ), index = list ("abcd" ), columns = list ("ABCD" ))
593
-
594
- df .loc ["a" , "A" ] = 1
595
- result = df .loc ["a" , "A" ]
596
- assert result == 1
597
-
598
- result = df .iloc [0 , 0 ]
599
- assert result == 1
600
-
601
- df .loc [:, "B" :"D" ] = 0
602
- expected = df .loc [:, "B" :"D" ]
603
- result = df .iloc [:, 1 :]
604
- tm .assert_frame_equal (result , expected )
605
-
606
- # GH 6254
607
- # setting issue
608
- df = DataFrame (index = [3 , 5 , 4 ], columns = ["A" ])
591
+ def test_loc_setitem_frame_with_reindex (self ):
592
+ # GH#6254 setting issue
593
+ df = DataFrame (index = [3 , 5 , 4 ], columns = ["A" ], dtype = float )
609
594
df .loc [[4 , 3 , 5 ], "A" ] = np .array ([1 , 2 , 3 ], dtype = "int64" )
610
- expected = DataFrame ({"A" : Series ([1 , 2 , 3 ], index = [4 , 3 , 5 ])}).reindex (
611
- index = [3 , 5 , 4 ]
612
- )
595
+
596
+ # setting integer values into a float dataframe with loc is inplace,
597
+ # so we retain float dtype
598
+ ser = Series ([2 , 3 , 1 ], index = [3 , 5 , 4 ], dtype = float )
599
+ expected = DataFrame ({"A" : ser })
613
600
tm .assert_frame_equal (df , expected )
614
601
615
- # GH 6252
616
- # setting with an empty frame
602
+ def test_loc_setitem_empty_frame ( self ):
603
+ # GH#6252 setting with an empty frame
617
604
keys1 = ["@" + str (i ) for i in range (5 )]
618
605
val1 = np .arange (5 , dtype = "int64" )
619
606
@@ -628,18 +615,39 @@ def test_loc_setitem_frame(self):
628
615
df ["B" ] = np .nan
629
616
df .loc [keys2 , "B" ] = val2
630
617
631
- expected = DataFrame (
632
- {"A" : Series (val1 , index = keys1 ), "B" : Series (val2 , index = keys2 )}
633
- ).reindex (index = index )
618
+ # Because df["A"] was initialized as float64, setting values into it
619
+ # is inplace, so that dtype is retained
620
+ sera = Series (val1 , index = keys1 , dtype = np .float64 )
621
+ serb = Series (val2 , index = keys2 )
622
+ expected = DataFrame ({"A" : sera , "B" : serb }).reindex (index = index )
634
623
tm .assert_frame_equal (df , expected )
635
624
625
+ def test_loc_setitem_frame (self ):
626
+ df = DataFrame (np .random .randn (4 , 4 ), index = list ("abcd" ), columns = list ("ABCD" ))
627
+
628
+ result = df .iloc [0 , 0 ]
629
+
630
+ df .loc ["a" , "A" ] = 1
631
+ result = df .loc ["a" , "A" ]
632
+ assert result == 1
633
+
634
+ result = df .iloc [0 , 0 ]
635
+ assert result == 1
636
+
637
+ df .loc [:, "B" :"D" ] = 0
638
+ expected = df .loc [:, "B" :"D" ]
639
+ result = df .iloc [:, 1 :]
640
+ tm .assert_frame_equal (result , expected )
641
+
642
+ def test_loc_setitem_frame_nan_int_coercion_invalid (self ):
636
643
# GH 8669
637
644
# invalid coercion of nan -> int
638
645
df = DataFrame ({"A" : [1 , 2 , 3 ], "B" : np .nan })
639
646
df .loc [df .B > df .A , "B" ] = df .A
640
647
expected = DataFrame ({"A" : [1 , 2 , 3 ], "B" : np .nan })
641
648
tm .assert_frame_equal (df , expected )
642
649
650
+ def test_loc_setitem_frame_mixed_labels (self ):
643
651
# GH 6546
644
652
# setting with mixed labels
645
653
df = DataFrame ({1 : [1 , 2 ], 2 : [3 , 4 ], "a" : ["a" , "b" ]})
@@ -1063,8 +1071,15 @@ def test_loc_setitem_str_to_small_float_conversion_type(self):
1063
1071
expected = DataFrame (col_data , columns = ["A" ], dtype = object )
1064
1072
tm .assert_frame_equal (result , expected )
1065
1073
1066
- # change the dtype of the elements from object to float one by one
1074
+ # assigning with loc/iloc attempts to set the values inplace, which
1075
+ # in this case is succesful
1067
1076
result .loc [result .index , "A" ] = [float (x ) for x in col_data ]
1077
+ expected = DataFrame (col_data , columns = ["A" ], dtype = float ).astype (object )
1078
+ tm .assert_frame_equal (result , expected )
1079
+
1080
+ # assigning the entire column using __setitem__ swaps in the new array
1081
+ # GH#???
1082
+ result ["A" ] = [float (x ) for x in col_data ]
1068
1083
expected = DataFrame (col_data , columns = ["A" ], dtype = float )
1069
1084
tm .assert_frame_equal (result , expected )
1070
1085
@@ -1219,7 +1234,9 @@ def test_loc_setitem_datetimeindex_tz(self, idxer, tz_naive_fixture):
1219
1234
tz = tz_naive_fixture
1220
1235
idx = date_range (start = "2015-07-12" , periods = 3 , freq = "H" , tz = tz )
1221
1236
expected = DataFrame (1.2 , index = idx , columns = ["var" ])
1222
- result = DataFrame (index = idx , columns = ["var" ])
1237
+ # if result started off with object dtype, tehn the .loc.__setitem__
1238
+ # below would retain object dtype
1239
+ result = DataFrame (index = idx , columns = ["var" ], dtype = np .float64 )
1223
1240
result .loc [:, idxer ] = expected
1224
1241
tm .assert_frame_equal (result , expected )
1225
1242
0 commit comments