-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-7188] added python support for math DataFrame functions #5750
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3ee0c05
added python functions
brkyvz 33c2c15
fixed python style
brkyvz 7b7d7c4
remove tests for removed methods
brkyvz d3f7e0f
addressed comments and added tests
brkyvz 25e6534
addressed comments v2.0
brkyvz d5dca3f
moved math functions to mathfunctions
brkyvz 3c4adde
cleanup imports
brkyvz 7c4f563
removed is_math
brkyvz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
addressed comments and added tests
- Loading branch information
commit d3f7e0fa2c054aef5b048bad588e81bcfebe94ab
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,11 +40,24 @@ def _function_obj(sc, is_math=False): | |
| return sc._jvm.mathfunctions | ||
|
|
||
|
|
||
| def _create_function(name, doc="", is_math=False): | ||
| def _create_function(name, doc="", is_math=False, binary=False): | ||
| """ Create a function for aggregator by name""" | ||
| def _(col): | ||
| def _(col1, col2=None): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is somewhat strange to have col2 be default None. I think it's easier if we just create a _create_binary_function function. |
||
| sc = SparkContext._active_spark_context | ||
| jc = getattr(_function_obj(sc, is_math), name)(col._jc if isinstance(col, Column) else col) | ||
| if not binary: | ||
| jc = getattr(_function_obj(sc, is_math), name)(col1._jc if isinstance(col1, Column) | ||
| else col1) | ||
| else: | ||
| assert col2, "The second column for %s not provided!" % name | ||
| # users might write ints for simplicity. This would throw an error on the JVM side. | ||
| if type(col1) is int: | ||
| col1 = col1 * 1.0 | ||
| if type(col2) is int: | ||
| col2 = col2 * 1.0 | ||
| jc = getattr(_function_obj(sc, is_math), name)(col1._jc if isinstance(col1, Column) | ||
| else col1, | ||
| col2._jc if isinstance(col2, Column) | ||
| else col2) | ||
| return Column(jc) | ||
| _.__name__ = name | ||
| _.__doc__ = doc | ||
|
|
@@ -107,14 +120,25 @@ def _(col): | |
| 'measured in radians.' | ||
| } | ||
|
|
||
| # math functions that take two arguments as input | ||
| _binary_math_functions = { | ||
| 'atan2': 'Returns the angle theta from the conversion of rectangular coordinates (x, y) to' + | ||
| 'polar coordinates (r, theta).', | ||
| 'hypot': 'Computes `sqrt(a^2^ + b^2^)` without intermediate overflow or underflow.', | ||
| 'pow': 'Returns the value of the first argument raised to the power of the second argument.' | ||
| } | ||
|
|
||
|
|
||
| for _name, _doc in _functions.items(): | ||
| globals()[_name] = _create_function(_name, _doc) | ||
| for _name, _doc in _math_functions.items(): | ||
| globals()[_name] = _create_function(_name, _doc, True) | ||
| for _name, _doc in _binary_math_functions.items(): | ||
| globals()[_name] = _create_function(_name, _doc, True, True) | ||
| del _name, _doc | ||
| __all__ += _functions.keys() | ||
| __all__ += _math_functions.keys() | ||
| __all__ += _binary_math_functions.keys() | ||
| __all__.sort() | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if you got my previous comment. it might be easier if is_math just takes a jvm object, rather than using an extra _function_obj
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did... The problem is, I can't pass in
scwhen it was outside this function. I guess the problem was globally callingsc = SparkContext._active_spark_context, meaning outside of a function. That's why I had to have this hack.