Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rdt/transformers/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ def _order_categories(self, unique_data):
nans = pd.isna(unique_data)
if self.order_by == 'alphabetical':
# pylint: disable=invalid-unary-operand-type
if any(map(lambda item: not isinstance(item, str), unique_data[~nans])): # noqa: C417
if any(not isinstance(item, str) for item in unique_data[~nans]):
raise TransformerInputError(
"The data must be of type string if order_by is 'alphabetical'."
)
elif self.order_by == 'numerical_value':
if not np.issubdtype(unique_data.dtype.type, np.number):
if any(not np.issubdtype(type(item), np.number) for item in unique_data[~nans]):
raise TransformerInputError(
"The data must be numerical if order_by is 'numerical_value'."
)
Expand Down
4 changes: 2 additions & 2 deletions rdt/transformers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,14 @@ def strings_from_regex(regex, max_repeat=16):


def _fill_nan_with_none_series(data):
sentinel = object()
dtype = data.dtype
if isinstance(dtype, pd.CategoricalDtype):
sentinel = object()
data = data.cat.add_categories([sentinel])
data = data.fillna(sentinel).replace({sentinel: None})
return pd.Series(pd.Categorical(data, categories=dtype.categories), index=data.index)

return data.fillna(sentinel).replace({sentinel: None})
return data.astype('object').where(~data.isna(), None)


def fill_nan_with_none(data):
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/transformers/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,24 @@ def test__fit(self):
assert transformer.frequencies == expected_frequencies
assert transformer.intervals == expected_intervals

def test_fit_with_nullable_integer_dtype(self):
"""Test that the ``fit`` method works with nullable integer columns."""
# Setup
data = pd.DataFrame({'example': [1, 2, 3, None]}, dtype='Int64')
transformer = UniformEncoder()

# Run
transformer.fit(data=data, column='example')

# Assert
expected_frequencies = {
1: 0.25,
2: 0.25,
3: 0.25,
None: 0.25,
}
assert transformer.frequencies == expected_frequencies

def test__set_fitted_parameters(self):
"""Test the ``_set_fitted_parameters`` method."""
# Setup
Expand Down
Loading