CoxPHFitter() .fit() --> AttributeError: 'Series' object has no attribute 'iteritems'
RSHum23 opened this issue · 4 comments
I get an AttributeError when running the .fit method of a CoxPHFitter object (please find the full stack below). The problem should be related to the "iteritems" method, now deprecated, in the call "df.dtypes.iteritems()" (line 997, last but one call). Can anyone please change this to "items" in the source file? I believe this should fix the issue, but I don't know how to do it...
Many thanks!
cph = CoxPHFitter()
cph.fit(dg, duration_col='time', event_col='evento')
Error Message:
AttributeError Traceback (most recent call last)
Input In [27], in <cell line: 2>()
1 cph = CoxPHFitter()
----> 2 cph.fit(dg, duration_col='time', event_col='evento')
File C:\ProgramData\Anaconda3\lib\site-packages\lifelines\utils_init_.py:56, in CensoringType.right_censoring..f(model, *args, **kwargs)
53 @wraps(function)
54 def f(model, *args, **kwargs):
55 cls.set_censoring_type(model, cls.RIGHT)
---> 56 return function(model, *args, **kwargs)
File C:\ProgramData\Anaconda3\lib\site-packages\lifelines\fitters\coxph_fitter.py:290, in CoxPHFitter.fit(self, df, duration_col, event_col, show_progress, initial_point, strata, weights_col, cluster_col, robust, batch_mode, timeline, formula, entry_col, fit_options)
184 """
185 Fit the Cox proportional hazard model to a right-censored dataset. Alias of fit_right_censoring
.
186
(...)
287
288 """
289 self.strata = utils.coalesce(strata, self.strata)
--> 290 self._model = self._fit_model(
291 df,
292 duration_col,
293 event_col=event_col,
294 show_progress=show_progress,
295 initial_point=initial_point,
296 strata=self.strata,
297 weights_col=weights_col,
298 cluster_col=cluster_col,
299 robust=robust,
300 batch_mode=batch_mode,
301 timeline=timeline,
302 formula=formula,
303 entry_col=entry_col,
304 fit_options=fit_options,
305 )
306 return self
File C:\ProgramData\Anaconda3\lib\site-packages\lifelines\fitters\coxph_fitter.py:610, in CoxPHFitter._fit_model(self, *args, **kwargs)
608 def _fit_model(self, *args, **kwargs):
609 if self.baseline_estimation_method == "breslow":
--> 610 return self._fit_model_breslow(*args, **kwargs)
611 elif self.baseline_estimation_method == "spline":
612 return self._fit_model_spline(*args, **kwargs)
File C:\ProgramData\Anaconda3\lib\site-packages\lifelines\fitters\coxph_fitter.py:623, in CoxPHFitter._fit_model_breslow(self, *args, **kwargs)
619 model = SemiParametricPHFitter(
620 penalizer=self.penalizer, l1_ratio=self.l1_ratio, strata=self.strata, alpha=self.alpha, label=self._label
621 )
622 if utils.CensoringType.is_right_censoring(self):
--> 623 model.fit(*args, **kwargs)
624 return model
625 else:
File C:\ProgramData\Anaconda3\lib\site-packages\lifelines\utils_init_.py:56, in CensoringType.right_censoring..f(model, *args, **kwargs)
53 @wraps(function)
54 def f(model, *args, **kwargs):
55 cls.set_censoring_type(model, cls.RIGHT)
---> 56 return function(model, *args, **kwargs)
File C:\ProgramData\Anaconda3\lib\site-packages\lifelines\fitters\coxph_fitter.py:1229, in SemiParametricPHFitter.fit(self, df, duration_col, event_col, show_progress, initial_point, strata, weights_col, cluster_col, robust, batch_mode, timeline, formula, entry_col, fit_options)
1226 self.formula = formula
1227 self.entry_col = entry_col
-> 1229 X, T, E, weights, entries, original_index, self._clusters = self._preprocess_dataframe(df)
1231 self.durations = T.copy()
1232 self.event_observed = E.copy()
File C:\ProgramData\Anaconda3\lib\site-packages\lifelines\fitters\coxph_fitter.py:1334, in SemiParametricPHFitter._preprocess_dataframe(self, df)
1331 utils.check_nans_or_infs(E)
1332 E = E.astype(bool)
-> 1334 self._check_values_pre_fitting(X, T, E, W, entries)
1336 return X, T, E, W, entries, original_index, _clusters
File C:\ProgramData\Anaconda3\lib\site-packages\lifelines\fitters\coxph_fitter.py:1350, in SemiParametricPHFitter._check_values_pre_fitting(self, X, T, E, W, entries)
1346 """
1347 Some utilities to check for bad data coming in, like NaNs or complete separation.
1348 """
1349 utils.check_low_var(X)
-> 1350 utils.check_for_numeric_dtypes_or_raise(X)
1351 utils.check_nans_or_infs(T)
1352 utils.check_nans_or_infs(X)
File C:\ProgramData\Anaconda3\lib\site-packages\lifelines\utils_init_.py:997, in check_for_numeric_dtypes_or_raise(df)
996 def check_for_numeric_dtypes_or_raise(df):
--> 997 nonnumeric_cols = [col for (col, dtype) in df.dtypes.iteritems() if dtype.name == "category" or dtype.kind not in "biuf"]
998 if len(nonnumeric_cols) > 0: # pylint: disable=len-as-condition
999 raise TypeError(
1000 "DataFrame contains nonnumeric columns: %s. Try 1) using pandas.get_dummies to convert the non-numeric column(s) to numerical data, 2) using it in stratification strata=
, or 3) dropping the column(s)."
1001 % nonnumeric_cols
1002 )
File C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py:6204, in NDFrame.getattr(self, name)
6197 if (
6198 name not in self._internal_names_set
6199 and name not in self._metadata
6200 and name not in self._accessors
6201 and self._info_axis._can_hold_identifiers_and_holds_name(name)
6202 ):
6203 return self[name]
-> 6204 return object.getattribute(self, name)
AttributeError: 'Series' object has no attribute 'iteritems'
Hi @RSHum23, can you confirm what version of lifelines you are on?
@CamDavidsonPilon it should be 0.27.1
I think I found a (very handmade) workaround: I changed locally the source code where needed (so, "items" instead of "iteritems" in utils_init_.py) and a couple of other changes to get rid of the risen exceptions and now it works...
I would try a later version of lifelines, too. I don't see any mention of iteritems
in the correct codebase.
I will! Thank you