Skip to content

Commit

Permalink
Always unwrap factor values before use due to unfortunate interaction…
Browse files Browse the repository at this point in the history
…s with numpy arrays. (#241)
  • Loading branch information
matthewwardrop authored Jan 9, 2025
1 parent 8552610 commit 497b2f7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 5 additions & 5 deletions formulaic/materializers/types/factor_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
Union,
)

try:
from typing import SupportsIndex
except ImportError: # pragma: no cover
from typing_extensions import SupportsIndex

import wrapt

from formulaic.parser.types import Factor
from formulaic.utils.sentinels import MISSING, MissingType

try:
from typing import SupportsIndex
except ImportError: # pragma: no cover
from typing_extensions import SupportsIndex

if TYPE_CHECKING: # pragma: no cover
from formulaic.model_spec import ModelSpec

Expand Down
7 changes: 6 additions & 1 deletion formulaic/transforms/contrasts.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ def encoder(
encoder_state: dict[str, Any],
model_spec: ModelSpec,
) -> FactorValues:
values = pandas.Series(values)
# wrapped numpy arrays are problematic
values = pandas.Series(
values.__wrapped__ if isinstance(values, FactorValues) else values
)
values = values.drop(index=values.index[drop_rows])
return encode_contrasts(
values,
Expand Down Expand Up @@ -129,6 +132,8 @@ def encode_contrasts( # pylint: disable=dangerous-default-value # always repla
levels = (
levels if levels is not None else _state.get("categories")
) # TODO: Is this too early to provide useful feedback to users?
if isinstance(data, FactorValues): # wrapped numpy arrays are problematic
data = data.__wrapped__

if contrasts is None:
contrasts = TreatmentContrasts()
Expand Down

0 comments on commit 497b2f7

Please sign in to comment.