-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
bool(MultiIndex) seems to always return False #7897
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
hmm, actually it should always return and in reality this should simply raise. its a numpy artifact that it does not. you should do:
or
as the index can never be None |
The case that caused a bug for me was this: # Reindex by a common index
new_index = frame_1.index.intersection(frame_2.index)
frame_1 = frame_1.reindex(index=new_index)
frame_2 = frame_2.reindex(index=new_index) The natural thing to do, since you have |
can u post the starting indexes? or a facsimile ? |
My previous comment (about my bug) was confusing. The bug occurred when there was a non-empty index intersection, and I therefore expected to step into an >>> frame_1 = pandas.DataFrame({'col1': [1, 2]},
index= pandas.MultiIndex.from_tuples([('A', 1), ('A', 2)]))
>>> frame_2 = pandas.DataFrame({'col1': [1, 2]},
index= pandas.MultiIndex.from_tuples([('A', 1), ('A', 3)]))
>>> common_index = frame_1.index.intersection(frame_2.index)
>>> common_index
MultiIndex(levels=[[u'A'], [1]],
labels=[[0], [0]],
sortorder=0) And then >>> if common_index:
print "Stepped in"
else:
print "Did not step in"
Did not step in |
ok I'll have a look I don't this was defined and relied on numpy behavior. was always false because of the way a MultiIndex is defined I think it's reasonable to make the nonzero simply return the len boolean |
+1. IMO doing anything other than raising would be surprising (same argument as Series and DataFrame). Wow to the other PR this inspired! |
This means that patterns such as:
with
my_frame
having aMultiIndex
will never enter theif
block. One can check thatbool(my_frame.index)
returns False. A single index (pandas.Index
) will (correctly IMO) raise aValueError
warning that the truth value of an array is ambiguous. However, aMultiIndex
fails silently.This is such an awesome source of silent bugs that I'm surprised this hasn't come up before. Is there any reason why we want
bool
to returnFalse
every time for aMultiIndex
?The text was updated successfully, but these errors were encountered: