MixedLM.predict inconsistently fails for df with string
Author: kenjiomanCreated Jun 11, 2018Updated Sep 28, 2026
Labelstype-possible-bug
Reproducible minimum example:
import numpy as np
import pandas as pd
import statsmodels.api as sm
data = pd.DataFrame({'A': np.arange(10),
'B': ['a'] * 10,
'G': [0]*5 + [1] * 5,
'Y': np.arange(10) + np.random.randn(10)})
print(data.head())
model = sm.MixedLM.from_formula('Y ~ A', groups='G', re_formula='1',
data=data.iloc[1:,])
result = model.fit()
# This works
print(result.predict(data.head(1)))
# This fails with AttributeError: 'DataFrame' object has no attribute 'dtype'
print(result.predict(data.iloc[0, :]))If you run it without column "B", or change it to 'B': [1] * 10, (in the data = pd.DataFrame step), it works every time.
Also, there are times that we get the
ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals
warning as well, but if you just re-run it a few more times, it'll work (through the fitting part) without any warnings, and the final error will still show up, regardless of the warning.
From show_versions (running with Anaconda distribution):
INSTALLED VERSIONS
------------------
Python: 3.6.5.final.0
OS: Linux 3.13.0-143-generic #192-Ubuntu SMP Tue Feb 27 10:45:36 UTC 2018 x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
Statsmodels
===========
Installed: 0.9.0 (/path/to/miniconda3/envs/virtual/lib/python3.6/site-packages/statsmodels)
Required Dependencies
=====================
cython: Not installed
numpy: 1.14.3 (/path/to/miniconda3/envs/virtual/lib/python3.6/site-packages/numpy)
scipy: 1.1.0 (/path/to/miniconda3/envs/virtual/lib/python3.6/site-packages/scipy)
pandas: 0.23.0 (/path/to/miniconda3/envs/virtual/lib/python3.6/site-packages/pandas)
dateutil: 2.7.3 (/path/to/miniconda3/envs/virtual/lib/python3.6/site-packages/dateutil)
patsy: 0.5.0 (/path/to/miniconda3/envs/virtual/lib/python3.6/site-packages/patsy)
Optional Dependencies
=====================
matplotlib: 2.2.2 (/path/to/miniconda3/envs/virtual/lib/python3.6/site-packages/matplotlib)
backend: module://ipykernel.pylab.backend_inline
cvxopt: Not installed
joblib: Not installed
Developer Tools
================
IPython: 6.4.0 (/path/to/miniconda3/envs/virtual/lib/python3.6/site-packages/IPython)
jinja2: 2.10 (/path/to/miniconda3/envs/virtual/lib/python3.6/site-packages/jinja2)
sphinx: 1.7.4 (/path/to/miniconda3/envs/virtual/lib/python3.6/site-packages/sphinx)
pygments: 2.2.0 (/path/to/miniconda3/envs/virtual/lib/python3.6/site-packages/pygments)
nose: Not installed
pytest: Not installed
virtualenv: Not installedSource: statsmodels/statsmodels