Skip to content

Commit e86c62b

Browse files
do not fail on missing class members that are new in v3
1 parent b185fe4 commit e86c62b

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/glum/_glm.py

+22-9
Original file line numberDiff line numberDiff line change
@@ -883,16 +883,23 @@ def _convert_from_pandas(
883883
if hasattr(self, "X_model_spec_"):
884884
return self.X_model_spec_.get_model_matrix(df, context=context)
885885

886-
cat_missing_method_after_alignment = self.cat_missing_method
886+
cat_missing_method_after_alignment = getattr(self, "cat_missing_method", "fail")
887887

888888
if hasattr(self, "feature_dtypes_"):
889889
df = _align_df_categories(
890890
df,
891891
self.feature_dtypes_,
892-
self.has_missing_category_,
893-
self.cat_missing_method,
892+
getattr(
893+
self,
894+
"has_missing_category_",
895+
{f: False for f in self.feature_dtypes_.keys()},
896+
),
897+
getattr(self, "cat_missing_method", "fail"),
894898
)
895-
if self.cat_missing_method == "convert":
899+
if (
900+
hasattr(self, "categorical_format")
901+
and self.cat_missing_method == "convert"
902+
):
896903
df = _add_missing_categories(
897904
df=df,
898905
dtypes=self.feature_dtypes_,
@@ -906,7 +913,10 @@ def _convert_from_pandas(
906913
X = tm.from_pandas(
907914
df,
908915
drop_first=self.drop_first,
909-
categorical_format=self.categorical_format,
916+
# prior to v3, categorical format used underscore
917+
categorical_format=getattr(
918+
self, "categorical_format", "{name}__{category}"
919+
),
910920
cat_missing_method=cat_missing_method_after_alignment,
911921
)
912922

@@ -2724,7 +2734,8 @@ def _set_up_and_check_fit_args(
27242734

27252735
self.feature_dtypes_ = X.dtypes.to_dict()
27262736
self.has_missing_category_ = {
2727-
col: (self.cat_missing_method == "convert") and X[col].isna().any()
2737+
col: (getattr(self, "cat_missing_method", "fail") == "convert")
2738+
and X[col].isna().any()
27282739
for col, dtype in self.feature_dtypes_.items()
27292740
if isinstance(dtype, pd.CategoricalDtype)
27302741
}
@@ -2784,9 +2795,11 @@ def _expand_categorical_penalties(
27842795
X = tm.from_pandas(
27852796
X,
27862797
drop_first=self.drop_first,
2787-
categorical_format=self.categorical_format,
2788-
cat_missing_method=self.cat_missing_method,
2789-
cat_missing_name=self.cat_missing_name,
2798+
categorical_format=getattr(
2799+
self, "categorical_format", "{name}__{category}"
2800+
),
2801+
cat_missing_method=getattr(self, "cat_missing_method", "fail"),
2802+
cat_missing_name=getattr(self, "cat_missing_name", "(MISSING)"),
27902803
)
27912804

27922805
if y is None:

0 commit comments

Comments
 (0)