Repository navigation
Feature/groupby reductions multiple levels - #1692
Conversation
|
The rebase to master has some strange interaction with my code. I'll investigate. |
| call(ddf.groupby([ddf['a'], ddf['b']])['c'], agg_func)) | ||
|
|
||
| assert_eq(call(pdf.groupby([pdf['a'] > 2, pdf['b'] > 1])['c'], agg_func), | ||
| call(ddf.groupby([ddf['a'] > 2, ddf['b'] > 1])['c'], agg_func)) |
There was a problem hiding this comment.
It might be nice to extend one of the existing tests to include multi-series groupers. For example you could add new groupers to the for loop in test_split_apply_combine_on_series.
There was a problem hiding this comment.
Good idea. Once I figure out the reasons for the segfaults, I will adapt the existing tests.
|
Ok, at first blush the segfaulting seems to be a little bit more complicated. There has to be some kind of race condition introduced in my code. If I use the synchronous scheduler, everything seems to run (at least locally). Specifically, there is no segfault, if the last test looks like: As a meta question: I cannot run travis without updating the PR, or can I? |
|
I suspect that you only have the ability to restart tests if you own the repository. You might be able to do this if you were to open a PR to your own fork. When I run into a segfault I usually try to distill it down to a small example and then share it with others. If possible I try to remove dask.dataframe from the example and see if I can reproduce with just Pandas and threads. If so then I kick it over to them as an issue and they can usually handle it fairly quickly. |
|
Sorry, With my local test setup (python version and dependency versions), I could not reproduce this issue at first. However, @sinhrks was so kind to give a pointer how to setup travis with my fork. The next PR will be extensively tested with travis first. Thanks for the debugging hint. Luckily, it was a bug in my code, that was easy to find by tracing the execution flow. However, there still seems to a possible segfault when passing a dask series directly to |
The segfault was also found here: #1616 (comment). |
|
What is the status on this? |
|
You mean the PR? I'm somewhat swamped at the moment. The implementation should be pretty feature complete, but I'd like to extend the tests. In particular, along the lines of your suggestion. Furthermore, I'd like to extend the tests in the direction of grouped series. I somehow overlooked that series can be grouped. Maybe this PR already fixes parts of the series-aggregate problem I stumbled upon in #1678 . |
|
OK, good to know. Don't mean to apply pressure. I mostly asked to see if things are blocking on lack of review. |
|
I extended the tests and ran of course into a couple of problems. In particular related to Further, I have yet to get Finally, for python3.4, the The tests marked with |
|
Changes:
The only issues remaining are groupby-applys on multiple series that are not simple column-selects, i.e., Since I am now touching existing code paths, I really would appreciate a through feedback, in particular of the nunique changes. |
mrocklin
left a comment
There was a problem hiding this comment.
Sorry for the delay in review. This is looking pretty good to me. Some small requests follow:
| elif (isinstance(self.index, list) and | ||
| any(isinstance(item, Series) for item in self.index)): | ||
| # TODO: implement this | ||
| raise NotImplementedError("raise an error to prevent a segfault") |
There was a problem hiding this comment.
This should have a better error message that users will understand. Something along the lines of "lists of Series not accepted in ...". The segfault text should be kept around as a comment.
There was a problem hiding this comment.
I haven't checked yet (working down through code) but it would be good to have an xfailed test for this if you don't have one already.
There was a problem hiding this comment.
The error message is changed, the x-failed tests were already part of the test-suite (test_full_groupby_multilevel and test_apply_shuffle_multilevel).
| """ | ||
| Return whether index is a Series sliced from df | ||
| """ | ||
| # TODO: handle list of series |
There was a problem hiding this comment.
Is this TODO now handled below?
There was a problem hiding this comment.
While trying to figure out this todo. If noticed that after index normalization quite some code could be simplified. I adapted the creation of _meta and _meta_nonmepty accordingly. I also added tests for the content of the _meta objects.
| def _get_groupby_chunk_args(obj, index): | ||
| if not isinstance(index, list): | ||
| return [obj, index] | ||
|
|
There was a problem hiding this comment.
I would remove this whitespace. The if and else clauses seem related
There was a problem hiding this comment.
I removed the whitespace.
| if isinstance(index, (tuple, list)) and len(index) > 1: | ||
| return list(range(len(index))) | ||
| else: | ||
| return 0 |
There was a problem hiding this comment.
It would be nice to have some text, perhaps just as a long comment in the code, about how these small functions _determine_levels, _get_groupby_chunk_args, etc. work together. This would help when maintaining this code and would also help newer developers.
There was a problem hiding this comment.
I added a module level description and short docstring to each function.
| msg = "Grouper for '{0}' not 1-dimensional" | ||
| raise ValueError(msg.format(index[0])) | ||
| #msg = "Grouper for '{0}' not 1-dimensional" | ||
| #raise ValueError(msg.format(index[0])) |
There was a problem hiding this comment.
Should these lines remain or be deleted?
There was a problem hiding this comment.
I adapted the logic to cover currently supported index values and added a test case to make sure exceptions are raised for unsupported ones.
| call(ddf['c'].groupby(grouper(ddf)), agg_func, split_every=2), | ||
| # for pandas ~ 0.18, the name is not not properly propagated for | ||
| # the mean aggregation | ||
| check_names=(agg_func not in {'mean', 'nunique'})) |
|
Thanks for the review. Once travis is green, I'll push the changes. Update: changes are pushed. |
|
So, I'm quite happy with the current state. How about you, the dask maintainers? The only functionality related to |
|
@jcrist said he'd probably be able to take a closer look later today. It'd be good to get his thoughts. |
mrocklin
left a comment
There was a problem hiding this comment.
This looks pretty good to me. Most of my comments are stylistic.
This tweet came up today, which seemed timely: https://twitter.com/dan_abramov/status/793122481695559680 :)
|
|
||
|
|
||
| def _get_groupby_chunk_args(obj, index): | ||
| """Get the object to group and any partioned index item as list of varargs. |
| return index | ||
|
|
||
|
|
||
| def _get_groupby_chunk_args(obj, index): |
There was a problem hiding this comment.
This function is small enough that we might just want to inline it as a ternary expression
# _get_groupby_chunk_args(obj, index) # before
[obj, index] if isinstance(index, list) else [obj] + index # after
I think that repeating this a few times is better than having the extra function indirection, which makes code harder to follow and maintain in the future.
| """Get an index suitable for grouping meta objects. | ||
|
|
||
| Note, the index must be normalized. The ``meta_attribute`` should be | ||
| either ``'_meat'`` or ``'_meta_nonempty'``. |
| return [obj] + index | ||
|
|
||
|
|
||
| def _get_index_meta(index, meta_attribute): |
There was a problem hiding this comment.
We might also want to inline this function. It's slightly more complex but only used in two places.
| return index | ||
|
|
||
|
|
||
| def _do_index_partions_align(df, index): |
There was a problem hiding this comment.
This function is only used once and is only a few lines long. Lets just inline it for increased code locality.
|
|
||
| # DataFrameGroupBy with column slice | ||
| assert_eq(ddf.groupby(grouper(ddf))[['B', 'C']].apply(lambda x: x.sum()), | ||
| pdf.groupby(grouper(pdf))[['B', 'C']].apply(lambda x: x.sum())) |
| # | ||
| # Finally, the ``_get_index_meta`` allows to retrieve a index suitable for | ||
| # usage in groupby operations of the meta objects. Both empty and nonempty meta | ||
| # objects are supported. |
|
Apologies for not getting to this yesterday. Overall this looks pretty good to me. I agree with Matt on the inlining comments above - should help improve readability. Thanks for your work! |
|
Thanks for your feedback. I will adapt the code accordingly. I hope I get to work on the PR tomorrow. |
|
I inlined the functions as requested. To be honest, my weak preference would have been to keep separate functions, as they give additional structure to the code. |
|
This looks fine to me. @jcrist ?
Yeah, I appreciate that my preferences here are stylistic and subjective. My general experience has been that structure feels good when you're writing code and already understand it fully but that structure feels like a burden when you're reading code for the first time or reviewing it. Structure highlights common patterns, but also adds extra steps before someone can understand what is going on. There is a tradeoff here that we need to balance each time. Do you mind if we fall on the side of less structure in this case? |
Thanks for the explanation. Don't get me wrong, I'm perfectly on board with the flat structure :) In the end this contribution has to fit well into the overall dask source. While I tried to follow existing conventions, I did surely deviate at places. My comment was meant as a mere explanation of my choices (and preferences). In the end, you, as a maintainer, have to feel comfortable with the code and I'm happy to adapt any of my changes accordingly. |
Fortunately I am increasingly being replaced as the maintainer of dask.dataframe by a set of developers such as yourself coming up to take it over. As this happens presumably my views and coding styles should have less prominence and the views/styles of the community should take more authority. |
|
Merging in 12 hours if no comments |
While working on #1678, I found that the aggregates
std,var,nuniquefordf.groupby(...)do not with multiple groupby columns. Further, grouping of multiple series that are transformations of columns is not supported.Tests that fail with dask master for these cases are attached.