Repository navigation
Add (lazy) shape property to dataframe and series - #3212
Conversation
mrocklin
left a comment
There was a problem hiding this comment.
In general this seems sensible to me. Thanks for taking it on. I've left a couple of small comments.
| row_size = delayed(int)(self.size / col_size) | ||
| return delayed(tuple)((row_size, col_size)) | ||
| else: | ||
| return delayed(tuple)((self.size,)) |
There was a problem hiding this comment.
Given the type selection in here perhaps we should define this separately on the DataFrame and Series classes directly?
There was a problem hiding this comment.
Ok, I will separate it
| assert_eq(result, len(full.columns)) | ||
|
|
||
| result = d.a.shape[0].compute() | ||
| assert_eq(result, len(full.a)) |
There was a problem hiding this comment.
Would it work to call assert_eq directly on the delayed object like assert_eq(d.shape, full.shape)?
The assert_eq function also checks a few things on task graphs that we probably want to keep if possible
There was a problem hiding this comment.
I believe this is not possible. At least I got this message:
TypeError: Truth of Delayed objects is not supported
|
@mrocklin Please tell me if I need to change more stuff. Thanks |
|
It looks like this causes other tests to fail. I recommend clicking on the "details" link to the right of the travis-ci failure at the bottom of this PR (look for a red X). It looks like other parts of code are checking for the |
|
@mrocklin There is some method to check if a variable is delayed or not? I cannot find it The problem seems to be in |
…taFrame or a Series return immediately
| if isinstance(x, (dd.DataFrame, dd.Series)): | ||
| return False | ||
| else: | ||
| return (hasattr(x, '__array__') and |
There was a problem hiding this comment.
There are a couple problems with this:
- We can't assume that dask.dataframe will import cleanly here. Dask doesn't require users be able to install dask dataframe in order to use other systems, like dask.bag or dask.array
- Arguably
dd.Seriesis array like by this definition, no?
Perhaps instead .shape should return a tuple of delayed objects rather than a delayed tuple?
There was a problem hiding this comment.
+1 on returning a tuple of delayed objects rather than a delayed tuple.
There was a problem hiding this comment.
I agree with both of you and that was the first approach that I tried. The "problem" with a tuple of delayed objects is that we need to create a class dask.tuple, otherwise, tuple don't have the compute method and create it, maybe, is a bit overkill.
Somewhere on dask documentation, I saw a scratch of dask.tuple implementation. Do you know if it's usable?
There was a problem hiding this comment.
The top-level dask.compute() function is probably the best way to evaluate these.
I suppose the other option is to return a 1D dask array instead of a tuple.
There was a problem hiding this comment.
I think we should avoid that idea in order to be coherent with Pandas API.
There was a problem hiding this comment.
...the other option is to return a 1D dask array instead of a tuple.
This could also be turned into a tuple of ints once the 1-D Dask Array is created.
|
I'm still in favor of using dask.compute and a tuple of delayed/int objects.
…On Mon, Feb 26, 2018 at 9:13 PM, Stephan Hoyer ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In dask/utils.py
<#3212 (comment)>:
> @@ -893,6 +893,10 @@ def is_arraylike(x):
>>> is_arraylike('cat')
False
"""
- return (hasattr(x, '__array__') and
+ import dask.dataframe as dd
+ if isinstance(x, (dd.DataFrame, dd.Series)):
+ return False
+ else:
+ return (hasattr(x, '__array__') and
The top-level dask.compute() function is probably the best way to
evaluate these.
I suppose the other option is to return a 1D dask array instead of a tuple.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3212 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AASszE4XitHEGH1Io-yHdhBh2ooC3jnwks5tY2TYgaJpZM4STFvY>
.
|
|
Ok, I agree. This will take a bit more time to me to dig and understand the code under the hood. I just started using Dask last week, so I'm newbie on this but I will try anyway |
|
We appreciate your effort on this @henriqueribeiro . Please let us know if you have further questions and how we can help. |
|
Hey @mrocklin! Finally I found some time to work on this again. On this commits, and following your suggestion, I created a Tuple class for dask. Now it's possible to have a tuple of dask objects and then call compute. In the end it turned out to be a very simple implementation but I'm not 100% sure that I'm doing it correctly. Also, I saw that some tests are failing but I believe it's not because of the shape method or the tuple class. Can you please have a look on it? Thanks a lot |
|
I think that we should avoid creating a tuple class in Dask. While I agree that this might be useful sometimes it leads us down a path of recreating an entire language in Dask, which more than I think we want to accomplish. Instead I recommend that we just return a normal Python tuple that contains a computable object. Perhaps something like the following? class Series(...):
def shape(self):
return (self.size(),)
class DataFrame(...):
def shape(self):
return (self.size(), len(self.columns))This won't have the nice |
|
I understand your point, but the problem with that approach is that then you need to iterate over each element and compute it. Still, and despite the increase of the project's scope, this class is very very simple, only adding |
But now people can ask for dict, set, list and other similar classes. I am -1 on adding a tuple class. I strongly believe that it will create a non-trivial amount of maintenance burden in the future. You're quite welcome to create a Dask Tuple class, but I would prefer that it not be defined or used within the main codebase.
Yes, I agree that this is suboptimal. People would have to use the |
|
You, better than me, have a much better overview of the whole project and so, I trust on your decision. I will change to the implementation that you proposed. Off topic: there is a list of these small things/methods that should be implemented in order to have, approximately, the same API as Pandas? I would like to continue contributing to the project and maybe these methods are the best methods to get in. |
|
FWIW one can do |
|
Do you think is it ok now? |
|
In principle I agree with the changes here. However, it looks like the tests are failing. You can see the logs by clicking on "Details" in the travis-ci section above, and then click on any of the four failing builds: It looks like tests around numpy ufuncs have started to fail? I wonder if, now that we have a |
|
@mrocklin you are right. The tests are failing because now the dataframe/series have the shape method and, whereas in the past the value returned was -1 for the "shape", now it returns a delayed value. With this, how do you think is the best way to handle it? To compute the shape and proceed or replace the delayed values and proceed? |
|
Apologies if this has been discussed already, but have we considered returning a tuple of A common use of |
|
I feel like this has been discussed somewhere, but I can't find the issue.
Returning a tuple of (delayed, int) makes sense to me
…On Mon, Jul 23, 2018 at 7:31 AM, Tom Augspurger ***@***.***> wrote:
Apologies if this has been discussed already, but have we considered
returning a tuple of (delayed_n_rows, concrete_n_columns)? We know the
number of columns, so it needn't be delayed.
A common use of DataFrame.shape is getting the length of the second axis
in a pandas & numpy generic way. If we have the number of columns be
delayed, users will still need to check whether they have a dataframe or
array.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3212 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AASszLgeoRo2nxyA2dhtPZd7pbuf-oNFks5uJbP5gaJpZM4STFvY>
.
|
|
Thanks. @henriqueribeiro do you have time to implement this soon? I have a short-term need for it. I think something roughly like @property
def shape(self):
return (self.reduction(len, np.sum, token='len', meta=int, split_every=False),
len(self.columns))
|
|
Hey @TomAugspurger and @mrocklin Yes, we had that discusssion some time ago but in the end, and in the current implmentation, the shape returns a tuple with a delayed object and a scalar, exactly as you suggested. The only problem that I see is that some methods use the absence of shape method to check if it's dask or not (at least is my understanding). How can we overcome this? |
|
Sorry I should have looked closer at the implementation. On my quick skim I thought the suggestion was for both values of the tuple to be delayed.
Hmm are these the test failures present in, e.g., https://travis-ci.org/dask/dask/jobs/378727207? I'll take closer look later. |
|
@TomAugspurger exactly. Tell me if you need something from me |
|
Pushed some updates, we'll see if CI passes. This That's primarily to avoid having to inspect elements of iterables in We rely on |
|
We should get some input from @mrocklin |
|
Yeah, CI is passing now, but I'm not happy with the _unknown_shape hack.
I'm still thinking through how to avoid that.
…On Fri, Jul 27, 2018 at 7:36 AM henriqueribeiro ***@***.***> wrote:
We should get some input from @mrocklin <https://github.com/mrocklin>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3212 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABQHIjbA24HMXEvc9z0hmRDj1_x6v2XAks5uKwllgaJpZM4STFvY>
.
|
|
Just pushed a commit removing the Do you have a preference @mrocklin, @shoyer, or @jakirkham? |
|
This looks reasonable to me. |
|
Thanks. Merging tomorrow if there are no objections. |
Didn't actually know about that hack. Anyways sounds like an improvement to me. |

flake8 daskdocs/source/changelog.rstfor all changesand one of the
docs/source/*-api.rstfiles for new APISince dask provides (lazy)
size, and in order to get close to Pandas API, I propose an implementation ofshapeproperty, both for Dataframes and Series.Tests Example
Imports
Details
Generation Function
Details
Generate df and convert to Dask
Details
Using Shape on DataFrame
Details
Get only row lenght
Using Shape on Series
Details