UDST / choicemodels

Python library for discrete choice modeling
https://udst.github.io/choicemodels
BSD 3-Clause "New" or "Revised" License
74 stars 33 forks source link

Resolving SettingWithCopyWarning in mnl_interaction_dataset() #16

Closed smmaurer closed 6 years ago

smmaurer commented 7 years ago

I've been getting runtime warnings for a piece of code from urbansim.urbanchoice that we use to sample alternatives for an estimation dataset.

screen shot 2017-08-10 at 12 56 07 pm

I think what's happening is that the runtime environment does not recognize the output of df.take() as an explicit copy (even though my reading of the documentation is that it should be), so it sees this as potential chained indexing:

alts_sample = alternatives.take(sample)
alts_sample['join_index'] = ...

http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.take.html

Neither of these changes resolved the warning:

alts_sample.loc[:,'join_index'] = ... alts_sample = alternatives.take(sample, is_copy=True)

But this did:

alts_sample = alternatives.take(sample).copy()

This PR also clears the warning from two notebooks (results remained the same).

coveralls commented 7 years ago

Coverage Status

Coverage remained the same at 52.805% when pulling f94f920c318b2a7ee7bf90bb55d4c61026131166 on setting-with-copy into 1ecfb4d1ec1a7c48045918048dd1d6f2087b5ea9 on master.

smmaurer commented 7 years ago

@janowicz Great - thank you for taking a look, and good to know you had the same reaction.