Skip to content

Add DataFrame.to_dask_array #3090

Closed
Closed

Description

@TomAugspurger

We have DataFrame.values, but this results in chunks of unknown size. If we happen to know the size of each partition / chunk, then we could do df.to_dask_array(chunk_lengths).

Basically,

In [35]: df = dd.from_pandas(pd.DataFrame(np.random.uniform(size=(100, 5))), 10)

In [36]: lengths = df.map_partitions(len).compute().values  # user knows this somehow

In [37]: blocks = []

In [38]: for block, length in zip(df.to_delayed(), lengths):
    ...:     blocks.append(da.from_delayed(block.values, (length, len(df.columns)), 'f8'))
    ...:

In [39]: arr = da.vstack(blocks)

In [40]: arr
Out[40]: dask.array<concatenate, shape=(100, 5), dtype=float64, chunksize=(10, 5)>

Activity

  1. mrocklin commented on Jan 22, 2018

    @mrocklin
    Member

    This seems like a good idea to me. Presumably it would also help with roundtripping arrays -> dataframes -> arrays. We might consider accepting a chunks= keyword to maintain uniformity with other I/O functions. The docstring should probably have a loud disclaimer that this doesn't mean that Dask will enforce this chunk sizing, but rather that you the user are promising that it is the case.

  2. jorisvandenbossche commented on Jan 22, 2018

    @jorisvandenbossche
    Member

    Could there be an option to specify that dask itself figures out the chunk sizes? Or should we require the user to pass it?
    It isn't difficult to do as shown above, the user should just be aware that it is materializing the full dataframe to get the lenghts, so it should probably not be the default (but in certain cases the user will otherwise to exactly this him/herself).

  3. mrocklin commented on Jan 22, 2018

    @mrocklin
    Member
  4. added
    good first issue
    on Jan 30, 2018
  5. cr458 commented on Feb 12, 2018

    @cr458
    Contributor

    Could I have a go at this? Provided no one else has already started.

  6. mrocklin commented on Feb 12, 2018

    @mrocklin
    Member

    That would be quite welcome @cr458 . I recommend leveraging dd.map_partitions with a function that produces a numpy array. This will produce the appropriate dask.array without good chunk information. You should then be able to modify ._chunks explicitly with provided chunk information. You may want to read more about chunks here: http://dask.pydata.org/en/latest/array-design.html#chunks

  7. cr458 commented on Mar 17, 2018

    @cr458
    Contributor

    @mrocklin apologies for the delay. I have been thinking about how to approach this given my lack of familiarity with dask.
    From my understanding a function that produces a numpy array using dd.map_partitions would look something like df.map_partitions(lambda x: x.values) which would produce a dask.array. We would then explicitly set the ._chunks attribute of the dask.array with a tuple of tuples which were passed by the user. Is there a reason that @TomAugspurger looped through the blocks rather than using dd.map_partitions and explicitly setting the chunks afterwards?

  8. TomAugspurger commented on Mar 17, 2018

    @TomAugspurger
    MemberAuthor

    @cr458 has map_partitions always been able to return a dask array, or is that a new addition?

  9. cr458 commented on Mar 17, 2018

    @cr458
    Contributor

    @TomAugspurger thanks for replying! I'm not sure actually, am fairly unfamiliar with dask. I only asked because @mrocklin suggested using dd.map_partitions, so wanted to check if there was a particular reason/preference for either method.

  10. TomAugspurger commented on Mar 17, 2018

    @TomAugspurger
    MemberAuthor
  11. mrocklin commented on Mar 17, 2018

    @mrocklin
    Member

    Map partitions is now smarter about creating a proper dask array than it was when this issue was created. A simple implementation of this would be the following:

    def to_dask_array(self, lengths):
        x = self.values
        x.chunks = (tuple(lengths), (len(self.columns),))
        return x

    Or the same but with to_records to create a record array. Given the simplicity of this now I wonder if it might make sense to forego creating a new method here, and maybe just make it easier to define chunk sizes on arrays with unknown chunks. This is somewhat related to #3293

  12. TomAugspurger commented on Mar 17, 2018

    @TomAugspurger
    MemberAuthor
  13. mrocklin commented on Mar 17, 2018

    @mrocklin
    Member
  14. cr458 commented on Mar 18, 2018

    @cr458
    Contributor

    Thank you for your replies, that clears things up considerably for me!
    From looking at #3293 it seems that there are two possible scenarios: one where the sizes are known beforehand, and one where the sizes are computed.
    For both cases we'd eventually set the sizes with something like x.chunks = (tuple(lengths), (len(self.columns),)), but I guess this will be addressed in the other PR and this one is no longer required?

  15. TomAugspurger commented on Mar 18, 2018

    @TomAugspurger
    MemberAuthor

    @cr458, I'm not sure if #3239 has settled on a consensus, but IIUC it will provide two things

    1. A helper for discovering the chunk sizes of unknown chunks
    2. A public API for converting an array with unknown chunks to known chunks.

    If we already know the chunk sizes, we can already achieve 2 by setting the private x._chunks value.

  16. cr458 commented on Mar 18, 2018

    @cr458
    Contributor

    @TomAugspurger great, thanks for clearing things up. I will look for another PR to contribute to in the mean time as am keen to help out where I can.

  17. jakirkham commented on Mar 28, 2018

    @jakirkham
    Member

    So you may find this comment interesting, @cr458. Expect calling to_records and employing that strategy to be reasonably effective.

  18. added a commit that references this issue on Aug 18, 2018
  19. added a commit that references this issue on Aug 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dataframegood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions