@@ -333,9 +333,7 @@ def test_no_prefix_string_cats_default_category(
333
333
):
334
334
dummies = DataFrame ({"a" : [1 , 0 , 0 ], "b" : [0 , 1 , 0 ]})
335
335
result = from_dummies (dummies , default_category = default_category )
336
- expected = DataFrame (expected )
337
- if using_infer_string :
338
- expected ["" ] = expected ["" ].astype ("str" )
336
+ expected = DataFrame (expected , dtype = dummies .columns .dtype )
339
337
tm .assert_frame_equal (result , expected )
340
338
341
339
@@ -449,3 +447,31 @@ def test_maintain_original_index():
449
447
result = from_dummies (df )
450
448
expected = DataFrame ({"" : list ("abca" )}, index = list ("abcd" ))
451
449
tm .assert_frame_equal (result , expected )
450
+
451
+
452
+ def test_int_columns_with_float_default ():
453
+ # https://github.com/pandas-dev/pandas/pull/60694
454
+ df = DataFrame (
455
+ {
456
+ 3 : [1 , 0 , 0 ],
457
+ 4 : [0 , 1 , 0 ],
458
+ },
459
+ )
460
+ with pytest .raises (ValueError , match = "Trying to coerce float values to integers" ):
461
+ from_dummies (df , default_category = 0.5 )
462
+
463
+
464
+ def test_object_dtype_preserved ():
465
+ # https://github.com/pandas-dev/pandas/pull/60694
466
+ # When the input has object dtype, the result should as
467
+ # well even when infer_string is True.
468
+ df = DataFrame (
469
+ {
470
+ "x" : [1 , 0 , 0 ],
471
+ "y" : [0 , 1 , 0 ],
472
+ },
473
+ )
474
+ df .columns = df .columns .astype ("object" )
475
+ result = from_dummies (df , default_category = "z" )
476
+ expected = DataFrame ({"" : ["x" , "y" , "z" ]}, dtype = "object" )
477
+ tm .assert_frame_equal (result , expected )
0 commit comments