You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 somehowIn [37]: blocks= []
In [38]: forblock, lengthinzip(df.to_delayed(), lengths):
...: blocks.append(da.from_delayed(block.values, (length, len(df.columns)), 'f8'))
...:
In [39]: arr=da.vstack(blocks)
In [40]: arrOut[40]: dask.array<concatenate, shape=(100, 5), dtype=float64, chunksize=(10, 5)>
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.
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).
On Mon, Jan 22, 2018 at 11:07 AM, Joris Van den Bossche < ***@***.***> wrote:
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).
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3090 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AASszHwVfvXjSr3IVZrScnm0syZtbivDks5tNLI_gaJpZM4RmrWM>
.
@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?
@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.
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:
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
I could imagine cases where the dask.dataframe user knows the sizes
(reading from a parquet file that has the sizes for example), so a compute
could be avoided.
On Sat, Mar 17, 2018 at 2:59 PM, Matthew Rocklin ***@***.***> wrote:
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
<#3293>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3090 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABQHIiQdrJ07SYsF99YaHPiMqveoLe1Lks5tfWsKgaJpZM4RmrWM>
.
Agreed. I think that some of the conversation in that issue is about that
possibility as well.
On Sat, Mar 17, 2018 at 4:03 PM, Tom Augspurger <[email protected]>
wrote:
I could imagine cases where the dask.dataframe user knows the sizes
(reading from a parquet file that has the sizes for example), so a compute
could be avoided.
On Sat, Mar 17, 2018 at 2:59 PM, Matthew Rocklin ***@***.***
>
wrote:
> 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
> <#3293>
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <#3090 (comment)>, or
mute
> the thread
> <https://github.com/notifications/unsubscribe-auth/
ABQHIiQdrJ07SYsF99YaHPiMqveoLe1Lks5tfWsKgaJpZM4RmrWM>
> .
>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3090 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AASszIJSst-qKgkZo3dKokyve5C0dRLhks5tfWwtgaJpZM4RmrWM>
.
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?
Activity
mrocklin commentedon Jan 22, 2018
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.jorisvandenbossche commentedon Jan 22, 2018
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).
mrocklin commentedon Jan 22, 2018
cr458 commentedon Feb 12, 2018
Could I have a go at this? Provided no one else has already started.
mrocklin commentedon Feb 12, 2018
That would be quite welcome @cr458 . I recommend leveraging
dd.map_partitionswith 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._chunksexplicitly with provided chunk information. You may want to read more about chunks here: http://dask.pydata.org/en/latest/array-design.html#chunkscr458 commentedon Mar 17, 2018
@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_partitionswould look something likedf.map_partitions(lambda x: x.values)which would produce a dask.array. We would then explicitly set the._chunksattribute 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 usingdd.map_partitionsand explicitly setting the chunks afterwards?TomAugspurger commentedon Mar 17, 2018
@cr458 has
map_partitionsalways been able to return a dask array, or is that a new addition?cr458 commentedon Mar 17, 2018
@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.TomAugspurger commentedon Mar 17, 2018
mrocklin commentedon Mar 17, 2018
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:
Or the same but with
to_recordsto 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 #3293TomAugspurger commentedon Mar 17, 2018
mrocklin commentedon Mar 17, 2018
cr458 commentedon Mar 18, 2018
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?TomAugspurger commentedon Mar 18, 2018
@cr458, I'm not sure if #3239 has settled on a consensus, but IIUC it will provide two things
If we already know the chunk sizes, we can already achieve 2 by setting the private
x._chunksvalue.cr458 commentedon Mar 18, 2018
@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.
jakirkham commentedon Mar 28, 2018
So you may find this comment interesting, @cr458. Expect calling
to_recordsand employing that strategy to be reasonably effective.ENH: DataFrame.to_dask_array
ENH: DataFrame.to_dask_array (#3884)