Skip to content

DataFrame: allow map_partitions to make Arrays with non-NumPy chunks - #7399

Draft
gjoseph92 wants to merge 4 commits into
dask:mainfrom
gjoseph92:array/preserve-subclass-in-finalize
Draft

gjoseph92 wants to merge 4 commits into
dask:mainfrom
gjoseph92:array/preserve-subclass-in-finalize

Conversation

@gjoseph92

@gjoseph92 gjoseph92 commented Mar 15, 2021 •

Copy link
Copy Markdown
Collaborator

Currently, if your map_partitions function returns an array-like object that's not a NumPy array (and has a dtype which isn't a NumPy dtype), map_partitions will fail when constructing meta. This PR allows whatever meta was produced to pass through directly to the Array. This should make mapped functions that return pandas ExtensionArrays "just work" (can add some tests that that's so if we want to go ahead with this). And if the ExtensionArray type won't behave well as an Array chunk type for a particular operation, then hopefully doing that operation on the meta should fail as well.

This was a 1-line change, but it then breaks other tests:

  • DataFrame.to_records now produces an Array where the meta is an np.recarray, not an np.ndarray
  • But when you compute an Array of recarrays, the chunks get concatenated with np.concatenate, which doesn't preserve ndarray subclasses. This causes assert_eq to complain that the type changed when computed (meta type is recarray, computed type is ndarray).
  • When you make concatenate preserve ndarray subclasses, test_to_npy_stack fails, because the meta for the Array indicated ndarray, but the result was actually memmap (because the chunks are memmaps, which we now preserve).

So that's why I also added logic for concatenate to preserve ndarray subclasses. If we even are okay with that change, I'd be fine splitting it to a separate PR for clarity.

cc @jrbourbeau: is this worth doing?

Related to #7377 (comment) and #7206.

  • Tests added / passed
  • Passes black dask / flake8 dask

`np.concatenate` doesn't preserve array subclasses. This doesn't much matter, except it means our `meta` could be accurate from the chunks, but not for the final array returned from `compute` (after chunks are concatenated), which throws off some `assert_eq` tests.
Don't assume it's a NumPy array and just pass `dtype`
@gjoseph92

Copy link
Copy Markdown
Collaborator Author

Brief update here: turns out pandas ExtensionArrays don't work as dask.Array chunks/meta out of the box. There are many places throughout the Array code where we expect arr.dtype to be a NumPy dtype, and construct NumPy arrays directly, rather than using whatever type meta is. In practice, this means both elemwise operations (arr + 1) and reductions (arr.mean()) don't work.

Some of this should be pretty easy to fix; I'm pushing a commit here (d6fe64f) that gets elemwise and some reductions sorta working. But there are other things that will be difficult to generalize to arbitrary duck dtypes: for example, the argmin/argmax implementation is heavily reliant on arr.dtype being a NumPy dtype, since it constructs a NumPy structured dtype around it.

@jsignell

jsignell commented Apr 1, 2021

Copy link
Copy Markdown
Member

The failures that you ran into in this change are related to the failures that I encountered on #7491. The records test in particular seems slightly broken in a way that we just weren't testing for as mentioned in #7486 (comment)

@jakirkham

jakirkham commented Apr 1, 2021 •

Copy link
Copy Markdown
Member

Do you know how does this behaves when using the Distributed Scheduler?

@gjoseph92

Copy link
Copy Markdown
Collaborator Author

Manually testing some basic stuff on a LocalCluster seems to work fine. Anything in particular you were wondering about?

@jakirkham

Copy link
Copy Markdown
Member

Wasn't sure how well some of the more obscure types behaved when being serialized for example. Though that might not be too relevant (memory-mapping makes more sense with the threaded scheduler for example)

@jsignell

Copy link
Copy Markdown
Member

@gjoseph92 @jakirkham I am kicking myself for not writing this up better after our meeting several months ago, but I think we decided to raise an informative error rather than trying to handle ExtensionArrays.

There are some comments in #7375 (comment) as well but none of those seem to address what we want to do with these two PRs.

@jakirkham

Copy link
Copy Markdown
Member

Yeah I think that sounds right, but must admit my memory is a bit fuzzy

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants