-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
CLN/BUG: Fix stacklevel on setting with copy warning. #5581
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
CLN/BUG: Fix stacklevel on setting with copy warning. #5581
Conversation
@TomAugspurger - please tell me whether this works for you and if it does I'll merge it. |
I'm not sure it got the entire stack. You can try it, I'm looking at the |
Can you post the point you're looking at? It may be that different functions need to set different stack levels. |
@jtratner you prob need to specify the stacklevel in core/indexing.py where the copy check routine is actually called |
I'm just running
Looks like 8 was the correct def test_set_value_keeps_names(self):
# motivating example from #3742
lev1 = ['hans', 'hans', 'hans', 'grethe', 'grethe', 'grethe']
lev2 = ['1', '2', '3'] * 2
idx = pd.MultiIndex.from_arrays(
[lev1, lev2],
names=['Name', 'Number'])
df = pd.DataFrame(
np.random.randn(6, 4),
columns=['one', 'two', 'three', 'four'],
index=idx)
df = df.sortlevel()
self.assertEqual(df.index.names, ('Name', 'Number'))
df = df.set_value(('grethe', '4'), 'one', 99.34)
self.assertEqual(df.index.names, ('Name', 'Number')) |
I'm -1 on making that work, you'd have to have a separate function to set def setter(item, v):
s = self.obj[item]
pi = plane_indexer[0] if lplane_indexer == 1 else
plane_indexer
# set the item, possibly having a dtype change
s = s.copy()
s._data = s._data.setitem(pi, v)
s._maybe_update_cacher(clear=True)
# would need to swap this out with a function to preserve
stacklevel
self.obj[item] = s Also, the problem with this is that you'd end up trying to do heroic things |
Isn't the actual issue that |
it's even more complicated than that so I agree because the user can always make it raise and investigate it IS an error after all; I don't think there are very many false positives @TomAugspurger did you see where the error is coming from? can u post to see the use case. is it a false positive? or actual error? |
And that warning at least shows you what is doing the setting, and for the majority of people who are doing something interactively, should be fine. |
This warning came from |
Also, yes the place your PR points to should work well for most cases. This use was a bit odd since it was from a test. |
@TomAugspurger good you noticed it though, because we should fix it so that |
CLN/BUG: Fix stacklevel on setting with copy warning.
May not be perfect, but gets us most of the way there. Resolves @TomAugspurger's comment on #5390.
Put the following in a file called
test.py
.Running it will generate this warning:
I don't think it's worth testing (especially because there's not necessarily a simple way to do it).