Skip to content

Fix MICEData SettingWithCopyWarning on mixed-type dataframes - #5431

Closed
mattwigway wants to merge 5 commits into
statsmodels:masterfrom
mattwigway:mice-setting-with-copy-warning
Closed

mattwigway wants to merge 5 commits into
statsmodels:masterfrom
mattwigway:mice-setting-with-copy-warning

Conversation

@mattwigway

Copy link
Copy Markdown

I believe this should fix #5430; I have an automated test that passes, at least for integer dtypes.

@coveralls

coveralls commented Dec 18, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) to 84.54% when pulling 93e7526 on mattwigway:mice-setting-with-copy-warning into 990fec5 on statsmodels:master.

@kshedden

Copy link
Copy Markdown
Contributor

Thanks, this looks good. The CI has been broken for a long time. I don't know when we will be able to merge things again, but I'll merge this as soon as possible.

@josef-pkt

Copy link
Copy Markdown
Member

The python 3.4 version on travis has a MICE related failure from this PR
It might not need to be fixed if we drop 3.4 for the release.
The python 3.6 fix for conda is waiting for merge, (I just forgot, moving to a new house in 3 days)

@mattwigway

Copy link
Copy Markdown
Author

The error on Python 3.4 is from the test I added, looks like some warnings are being produced (not, I think, the warning I was trying to catch, but something else). I'll investigate further.

@josef-pkt

Copy link
Copy Markdown
Member

I merged #5424, so travis with python 3.6 is also green on master.
The only failures are in "allowed failures" versions, i.e. problems with dev versions of dependencies.

@mattwigway
mattwigway force-pushed the mice-setting-with-copy-warning branch from 01d84ee to 20691ae Compare April 26, 2019 02:41
@codecov

codecov Bot commented Apr 26, 2019

Copy link
Copy Markdown

Codecov Report

Merging #5431 into master will decrease coverage by 0.27%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5431      +/-   ##
==========================================
- Coverage   82.03%   81.76%   -0.28%     
==========================================
  Files         588      569      -19     
  Lines       93062    89859    -3203     
  Branches    10317    10022     -295     
==========================================
- Hits        76345    73474    -2871     
+ Misses      14355    14119     -236     
+ Partials     2362     2266      -96
Impacted Files Coverage Δ
statsmodels/imputation/mice.py 87.25% <100%> (ø) ⬆️
statsmodels/imputation/tests/test_mice.py 96.76% <100%> (+0.1%) ⬆️
statsmodels/base/elastic_net.py 87.39% <0%> (-3.37%) ⬇️
statsmodels/base/_penalized.py 80% <0%> (-1.38%) ⬇️
statsmodels/tsa/vector_ar/util.py 84.21% <0%> (-1.25%) ⬇️
statsmodels/tsa/arima_process.py 77.68% <0%> (-0.57%) ⬇️
statsmodels/regression/mixed_linear_model.py 89.77% <0%> (-0.47%) ⬇️
statsmodels/genmod/generalized_linear_model.py 90.7% <0%> (-0.4%) ⬇️
statsmodels/iolib/summary.py 54.63% <0%> (-0.31%) ⬇️
statsmodels/tsa/vector_ar/tests/test_var.py 93.4% <0%> (-0.25%) ⬇️
... and 55 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 990fec5...20691ae. Read the comment docs.

@codecov

codecov Bot commented Apr 26, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #5431 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5431      +/-   ##
==========================================
+ Coverage   82.03%   82.03%   +<.01%     
==========================================
  Files         588      588              
  Lines       93062    93073      +11     
  Branches    10317    10318       +1     
==========================================
+ Hits        76345    76356      +11     
  Misses      14355    14355              
  Partials     2362     2362
Impacted Files Coverage Δ
statsmodels/imputation/mice.py 87.25% <100%> (ø) ⬆️
statsmodels/imputation/tests/test_mice.py 96.8% <100%> (+0.14%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 990fec5...93e7526. Read the comment docs.

@mattwigway

Copy link
Copy Markdown
Author

Hey, kinda forgot about this over Christmas, sorry. I just dug more into why the test was failing on Python 3.4 and confirmed it is unrelated to the SettingWithCopyWarning issue - there's a DeprecationWarning being raised which is

DeprecationWarning('pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead',)

I'm assuming you're not concerned about the deprecation warning that's only showing up on Python 3.4, so I just made the test ignore deprecation warnings. I also rebased my branch to latest master.

I did put the existing master on a debugger and step through the code, and it appears that even with the existing code that the data is getting updated in spite of the SettingWithCopyWarning, but there's no guarantee that that would be true in every case, I think, so I think there could be a situation where the imputation does not work as intended with the current master because _store_values fails to actually store the values due to the chained indexing.

Let me know if there's anything I can do to help with the CI (if that's even still an issue), would love to see this merged so I don't have to work around it in my workflow.


with pd.option_context('mode.chained_assignment', 'warn'):
with warnings.catch_warnings(record=True) as ws:
warnings.simplefilter('always')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would pytest.mark.filterwarnings('ignore:pandas.core.common.is_categorical_dtype is deprecated:DeprecationWarning') work better? you might still be able to assert_no_warnings()?

You might be able to do...

@pytest.mark.filterwarnings('ignore:pandas.core.common.is_categorical_dtype is deprecated:DeprecationWarning')
def test_settingwithcopywarning(self):
    df = gendat()
    # There need to be some ints in here for the error to be thrown
    df['intcol'] = np.arange(len(df))
    df['intcol'] = df.intcol.astype('int32')
	
    miceData = mice.MICEData(df)
    with assert_no_warnings():
        miceData.update_all()

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good thought. I just tried that though and unfortunately it doesn't work - assert_no_warnings still fails (must be catching them below where pytest does its filtering).

@bashtage bashtage mentioned this pull request May 2, 2019
4 tasks done
@bashtage bashtage added the rejected Used for PRs with changes that are not acceptable label Jul 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rejected Used for PRs with changes that are not acceptable

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

6 participants