@@ -274,6 +274,28 @@ def test_to_records_dtype(self, kwargs, expected):
274274 result = df .to_records (** kwargs )
275275 tm .assert_almost_equal (result , expected )
276276
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+ df = DataFrame ({"A" : [1 , 2 ], "B" : [0.2 , 1.5 ], "C" : ["a" , "bc" ]})
291+
292+ if isinstance (expected , str ):
293+ with pytest .raises (ValueError , match = expected ):
294+ df .to_records (** kwargs )
295+ else :
296+ result = df .to_records (** kwargs )
297+ tm .assert_almost_equal (result , expected )
298+
277299 @pytest .mark .parametrize ("df,kwargs,expected" , [
278300 # MultiIndex in the index.
279301 (DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ], [7 , 8 , 9 ]],
0 commit comments