Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented GroupBy.median() #1957

Merged
merged 2 commits into from
Dec 11, 2020
Merged

Conversation

itholic
Copy link
Contributor

@itholic itholic commented Dec 9, 2020

This PR proposes GroupBy.median().

Note: the result can be slightly different from pandas since we use an approximated median based upon approximate percentile computation because computing median across a large dataset is extremely expensive.

>>> kdf = ks.DataFrame({'a': [1., 1., 1., 1., 2., 2., 2., 3., 3., 3.],
...                     'b': [2., 3., 1., 4., 6., 9., 8., 10., 7., 5.],
...                     'c': [3., 5., 2., 5., 1., 2., 6., 4., 3., 6.]},
...                    columns=['a', 'b', 'c'],
...                    index=[7, 2, 4, 1, 3, 4, 9, 10, 5, 6])
>>> kdf
      a     b    c
7   1.0   2.0  3.0
2   1.0   3.0  5.0
4   1.0   1.0  2.0
1   1.0   4.0  5.0
3   2.0   6.0  1.0
4   2.0   9.0  2.0
9   2.0   8.0  6.0
10  3.0  10.0  4.0
5   3.0   7.0  3.0
6   3.0   5.0  6.0

>>> kdf.groupby('a').median().sort_index()  # doctest: +NORMALIZE_WHITESPACE
       b    c
a
1.0  2.0  3.0
2.0  8.0  2.0
3.0  7.0  4.0

>>> kdf.groupby('a')['b'].median().sort_index()
a
1.0    2.0
2.0    8.0
3.0    7.0
Name: b, dtype: float64

ref #1929

@itholic
Copy link
Contributor Author

itholic commented Dec 9, 2020

FYI: we're doing same thing in median for DataFrame and Series.

def median(self, axis=None, numeric_only=True, accuracy=10000) -> Union[Scalar, "Series"]:
"""
Return the median of the values for the requested axis.
.. note:: Unlike pandas', the median in Koalas is an approximated median based upon
approximate percentile computation because computing median across a large dataset
is extremely expensive.

@codecov-io
Copy link

codecov-io commented Dec 9, 2020

Codecov Report

Merging #1957 (e121472) into master (a68717d) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1957   +/-   ##
=======================================
  Coverage   94.59%   94.60%           
=======================================
  Files          49       49           
  Lines       10882    10890    +8     
=======================================
+ Hits        10294    10302    +8     
  Misses        588      588           
Impacted Files Coverage Δ
databricks/koalas/missing/groupby.py 100.00% <ø> (ø)
databricks/koalas/groupby.py 91.58% <100.00%> (+0.07%) ⬆️
databricks/koalas/series.py 96.90% <0.00%> (+0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a68717d...e121472. Read the comment docs.

@xinrong-meng
Copy link
Contributor

Otherwise, LGTM!

Copy link
Collaborator

@ueshin ueshin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you fix the conflicts as well?
Otherwise, LGTM.

databricks/koalas/groupby.py Outdated Show resolved Hide resolved
@@ -2343,6 +2344,73 @@ def get_group(self, name) -> Union[DataFrame, Series]:

return DataFrame(internal)

def median(self, numeric_only=True, accuracy=10000) -> Union[DataFrame, Series]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are numeric_only and accuracy unknown types?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add more type hints for function arguments in the separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll add in the separated PR.

Thanks for the review!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, anyway numeric_only only supports for True since this is only for pandas compatibility.

Should we add type hints as bool though ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it must be bool, and accuracy must be int?
There are other similar places in the file or the other files, we can do it in a batch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I will take a look and add type hints throughout the whole file next week !

@ueshin
Copy link
Collaborator

ueshin commented Dec 11, 2020

Thanks! I'd merge this now.
Let's add more type hints for function arguments later.

@ueshin ueshin merged commit 78b1004 into databricks:master Dec 11, 2020
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.

4 participants