File tree Expand file tree Collapse file tree 1 file changed +8
-6
lines changed Expand file tree Collapse file tree 1 file changed +8
-6
lines changed Original file line number Diff line number Diff line change @@ -418,15 +418,17 @@ def from_dummies(
418
418
[a, b, c]
419
419
Categories (3, object): [a, b, c]
420
420
"""
421
- # GH 8745
422
421
df = dummies .drop (columns = np .nan , errors = "ignore" ).astype (bool )
423
422
424
423
if (df .sum (axis = 1 ) > 1 ).any ():
425
424
raise ValueError ("Some rows belong to >1 category" )
426
425
427
- mult_by = np .arange (1 , df .shape [1 ] + 1 )
428
-
429
- codes = (df .astype (int ) * mult_by ).sum (axis = 1 ) - 1
426
+ mult_by = np .arange (df .shape [1 ]) + 1
427
+ # 000 000 0 -1
428
+ # 010 020 2 1
429
+ # 001 * 1,2,3 => 003 -> 3 -> 2 = correct codes
430
+ # 100 100 1 0
431
+ codes = (df * mult_by ).sum (axis = 1 ) - 1
430
432
codes [codes .isna ()] = - 1
431
433
return cls .from_codes (codes , df .columns .values , ordered = ordered )
432
434
@@ -462,8 +464,8 @@ def to_dummies(self, na_column=None) -> "DataFrame":
462
464
1 False True
463
465
2 False False
464
466
465
- >>> Categorical(["a", "b", np.nan]).to_dummies("c ")
466
- a b c
467
+ >>> Categorical(["a", "b", np.nan]).to_dummies("other ")
468
+ a b other
467
469
0 True False False
468
470
1 False True False
469
471
2 False False True
You can’t perform that action at this time.
0 commit comments