Skip to content

MICEData update produces SettingWithCopyWarning when using a data frame with mixed types #5430

Open

Description

@mattwigway

When running MICE on a data frame that contains an int column, I get a SettingWithCopyWarning (I'm not imputing the int column; in fact in my original data it's an ID column that is not used in the imputation at all). Here's a short example (note that column b is type integer).

import pandas as pd
import numpy as np
from statsmodels.imputation.mice import MICEData

d = pd.DataFrame({'a':[1, 2, np.nan, 4], 'b':[3, 2, 1, 4]})

print(d.dtypes)

miceData = MICEData(d)
miceData.update_all()

output:

a    float64
b      int64
dtype: object
/Users/matthewc/anaconda/envs/py37/lib/python3.7/site-packages/pandas/core/indexing.py:189: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)

If you convert column b to float, the warning disappears. I'm guessing this is because Pandas doesn't make a copy behind the scenes when all the columns are of type float.

It appears that the culprit is this line, where a chain of column indexing and location-based indexing is used.

Also mentioned in passing in #4393.

I'm working on a solution to this in my branch.

sm.show_versions output:

Details INSTALLED VERSIONS ------------------ Python: 3.7.0.final.0 OS: Darwin 18.2.0 Darwin Kernel Version 18.2.0: Mon Nov 12 20:24:46 PST 2018; root:xnu-4903.231.4~2/RELEASE_X86_64 x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8

Statsmodels

Installed: 0.9.0 (/Users/matthewc/anaconda/envs/py37/lib/python3.7/site-packages/statsmodels)

Required Dependencies

cython: Not installed
numpy: 1.14.5 (/Users/matthewc/anaconda/envs/py37/lib/python3.7/site-packages/numpy)
scipy: 1.1.0 (/Users/matthewc/anaconda/envs/py37/lib/python3.7/site-packages/scipy)
pandas: 0.23.4 (/Users/matthewc/anaconda/envs/py37/lib/python3.7/site-packages/pandas)
dateutil: 2.7.3 (/Users/matthewc/anaconda/envs/py37/lib/python3.7/site-packages/dateutil)
patsy: 0.5.0 (/Users/matthewc/anaconda/envs/py37/lib/python3.7/site-packages/patsy)

Optional Dependencies

matplotlib: 2.2.2 (/Users/matthewc/anaconda/envs/py37/lib/python3.7/site-packages/matplotlib)
backend: module://ipykernel.pylab.backend_inline
cvxopt: Not installed
joblib: Not installed

Developer Tools

IPython: 6.4.0 (/Users/matthewc/anaconda/envs/py37/lib/python3.7/site-packages/IPython)
jinja2: 2.10 (/Users/matthewc/anaconda/envs/py37/lib/python3.7/site-packages/jinja2)
sphinx: Not installed
pygments: 2.2.0 (/Users/matthewc/anaconda/envs/py37/lib/python3.7/site-packages/pygments)
nose: Not installed
pytest: 4.0.2 (/Users/matthewc/anaconda/envs/py37/lib/python3.7/site-packages)
virtualenv: Not installed

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions