Skip to content

Commit edacb70

Browse files
committed
dummies review comments
1 parent d92226f commit edacb70

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

pandas/core/arrays/categorical.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -418,15 +418,17 @@ def from_dummies(
418418
[a, b, c]
419419
Categories (3, object): [a, b, c]
420420
"""
421-
# GH 8745
422421
df = dummies.drop(columns=np.nan, errors="ignore").astype(bool)
423422

424423
if (df.sum(axis=1) > 1).any():
425424
raise ValueError("Some rows belong to >1 category")
426425

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
430432
codes[codes.isna()] = -1
431433
return cls.from_codes(codes, df.columns.values, ordered=ordered)
432434

@@ -462,8 +464,8 @@ def to_dummies(self, na_column=None) -> "DataFrame":
462464
1 False True
463465
2 False False
464466
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
467469
0 True False False
468470
1 False True False
469471
2 False False True

0 commit comments

Comments
 (0)