@@ -274,6 +274,29 @@ def test_to_records_dtype(self, kwargs, expected):
274
274
result = df .to_records (** kwargs )
275
275
tm .assert_almost_equal (result , expected )
276
276
277
+ @pytest .mark .parametrize ("kwargs,expected" , [
278
+ # Pass in a dtype instance.
279
+ (dict (column_dtypes = np .dtype ('unicode' )),
280
+ np .rec .array ([("0" , "1" , "0.2" , "a" ), ("1" , "2" , "1.5" , "bc" )],
281
+ dtype = [("index" , "<i8" ), ("A" , "<U" ),
282
+ ("B" , "<U" ), ("C" , "<U" )])),
283
+
284
+ # Names / indices not in mapping default to array dtype.
285
+ (dict (column_dtypes = {"A" : np .dtype ('int8' ), "B" : np .dtype ('float32' )}),
286
+ np .rec .array ([("0" , "1" , "0.2" , "a" ), ("1" , "2" , "1.5" , "bc" )],
287
+ dtype = [("index" , "<i8" ), ("A" , "i1" ),
288
+ ("B" , "<f4" ), ("C" , "O" )]))])
289
+ def test_to_records_dtype_dtype (self , kwargs , expected ):
290
+ # See gh-24895
291
+ df = DataFrame ({"A" : [1 , 2 ], "B" : [0.2 , 1.5 ], "C" : ["a" , "bc" ]})
292
+
293
+ if isinstance (expected , str ):
294
+ with pytest .raises (ValueError , match = expected ):
295
+ df .to_records (** kwargs )
296
+ else :
297
+ result = df .to_records (** kwargs )
298
+ tm .assert_almost_equal (result , expected )
299
+
277
300
@pytest .mark .parametrize ("df,kwargs,expected" , [
278
301
# MultiIndex in the index.
279
302
(DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ], [7 , 8 , 9 ]],
0 commit comments