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

Implement Series.aggregate and agg #816

Merged
merged 8 commits into from
Sep 23, 2019
Merged

Conversation

itholic
Copy link
Contributor

@itholic itholic commented Sep 22, 2019

Like pandas Series.aggregate (https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.aggregate.html)

I implemented aggregate function for series.

Example:

>>> s = ks.Series([1, 2, 3, 4])
>>> s
0    1
1    2
2    3
3    4
Name: 0, dtype: int64

>>> s.agg('min')
1

>>> s.agg(['min', 'max'])
min    1
max    4
Name: 0, dtype: int64

(above example is using pandas one)

스크린샷 2019-09-23 오전 12 01 32

@codecov-io
Copy link

codecov-io commented Sep 22, 2019

Codecov Report

Merging #816 into master will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #816      +/-   ##
==========================================
+ Coverage   94.34%   94.36%   +0.02%     
==========================================
  Files          32       32              
  Lines        5849     5854       +5     
==========================================
+ Hits         5518     5524       +6     
+ Misses        331      330       -1
Impacted Files Coverage Δ
databricks/koalas/missing/series.py 100% <ø> (ø) ⬆️
databricks/koalas/frame.py 96.89% <ø> (+0.06%) ⬆️
databricks/koalas/series.py 95.22% <100%> (+0.05%) ⬆️

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 f6f27b0...cbcb502. Read the comment docs.

raise ValueError("If the given function is a list, it "
"should only contains function names as strings.")
elif isinstance(func, str):
return eval("self.{}()".format(func))
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 avoid eval() as far as possible. getattr(self, func)() instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ueshin Thanks for review ueshin :) I totally agree. fixed it !

if isinstance(func, list):
if all((isinstance(f, str) for f in func)):
rows = OrderedDict((f, eval("self.{}()".format(f), dict(self=self))) for f in func)
return Series(rows)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This runs Spark jobs many times. In this case, I think we can reuse DataFrame's aggregate.

Copy link
Contributor Author

@itholic itholic Sep 23, 2019

Choose a reason for hiding this comment

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

@ueshin Thanks again!! fixed it :)
Anyway I have a question, (It may be a very basic question though 😿 )
is it right the every function call in OrderedDict comprehension(when run eval) call spark job each time?

Copy link
Collaborator

Choose a reason for hiding this comment

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

yes, each aggregate function for Series call triggers sdf.head(2) in

def _unpack_scalar(sdf):
"""
Takes a dataframe that is supposed to contain a single row with a single scalar value,
and returns this value.
"""
l = sdf.head(2)
assert len(l) == 1, (sdf, l)
row = l[0]
l2 = list(row.asDict().values())
assert len(l2) == 1, (row, l2)
return l2[0]
to return the result value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ueshin Oh now i totally got it. Thanks ueshin!! 😃

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.

Otherwise, LGTM, pending tests.

@@ -19,7 +19,7 @@
"""
import re
import inspect
from collections import Iterable
from collections import Iterable, OrderedDict
Copy link
Collaborator

@ueshin ueshin Sep 23, 2019

Choose a reason for hiding this comment

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

nit: no need this change anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ueshin right. i just removed it :)

@softagram-bot
Copy link

Softagram Impact Report for pull/816 (head commit: cbcb502)

⭐ Change Overview

Showing the changed files, dependency changes and the impact - click for full size
(Open in Softagram Desktop for full details)

📄 Full report

Impact Report explained. Give feedback on this report to [email protected]

@ueshin
Copy link
Collaborator

ueshin commented Sep 23, 2019

Thanks! merging.

@ueshin ueshin merged commit 3e263df into databricks:master Sep 23, 2019
thoo added a commit to thoo/koalas that referenced this pull request Sep 24, 2019
* upstream/master:
  Updated the koalas logo in readme.md
  Adding koalas-logo without label
  Adding Koalas logo to readme
  Adding koalas logo
  Clean pandas usage in frame.agg (databricks#821)
  Implement Series.aggregate and agg (databricks#816)
  Raise a more helpful error for duplicated columns in Join (databricks#820)
@itholic itholic deleted the series_agg branch September 25, 2019 02:03
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