You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG: truthiness of strings is arbitrary, context-dependent, and inconsistent #9875
@LevN0 it is the only consistent way and it is identical to python: bool("0") vs. bool(int("0")). So yes, what to do right now is a bit trickier, unless we do a quick 1.18.4 release just for this. @charris ping just in case you did not see gh-16023 yet.
Now, there are some exceptions to this and I am not sure what they currently do. That is textreaders should behave differently, since they parse True/False. But I guess those must not have been using this anyway.
Jeesh, I did not remember the crazyness of ignoring all whitespaces for the nonzero implementation. I am not even sure we can put a warning into that easily, since it might be used all over without the GIL...
That is:
np.nonzero(np.array([" ", "\n", "\t", ""]))
# Should only consider the last "" as Falsy, but considers them all False.
Yeah, I think we should/have to do that... I am not quite sure yet how to do the transition(or lets say how nasty it will be). For current strings. I suppose arr == arr.dtype.type() is a way to opt in to the future behaviour (for most dtypes anyway, not for object).
What would this mean for pandas (and dask)? The current (white-space ignoring) way seems so strange to me from a modern Python perspective...
The current (white-space ignoring) way seems so strange to me from a modern Python perspective
Agreed.
I only discovered this issue because of a test in dask. We currently implement argwhere via array.astype(bool), which does treat ' ' as True, so our results differ from NumPy. I'm probably OK with that difference.
This shouldn't really affect pandas users, since we convert incoming fix-width string dtype to object-dtype.
Right, the only real thing I would be worried about if we manage to add reasonable warnings in our code paths and you all suddenly have to try and work around that.
Since Chuck gave a ping on the related PR. Do we think we could make a decision to move forward with this?
I doubt it is much in the way of DTypes, just one more of those annoying things to look out for. The reason is that all this does is that nonzero can't be implemented in terms of .astype(bool) and .astype(bool) is hopeless inconsistent for strings. That is all very annoying, but I am not sure it will float up somewhere as more than a simple special case.
Of course, this is still a major bug and trap, so I would still be happy to form a plan about just fixing it!
We had discussed this, and we somewhat agreed that this is desireable, and should have FutureWarnings (or VisibleDeprecationWarning.
A specific issue may be np.loadtext and genfromtxt since that might be a more common use case for using 0 and 1 as boolean, so that may need some special consideration.
I hope to look at this, hopefully, FutureWarnings (and opting in to future behaviour) will be reasonably possible.
Activity
eric-wieser commentedon Oct 17, 2017
Continues from #5967
mattip commentedon Apr 29, 2019
Should we move forward with this? PR #9877 requests discussion take place here, but the issue does not seem to have garnered much attention.
Edit: PR number
LevN0 commentedon Apr 20, 2020
Looking at that PR, what is the future path for doing the conversation it cites if some version of it gets approved.
So is the suggestion for future code going from an array of strings containing 0/1 to bool to typecast twice?
seberg commentedon Apr 20, 2020
@LevN0 it is the only consistent way and it is identical to python:
bool("0")vs.bool(int("0")). So yes, what to do right now is a bit trickier, unless we do a quick 1.18.4 release just for this. @charris ping just in case you did not see gh-16023 yet.Now, there are some exceptions to this and I am not sure what they currently do. That is textreaders should behave differently, since they parse True/False. But I guess those must not have been using this anyway.
eric-wieser commentedon Apr 20, 2020
Can someone compute the diff between the table here and the one from 1.18.3?
seberg commentedon Apr 20, 2020
Well, I used master, but I doubt anything else changed this. Only the last column changed, and that of course changed quite a bit:
xbool(x)bool(sc)bool(arr[()])bool(arr)sc.astype(bool)''FalseFalseFalseFalseFalse'\0'TrueTrueFalseFalseFalse' 'TrueTrueTrueFalseTrue' \0'TrueTrueTrueFalseTrue'0'TrueTrueTrueTrueTrue'1'TrueTrueTrueTrueTrue'\0 'TrueTrueTrueTrueTrue' \0 'TrueTrueTrueTrueTrueThe NULL padding getting lost is the problem of the scalar though, numpy unicode though, not the bool cast itself.
seberg commentedon Apr 20, 2020
As to the
bool(sc)logic, it is mirrored bylen(sc)since the NULL stripping is not consistent there.seberg commentedon Apr 24, 2020
Jeesh, I did not remember the crazyness of ignoring all whitespaces for the
nonzeroimplementation. I am not even sure we can put a warning into that easily, since it might be used all over without the GIL...That is:
See also gh-9877
TomAugspurger commentedon May 1, 2020
Just to confirm, any changes here would eventually apply to
whereandargwhere? So the output ofmight change to count
' 'as true in the future?seberg commentedon May 1, 2020
Yeah, I think we should/have to do that... I am not quite sure yet how to do the transition(or lets say how nasty it will be). For current strings. I suppose
arr == arr.dtype.type()is a way to opt in to the future behaviour (for mostdtypesanyway, not forobject).What would this mean for pandas (and dask)? The current (white-space ignoring) way seems so strange to me from a modern Python perspective...
TomAugspurger commentedon May 1, 2020
Agreed.
I only discovered this issue because of a test in dask. We currently implement
argwhereviaarray.astype(bool), which does treat' 'as True, so our results differ from NumPy. I'm probably OK with that difference.This shouldn't really affect pandas users, since we convert incoming fix-width string dtype to object-dtype.
seberg commentedon May 1, 2020
Right, the only real thing I would be worried about if we manage to add reasonable warnings in our code paths and you all suddenly have to try and work around that.
TomAugspurger commentedon May 1, 2020
Ah, no, I don't think so (at least not for where) since we're using
astype(bool)and masking, Dask users shouldn't see any warnings.seberg commentedon Dec 18, 2020
Since Chuck gave a ping on the related PR. Do we think we could make a decision to move forward with this?
I doubt it is much in the way of DTypes, just one more of those annoying things to look out for. The reason is that all this does is that
nonzerocan't be implemented in terms of.astype(bool)and.astype(bool)is hopeless inconsistent for strings. That is all very annoying, but I am not sure it will float up somewhere as more than a simple special case.Of course, this is still a major bug and trap, so I would still be happy to form a plan about just fixing it!
seberg commentedon Jan 27, 2021
We had discussed this, and we somewhat agreed that this is desireable, and should have
FutureWarnings(orVisibleDeprecationWarning.A specific issue may be
np.loadtextandgenfromtxtsince that might be a more common use case for using 0 and 1 as boolean, so that may need some special consideration.I hope to look at this, hopefully,
FutureWarnings(and opting in to future behaviour) will be reasonably possible.