@@ -125,6 +125,7 @@ def test_inf_upcast(self):
125
125
expected = pd .Float64Index ([1 , 2 , np .inf ])
126
126
tm .assert_index_equal (result , expected )
127
127
128
+ def test_inf_upcast_empty (self ):
128
129
# Test with np.inf in columns
129
130
df = DataFrame ()
130
131
df .loc [0 , 0 ] = 1
@@ -148,26 +149,29 @@ def test_setitem_dtype_upcast(self):
148
149
)
149
150
tm .assert_frame_equal (df , expected )
150
151
152
+ @pytest .mark .parametrize ("val" , [3.14 , "wxyz" ])
153
+ def test_setitem_dtype_upcast2 (self , val ):
154
+
151
155
# GH10280
152
156
df = DataFrame (
153
157
np .arange (6 , dtype = "int64" ).reshape (2 , 3 ),
154
158
index = list ("ab" ),
155
159
columns = ["foo" , "bar" , "baz" ],
156
160
)
157
161
158
- for val in [3.14 , "wxyz" ]:
159
- left = df .copy ()
160
- left .loc ["a" , "bar" ] = val
161
- right = DataFrame (
162
- [[0 , val , 2 ], [3 , 4 , 5 ]],
163
- index = list ("ab" ),
164
- columns = ["foo" , "bar" , "baz" ],
165
- )
162
+ left = df .copy ()
163
+ left .loc ["a" , "bar" ] = val
164
+ right = DataFrame (
165
+ [[0 , val , 2 ], [3 , 4 , 5 ]],
166
+ index = list ("ab" ),
167
+ columns = ["foo" , "bar" , "baz" ],
168
+ )
166
169
167
- tm .assert_frame_equal (left , right )
168
- assert is_integer_dtype (left ["foo" ])
169
- assert is_integer_dtype (left ["baz" ])
170
+ tm .assert_frame_equal (left , right )
171
+ assert is_integer_dtype (left ["foo" ])
172
+ assert is_integer_dtype (left ["baz" ])
170
173
174
+ def test_setitem_dtype_upcast3 (self ):
171
175
left = DataFrame (
172
176
np .arange (6 , dtype = "int64" ).reshape (2 , 3 ) / 10.0 ,
173
177
index = list ("ab" ),
@@ -195,6 +199,8 @@ def test_dups_fancy_indexing(self):
195
199
expected = Index (["b" , "a" , "a" ])
196
200
tm .assert_index_equal (result , expected )
197
201
202
+ def test_dups_fancy_indexing_across_dtypes (self ):
203
+
198
204
# across dtypes
199
205
df = DataFrame ([[1 , 2 , 1.0 , 2.0 , 3.0 , "foo" , "bar" ]], columns = list ("aaaaaaa" ))
200
206
df .head ()
@@ -208,6 +214,7 @@ def test_dups_fancy_indexing(self):
208
214
209
215
tm .assert_frame_equal (df , result )
210
216
217
+ def test_dups_fancy_indexing_not_in_order (self ):
211
218
# GH 3561, dups not in selected order
212
219
df = DataFrame (
213
220
{"test" : [5 , 7 , 9 , 11 ], "test1" : [4.0 , 5 , 6 , 7 ], "other" : list ("abcd" )},
@@ -232,6 +239,8 @@ def test_dups_fancy_indexing(self):
232
239
with pytest .raises (KeyError , match = "with any missing labels" ):
233
240
df .loc [rows ]
234
241
242
+ def test_dups_fancy_indexing_only_missing_label (self ):
243
+
235
244
# List containing only missing label
236
245
dfnu = DataFrame (np .random .randn (5 , 3 ), index = list ("AABCD" ))
237
246
with pytest .raises (
@@ -244,6 +253,8 @@ def test_dups_fancy_indexing(self):
244
253
245
254
# ToDo: check_index_type can be True after GH 11497
246
255
256
+ def test_dups_fancy_indexing_missing_label (self ):
257
+
247
258
# GH 4619; duplicate indexer with missing label
248
259
df = DataFrame ({"A" : [0 , 1 , 2 ]})
249
260
with pytest .raises (KeyError , match = "with any missing labels" ):
@@ -253,6 +264,8 @@ def test_dups_fancy_indexing(self):
253
264
with pytest .raises (KeyError , match = "with any missing labels" ):
254
265
df .loc [[0 , 8 , 0 ]]
255
266
267
+ def test_dups_fancy_indexing_non_unique (self ):
268
+
256
269
# non unique with non unique selector
257
270
df = DataFrame ({"test" : [5 , 7 , 9 , 11 ]}, index = ["A" , "A" , "B" , "C" ])
258
271
with pytest .raises (KeyError , match = "with any missing labels" ):
@@ -447,6 +460,7 @@ def test_multi_assign(self):
447
460
df2 .loc [mask , cols ] = dft .loc [mask , cols ].values
448
461
tm .assert_frame_equal (df2 , expected )
449
462
463
+ def test_multi_assign_broadcasting_rhs (self ):
450
464
# broadcasting on the rhs is required
451
465
df = DataFrame (
452
466
{
@@ -781,14 +795,16 @@ def test_non_reducing_slice(self, slc):
781
795
tslice_ = non_reducing_slice (slc )
782
796
assert isinstance (df .loc [tslice_ ], DataFrame )
783
797
784
- def test_list_slice (self ):
798
+ @pytest .mark .parametrize ("box" , [list , Series , np .array ])
799
+ def test_list_slice (self , box ):
785
800
# like dataframe getitem
786
- slices = [["A" ], Series (["A" ]), np .array (["A" ])]
801
+ subset = box (["A" ])
802
+
787
803
df = DataFrame ({"A" : [1 , 2 ], "B" : [3 , 4 ]}, index = ["A" , "B" ])
788
804
expected = pd .IndexSlice [:, ["A" ]]
789
- for subset in slices :
790
- result = non_reducing_slice (subset )
791
- tm .assert_frame_equal (df .loc [result ], df .loc [expected ])
805
+
806
+ result = non_reducing_slice (subset )
807
+ tm .assert_frame_equal (df .loc [result ], df .loc [expected ])
792
808
793
809
def test_maybe_numeric_slice (self ):
794
810
df = DataFrame ({"A" : [1 , 2 ], "B" : ["c" , "d" ], "C" : [True , False ]})
0 commit comments