Skip to content

Better error message for length of array with unknown chunk sizes - #8436

Merged
jrbourbeau merged 2 commits into
dask:mainfrom
douglasdavis:len-on-unknown-size
Dec 7, 2021
Merged

jrbourbeau merged 2 commits into
dask:mainfrom
douglasdavis:len-on-unknown-size

Conversation

@douglasdavis

@douglasdavis douglasdavis commented Nov 30, 2021 •

Copy link
Copy Markdown
Member
  • [ ] Closes #xxxx
  • Tests added / passed
  • Passes pre-commit run --all-files

Discovered this while tinkering with len of collections for some dask-awkward dev. Previous behavior:

>>> import dask.array as da
>>> a = da.random.random((100, 2))
>>> a = a[a < 0.5]
>>> len(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer

with this PR:

>>> a = da.random.random((100, 2))
>>> a = a[a < 0.5]
>>> len(a)
ValueError: Cannot call len() on object with unknown chunk size.

A possible solution: https://docs.dask.org/en/latest/array-chunks.html#unknown-chunks
Summary: to compute chunks sizes, use

   x.compute_chunk_sizes()  # for Dask Array `x`
   ddf.to_dask_array(lengths=True)  # for Dask DataFrame `ddf`

@github-actions github-actions Bot added the array label Nov 30, 2021
@jrbourbeau jrbourbeau mentioned this pull request Dec 3, 2021
2 of 3 tasks

@jrbourbeau jrbourbeau left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @douglasdavis -- apologies for the delayed review

Comment thread dask/array/core.py Outdated
Comment on lines +1407 to +1410
try:
return int(sum(self.chunks[0]))
except (TypeError, ValueError):
pass

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we're catching multiple failure modes here. I wonder if we might be more explicit and do something like

if np.isnan(self.shape).any():

instead where we check is there are any np.nans in self.chunks[0].

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this is easier to understand

Comment thread dask/array/core.py Resolved
@douglasdavis

Copy link
Copy Markdown
Member Author

Thanks @jrbourbeau! Just addressed your comments. Let me know if anything else comes to mind.

@jrbourbeau jrbourbeau changed the title Better error message for length of object with unknown size. Better error message for length of array with unknown chunk sizes Dec 7, 2021

@jrbourbeau jrbourbeau left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @douglasdavis, this is a nice improvement. Will merge once CI finishes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants