@@ -263,31 +263,44 @@ def test_to_dict_wide(self):
263263 expected = {f"A_{ i :d} " : i for i in range (256 )}
264264 assert result == expected
265265
266- def test_to_dict_orient_dtype (self ):
267- # GH22620 & GH21256
268-
269- df = DataFrame (
270- {
271- "bool" : [True , True , False ],
272- "datetime" : [
266+ @pytest .mark .parametrize (
267+ "data,dtype" ,
268+ (
269+ ([True , True , False ], bool ),
270+ [
271+ [
273272 datetime (2018 , 1 , 1 ),
274273 datetime (2019 , 2 , 2 ),
275274 datetime (2020 , 3 , 3 ),
276275 ],
277- "float" : [1.0 , 2.0 , 3.0 ],
278- "int" : [1 , 2 , 3 ],
279- "str" : ["X" , "Y" , "Z" ],
280- }
281- )
276+ Timestamp ,
277+ ],
278+ [[1.0 , 2.0 , 3.0 ], float ],
279+ [[1 , 2 , 3 ], int ],
280+ [["X" , "Y" , "Z" ], str ],
281+ ),
282+ )
283+ def test_to_dict_orient_dtype (self , data , dtype ):
284+ # GH22620 & GH21256
282285
283- expected = {
284- "int" : int ,
285- "float" : float ,
286- "str" : str ,
287- "datetime" : Timestamp ,
288- "bool" : bool ,
289- }
286+ df = DataFrame ({"a" : data })
287+ d = df .to_dict (orient = "records" )
288+ assert all (type (record ["a" ]) is dtype for record in d )
289+
290+ @pytest .mark .parametrize (
291+ "data,expected_dtype" ,
292+ (
293+ [np .uint64 (2 ), int ],
294+ [np .int64 (- 9 ), int ],
295+ [np .float64 (1.1 ), float ],
296+ [np .bool_ (True ), bool ],
297+ [np .datetime64 ("2005-02-25" ), Timestamp ],
298+ ),
299+ )
300+ def test_to_dict_scalar_constructor_orient_dtype (self , data , expected_dtype ):
301+ # GH22620 & GH21256
290302
291- for df_dict in df .to_dict ("records" ):
292- result = {col : type (df_dict [col ]) for col in list (df .columns )}
293- assert result == expected
303+ df = DataFrame ({"a" : data }, index = [0 ])
304+ d = df .to_dict (orient = "records" )
305+ result = type (d [0 ]["a" ])
306+ assert result is expected_dtype
0 commit comments