question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bool(Dataset(False)) is True

See original GitHub issue

What happened:

v=True
bool(xr.DataArray(v)) # True
bool(xr.DataArray(v).to_dataset(name='var')) # True

v=False
bool(xr.DataArray(v)) # False
# unexpected behaviour below
bool(xr.DataArray(v).to_dataset(name='var')) # True

What you expected to happen:

bool(xr.DataArray(False).to_dataset(name='var')) # False

Maybe this is intentional and I dont understand why.

xr.version = ‘0.16.0’

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
max-sixtycommented, Jul 30, 2020

We mirror numpy for DataArray and, per @keewis , Mapping for Dataset. I think we should have a fairly high bar for deviating from those.

For those who think bool(xr.Dataset(dict(x=False))) should evaluate to False, what do you think dict(x=False) should evaluate to?

IIUC, this is somewhat of an edge case — it’s only possible to coerce DataArray to bool when there’s exactly one element. (Not that we shouldn’t resolve)

This question seems to me to be conceptually similar to the question I described in #3315: “Should we treat a named DataArray and a single-variable Dataset as if they are the same?”

👍

1reaction
keewiscommented, Jul 30, 2020

If I remember correctly, Dataset inherits from dict and doesn’t define it’s own __bool__, so it has the behavior of dict.__bool__:

In [2]: bool(xr.Dataset())
Out[2]: False

In [3]: bool(xr.Dataset({"x": False}))
Out[3]: True

In [4]: bool({})
Out[4]: False

In [5]: bool({"x": False})
Out[5]: True
Read more comments on GitHub >

github_iconTop Results From Across the Web

true and false operators - treat objects as Boolean values
The false operator returns the bool value true to indicate that its operand is definitely false.
Read more >
Boolean values - IBM
A Boolean value represents a truth value; that is, TRUE or FALSE. A Boolean expression or predicate can result in a value of...
Read more >
Boolean Data Type
The result of a Boolean expression is either true or false. • Boolean expressions allow us to write programs that decide whether to...
Read more >
How come I can add the boolean value False but not True in a ...
I just started investigating the set data type in Python. For some reason, whenever I add the Boolean value of True to a...
Read more >
Boolean data type in Python - GeeksforGeeks
Python boolean type is one of the built-in data types provided by Python, which represents one of the two values i.e. True or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found