Skip to content

BUG: fix inconsistency with np.array(['']) being truthy - #5967

Merged
homu merged 5 commits into
numpy:masterfrom
wimglenn:bugfix/empty_string_array
Jun 16, 2015
Merged

homu merged 5 commits into
numpy:masterfrom
wimglenn:bugfix/empty_string_array

Conversation

@wimglenn

Copy link
Copy Markdown
a = np.array([0])
b = np.array([None])
c = np.array([''])
d = np.array(['   '])

Why should we have this inconsistency:

>>> bool(a)
False
>>> bool(b)
False
>>> bool(c)
True
>>> bool(d)
False

@seberg

seberg commented Jun 14, 2015

Copy link
Copy Markdown
Member

Sounds good, but what about " \x00" strings? So I think the fix is right but should be moved further down. I.e. to include all spaces which are followed by only "\x00" and nothing else. Which is our rule for giving back things out of the array.

@seberg

seberg commented Jun 14, 2015

Copy link
Copy Markdown
Member

On the other hand, I find the whitespace rule odd, python seems to behave different?

@wimglenn

Copy link
Copy Markdown
Author

Of course, in python, empty string falsey and any other string truthy. I don't know why numpy considers whitespace strings falsey aswell, but assumed there must be a good reason since it is explicitly checked? I'm happy to fix that too but it seems like a much bigger logical change.

@seberg

seberg commented Jun 15, 2015

Copy link
Copy Markdown
Member

Yeah, it is. Maybe we could be made a FutureWarning, but should probably write the list before that. Considering the inconsistencies, I think it might be a real option to change, but then I tend to think we should weed out this stuff ;). It is likely difficult to find out why it is as it is, it isn't implausible to me that python itself changed the definition at some point.
We can put this in without all that if you make it trailing "\x00" instead of explicit length 1 check (and add a test for it of course). Maybe it is best to do that anyway and then think about FutureWarnings, etc. in a follow up. (also then we can possibly merge it for 1.10. and put off behaviour change to later)

Though one could argue the fix converts some False's to True's that in the end we might prefer to be False ;), but it is more the other way around probably :).

@wimglenn

Copy link
Copy Markdown
Author

OK, I will remove the explicit length check and look for all trailing nulls. Can you confirm the tests in the last commit are the desired behaviour for now? (They will be failing currently of course)

@pv

pv commented Jun 15, 2015 via email

Copy link
Copy Markdown
Member

@njsmith

njsmith commented Jun 15, 2015

Copy link
Copy Markdown
Member

+1 to FutureWarning from me, though yeah, should email list
On Jun 15, 2015 6:15 AM, "Pauli Virtanen" [email protected] wrote:

Fortran pads strings with whitespace rather than nulls. The strange
truthiness probably somehow derives from that; even though strings don't
have truth value in F.

—
Reply to this email directly or view it on GitHub
#5967 (comment).

@wimglenn

Copy link
Copy Markdown
Author

To email list and FutureWarning is for if/when the behaviour for whitespace string is changed to match python's behaviour, not for this change, right? Is anything else needed for this PR?

@seberg

seberg commented Jun 16, 2015

Copy link
Copy Markdown
Member

Code seems good to me the " \0 \0" being False thing seems good to me as well.
Yes, no need to mail the list for the current state probably. I am fine with putting this in as is and then thinking about it in a new PR. But if anyone has compatibility concerns, or you are happy to go for the FutureWarning that is also great (though I admit, it is more likely to be put off after 1.10 maybe then).

@njsmith

njsmith commented Jun 16, 2015

Copy link
Copy Markdown
Member

@homu r=seberg
.
We definitely shouldn't block the bug fix on the other change, so if you're
happy let's just put this in. Plus, this way we can try the new gizmo :-).
On Jun 16, 2015 12:12 AM, "seberg" [email protected] wrote:

Code seems good to me the " \0 \0" being False thing seems good to me as
well.
Yes, no need to mail the list for the current state probably. I am fine
with putting this in as is and then thinking about it in a new PR. But if
anyone has compatibility concerns, or you are happy to go for the
FutureWarning that is also great (though I admit, it is more likely to be
put off after 1.10 maybe then).

—
Reply to this email directly or view it on GitHub
#5967 (comment).

@homu

homu commented Jun 16, 2015

Copy link
Copy Markdown
Contributor

📌 Commit 8749a9a has been approved by seberg

@homu

homu commented Jun 16, 2015

Copy link
Copy Markdown
Contributor

⌛ Testing commit 8749a9a with merge 8c86a0a...

homu added a commit that referenced this pull request Jun 16, 2015
BUG: fix inconsistency with np.array(['']) being truthy

```
a = np.array([0])
b = np.array([None])
c = np.array([''])
d = np.array(['   '])
```

Why should we have this inconsistency:

```
>>> bool(a)
False
>>> bool(b)
False
>>> bool(c)
True
>>> bool(d)
False
```
@homu

homu commented Jun 16, 2015

Copy link
Copy Markdown
Contributor

☀️ Test successful - status

@homu
homu merged commit 8749a9a into numpy:master Jun 16, 2015
@wimglenn
wimglenn deleted the bugfix/empty_string_array branch January 8, 2017 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants