-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
spurious SettingWithCopyWarning #5597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is not spurious, but exactly as intended. Though you may view it as a False Positive (but the whole point of this warning is try to not have False Negatives). The The warning can be turned off by doing any of the following, or simply setting
|
I see. Hmm, well I know I've copied the data... Same thing, but more explicitly, perhaps.
Ok. I guess I'll set this because I don't care for this level of hand-holding. In numpy, you just know what you're doing copy vs. view or explicitly ask for a copy when you want to be sure. What else does 'chained_assignment' handle? It sounds delightfully mysterious. |
Hmm, maybe I need it I guess if the pandas view vs. copy is not as readily determined as numpy's. I am very wary of your it's usually a copy (or you're 'usually not producing garbage results' elsewhere, possibly silently). Sounds like I have to live with this noise for quite common operations. Plus the warning only occurs the first time by default. This is a pretty ugly state of affairs IMO. Meh. |
agreed...the warning is really meant for new users for the most part see the docs the problem is people try to do this:
The problem is detecting this is quite difficult as the above, for example, yields 2 separate (and unreleated) another possibility (though still not pretty) is to allow
|
the thing is it has always been there, this warning just makes it known. |
Makes sense. I had to get out of the df[][] = ... habit early on. |
@jseabold @jreback Could the issue be reopened please? I'm getting a millions of warnings too in the code which I know works as intended. I mean, come on, isn't it a little too much? Something as trivial as >>> df = pd.DataFrame({'a': [1]}).dropna()
>>> df['a'] += 1 results in
Do you think this kind of code could be misinterpreted in any possible way? Every snippet like this would effectively yield a false positive. I consider the snippet above legit pandas code; what would you expect noobs to do here to avoid the warning? Dig into
Edit: I understand this has been partially addressed by #5584 for setting entire columns. I believe though there are many more particular cases where a warning can be assured.to be false positive :/ |
@aldanor I updated #5584, their WAS a supriuos indication when the operation is doing nothing except creating a new object. pls give a try with that PR and lmk. I think this warning is useful (you can always turn it off of course), but their are some false positives; just trying to remove the ones I can (as you can see above its not always possible). If you have more cases, pls post here |
Count me among the pandas users who find the current state of affairs very frustrating. We can't have a warning being standard if you follow recommended practice, which means I can no longer recommend people do the obvious thing even if it's clear it's going to work. As a result I now have to write Admittedly the only way I can think to get around this offhand is to have |
@dsm054 ony other warnigns you are getting that look spurious? |
I just noticed that the statsmodels test suite is littered with these warnings, but no failures. I've updated our code to ignore them, but surely this is spurious? If it's setting on a copy I'd assume the original to be unchanged.
|
This is THE specific case that you should NOT do
It WILL work in a single-dtyped case as numpy almost always (and that's the rub) will give you a view. But will not work in a mixed dtype case. You can turn them off, but the point is to use the correct semantics. It is not a bug but a warning to catch a potential error. The setting will proceed, but you may be setting a copy. So if succeeds you had a view and the warning was spurious. but you cannot be sure; better to set using the loc/ix indexer (in its full form). |
What version was If I read what you're saying then I could use this right?
I don't get a warning here, and I know what the dtype of table will be always. |
you can use that is the 'correct' expression to ensure it will always work |
IIRC |
Ok thanks. That's what I thought. We are still supporting old pandas unfortunately, because we are still supporting old(er) numpy. |
I am getting the warning using .loc ? FYI: |
that can generate a warning as well. simply use |
I'm getting spurious warnings on some old code that I'm running with new pandas. You can replicate by doing something like this (you have to take a subset of the data first, that's the key)
The text was updated successfully, but these errors were encountered: