-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: merge_asof() has type specializations and can take multiple 'by' parameters (#13936) #14783
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 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
75157fc
merge_asof() has type specializations and can take multiple 'by' para…
f01142c
Merge master branch
46cc309
Update documentation
c33c4cb
Merge branch 'master' into GH13936
5eeb7d9
Merge master into GH13936
fafbb02
Updated benchmarks to reflect new ASV setup
2bce3cc
Revert dict back to PyObjectHashTable in response to code review
0ad1687
Fixed whatsnew
89256f0
Test 8-bit integers and raise error on 16-bit floats; add comments
77eb47b
Merge master branch into GH13936
1f208a8
Use tuple representation instead of strings
ffcf0c2
Added test to reject float16; fixed typos
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
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
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
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
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 |
---|---|---|
|
@@ -28,7 +28,8 @@ | |
is_list_like, | ||
_ensure_int64, | ||
_ensure_float64, | ||
_ensure_object) | ||
_ensure_object, | ||
_get_dtype) | ||
from pandas.types.missing import na_value_for_dtype | ||
|
||
from pandas.core.generic import NDFrame | ||
|
@@ -270,8 +271,8 @@ def merge_asof(left, right, on=None, | |
DataFrame whose 'on' key is less than or equal to the left's key. Both | ||
DataFrames must be sorted by the key. | ||
|
||
Optionally perform group-wise merge. This searches for the nearest match | ||
on the 'on' key within the same group according to 'by'. | ||
Optionally match on equivalent keys with 'by' before searching for nearest | ||
match with 'on'. | ||
|
||
.. versionadded:: 0.19.0 | ||
|
||
|
@@ -288,9 +289,8 @@ def merge_asof(left, right, on=None, | |
Field name to join on in left DataFrame. | ||
right_on : label | ||
Field name to join on in right DataFrame. | ||
by : column name | ||
Group both the left and right DataFrames by the group column; perform | ||
the merge operation on these pieces and recombine. | ||
by : column name or list of column names | ||
Match on these columns before performing merge operation. | ||
suffixes : 2-length sequence (tuple, list, ...) | ||
Suffix to apply to overlapping column names in the left and right | ||
side, respectively | ||
|
@@ -926,27 +926,44 @@ def get_result(self): | |
return result | ||
|
||
|
||
_asof_functions = { | ||
'int64_t': _join.asof_join_int64_t, | ||
'double': _join.asof_join_double, | ||
} | ||
def _asof_function(on_type): | ||
return getattr(_join, 'asof_join_%s' % on_type, None) | ||
|
||
|
||
def _asof_by_function(on_type, by_type): | ||
return getattr(_join, 'asof_join_%s_by_%s' % (on_type, by_type), None) | ||
|
||
_asof_by_functions = { | ||
('int64_t', 'int64_t'): _join.asof_join_int64_t_by_int64_t, | ||
('double', 'int64_t'): _join.asof_join_double_by_int64_t, | ||
('int64_t', 'object'): _join.asof_join_int64_t_by_object, | ||
('double', 'object'): _join.asof_join_double_by_object, | ||
} | ||
|
||
_type_casters = { | ||
'int64_t': _ensure_int64, | ||
'double': _ensure_float64, | ||
'object': _ensure_object, | ||
} | ||
|
||
_cyton_types = { | ||
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. _cython_types ? |
||
'uint8': 'uint8_t', | ||
'uint32': 'uint32_t', | ||
'uint16': 'uint16_t', | ||
'uint64': 'uint64_t', | ||
'int8': 'int8_t', | ||
'int32': 'int32_t', | ||
'int16': 'int16_t', | ||
'int64': 'int64_t', | ||
'float16': 'float', | ||
'float32': 'float', | ||
'float64': 'double', | ||
} | ||
|
||
|
||
def _get_cython_type(dtype): | ||
""" Given a dtype, return 'int64_t', 'double', or 'object' """ | ||
""" Given a dtype, return a C name like 'int64_t' or 'double' """ | ||
type_name = _get_dtype(dtype).name | ||
ctype = _cyton_types.get(type_name, 'object') | ||
return ctype | ||
|
||
|
||
def _get_cython_type_upcast(dtype): | ||
""" Upcast a dtype to 'int64_t', 'double', or 'object' """ | ||
if is_integer_dtype(dtype): | ||
return 'int64_t' | ||
elif is_float_dtype(dtype): | ||
|
@@ -990,9 +1007,6 @@ def _validate_specification(self): | |
if not is_list_like(self.by): | ||
self.by = [self.by] | ||
|
||
if len(self.by) != 1: | ||
raise MergeError("can only asof by a single key") | ||
|
||
self.left_on = self.by + list(self.left_on) | ||
self.right_on = self.by + list(self.right_on) | ||
|
||
|
@@ -1046,6 +1060,11 @@ def _get_merge_keys(self): | |
def _get_join_indexers(self): | ||
""" return the join indexers """ | ||
|
||
def flip_stringify(xs): | ||
""" flip an array of arrays and string-ify contents """ | ||
xt = np.transpose(xs) | ||
return _join.stringify(_ensure_object(xt)) | ||
|
||
# values to compare | ||
left_values = self.left_join_keys[-1] | ||
right_values = self.right_join_keys[-1] | ||
|
@@ -1067,22 +1086,23 @@ def _get_join_indexers(self): | |
|
||
# a "by" parameter requires special handling | ||
if self.by is not None: | ||
left_by_values = self.left_join_keys[0] | ||
right_by_values = self.right_join_keys[0] | ||
|
||
# choose appropriate function by type | ||
on_type = _get_cython_type(left_values.dtype) | ||
by_type = _get_cython_type(left_by_values.dtype) | ||
if len(self.left_join_keys) > 2: | ||
# get string representation of values if more than one | ||
left_by_values = flip_stringify(self.left_join_keys[0:-1]) | ||
right_by_values = flip_stringify(self.right_join_keys[0:-1]) | ||
else: | ||
left_by_values = self.left_join_keys[0] | ||
right_by_values = self.right_join_keys[0] | ||
|
||
on_type_caster = _type_casters[on_type] | ||
# upcast 'by' parameter because HashTable is limited | ||
by_type = _get_cython_type_upcast(left_by_values.dtype) | ||
by_type_caster = _type_casters[by_type] | ||
func = _asof_by_functions[(on_type, by_type)] | ||
|
||
left_values = on_type_caster(left_values) | ||
right_values = on_type_caster(right_values) | ||
left_by_values = by_type_caster(left_by_values) | ||
right_by_values = by_type_caster(right_by_values) | ||
|
||
# choose appropriate function by type | ||
on_type = _get_cython_type(left_values.dtype) | ||
func = _asof_by_function(on_type, by_type) | ||
return func(left_values, | ||
right_values, | ||
left_by_values, | ||
|
@@ -1092,12 +1112,7 @@ def _get_join_indexers(self): | |
else: | ||
# choose appropriate function by type | ||
on_type = _get_cython_type(left_values.dtype) | ||
type_caster = _type_casters[on_type] | ||
func = _asof_functions[on_type] | ||
|
||
left_values = type_caster(left_values) | ||
right_values = type_caster(right_values) | ||
|
||
func = _asof_function(on_type) | ||
return func(left_values, | ||
right_values, | ||
self.allow_exact_matches, | ||
|
Oops, something went wrong.
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.
why do you need this again? (I see you are using it), but what is the input that you are giving it?
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.
maybe a doc-string would help
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've added a couple comments to address this. When the
by
parameter has multiple entries, then we want to store the entire array in the hash table. Unfortunately, NumPy arrays aren't hashable. After lots of digging, the fastest thing to do is to convert to a string.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.
oh if all u need is hashing we just added this:
https://github.com/pandas-dev/pandas/blob/master/pandas/tools/hashing.py
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.
oh if all u need is hashing we just added this:
https://github.com/pandas-dev/pandas/blob/master/pandas/tools/hashing.py