Skip to content

Commit 5616f6f

Browse files
authored
Merge pull request #174 from datarian/fix-basen-inverse-transform-colnames
Fix basen and onehot inverse transform colnames
2 parents d329d9a + c3e7a82 commit 5616f6f

File tree

4 files changed

+9
-11
lines changed

4 files changed

+9
-11
lines changed

category_encoders/one_hot.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -372,26 +372,24 @@ def reverse_dummies(self, X, mapping):
372372
numerical: DataFrame
373373
374374
"""
375-
out_cols = X.columns.values
376-
cols = []
375+
out_cols = X.columns.values.tolist()
377376
mapped_columns = []
378377
for switch in mapping:
379378
col = switch.get('col')
380379
mod = switch.get('mapping')
381-
cols.append(col)
380+
insert_at = out_cols.index(mod.columns[0])
382381

383-
X[col] = 0
382+
X.insert(insert_at, col, 0)
384383
positive_indexes = mod.index[mod.index > 0]
385384
for i in range(positive_indexes.shape[0]):
386385
existing_col = mod.columns[i]
387386
val = positive_indexes[i]
388-
389387
X.loc[X[existing_col] == 1, col] = val
390388
mapped_columns.append(existing_col)
389+
X.drop(mod.columns, axis=1, inplace=True)
390+
out_cols = X.columns.values.tolist()
391391

392-
out_cols = [col0 for col0 in out_cols if col0 not in mapped_columns]
393-
394-
return X.reindex(columns=out_cols + cols)
392+
return X
395393

396394
def get_feature_names(self):
397395
"""

category_encoders/tests/test_encoders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_np(self):
4848
def test_classification(self):
4949
for encoder_name in encoders.__all__:
5050
with self.subTest(encoder_name=encoder_name):
51-
cols = ['unique_str', 'underscore', 'extra', 'none', 'invariant', 321, 'categorical']
51+
cols = ['unique_str', 'underscore', 'extra', 'none', 'invariant', 321, 'categorical', 'na_categorical']
5252

5353
enc = getattr(encoders, encoder_name)(cols=cols)
5454
enc.fit(X, np_y)

category_encoders/tests/test_polynomial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from unittest2 import TestCase # or `from unittest import ...` if on Python 3.4+
33
import numpy as np
44
import category_encoders as encoders
5-
from category_encoders.tests.test_helpers import deep_round
5+
from category_encoders.tests.helpers import deep_round
66

77
a_encoding = [1, -0.7071067811865476, 0.40824829046386313]
88
b_encoding = [1, -5.551115123125783e-17, -0.8164965809277261]

category_encoders/tests/test_woe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
class TestWeightOfEvidenceEncoder(TestCase):
1919
def test_woe(self):
20-
cols = ['unique_str', 'underscore', 'extra', 'none', 'invariant', 321, 'categorical']
20+
cols = ['unique_str', 'underscore', 'extra', 'none', 'invariant', 321, 'categorical', 'na_categorical']
2121

2222
# balanced label with balanced features
2323
X_balanced = pd.DataFrame(data=['1', '1', '1', '2', '2', '2'], columns=['col1'])

0 commit comments

Comments
 (0)