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
Enh mappable remapper #4490
Enh mappable remapper #4490
Changes from 3 commits
47b7fc8
270144b
6657e65
08edd22
48d1cfa
bbd7356
1e5ae2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
import six | ||
from six.moves import xrange, zip | ||
from itertools import repeat | ||
from functools import wraps | ||
|
||
import datetime | ||
import errno | ||
|
@@ -2406,6 +2407,132 @@ def get_instancemethod(self): | |
return getattr(self.parent_obj, self.instancemethod_name) | ||
|
||
|
||
def mappable_kwargs_decorator(func, map_targets): | ||
"""Decompose labeled data for passing to base functions. | ||
|
||
This is a decorator to easy the burden of unpacking | ||
pandas data frames into base objects. | ||
|
||
In the returned function args and kwargs are passed through and | ||
the specified columns are re-mapped to keys in the kwargs. | ||
|
||
The args and kwargs are then unpacked into the function (which will | ||
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. " (which" -> ", which " |
||
expect an `Axes` as the first input and a `DataFrame`-like objects | ||
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. -> "object" (singular) |
||
as the second. | ||
|
||
The function call :: | ||
|
||
func(ax, x=data['foo'], y=data['bar']) | ||
|
||
is the same as :: | ||
|
||
wrapped = mappable_kwargs_decorator(func, {'x': 'foo', 'y':'bar'}) | ||
wrapped(ax, data) | ||
|
||
Parameters | ||
---------- | ||
function : callable | ||
|
||
A function which expects an `Axes` objects as the first argument | ||
and can take data as kwargs. | ||
|
||
map_targets : dict | ||
|
||
Mapping between key expected by the function and the column name. | ||
ex ``{'x': 'foo', 'y': 'bar'}`` -> | ||
``kwargs['x'] == data['foo']`` and ``kwargs['y'] == data['bar']`` | ||
|
||
|
||
Returns | ||
------- | ||
callable | ||
|
||
Decorated function with the signature :: | ||
|
||
func(ax, df, *args, **kwargs) | ||
|
||
|
||
""" | ||
@wraps(func) | ||
def inner(ax, data, *args, **kwargs): | ||
for k, v in map_targets.items(): | ||
if k in kwargs: | ||
raise ValueError(("Trying to map a column ({1} -> {0}) " | ||
"on to a passed in " | ||
"keyword arg ({0})").format(k, v)) | ||
kwargs[k] = data[v].values | ||
return func(ax, *args, **kwargs) | ||
|
||
return inner | ||
|
||
|
||
def mappable_args_decorator(func, map_targets): | ||
"""Decompose labeled data for passing to base functions. | ||
|
||
This is a decorator to easy the burden of unpacking | ||
pandas data frames into base objects. | ||
|
||
In the returned function args and kwargs are passed through and | ||
the specified columns are re-mapped to precede the args. | ||
|
||
The args and kwargs are then unpacked into the function (which will | ||
expect an `Axes` as the first input and a `DataFrame`-like objects | ||
as the second. | ||
|
||
|
||
The function call :: | ||
|
||
func(ax, data['foo'], data['bar']) | ||
|
||
is the same as :: | ||
|
||
wrapped = mappable_args_decorator(func, ('foo', 'bar')) | ||
wrapped(ax, data) | ||
|
||
|
||
Parameters | ||
---------- | ||
function : callable | ||
A function which expects an `Axes` objects as the first argument | ||
and can take data as kwargs. | ||
|
||
map_targets : tuple | ||
Order of columns to extract to pass to the function. These column | ||
are the 2nd -> n+1 args | ||
|
||
|
||
Returns | ||
------- | ||
callable | ||
Decorated function with the signature :: | ||
|
||
func(ax, df, *args, **kwargs) | ||
|
||
|
||
""" | ||
@wraps(func) | ||
def inner(ax, data, *args, **kwargs): | ||
args = tuple(data[k] for k in map_targets) + args | ||
return func(ax, *args, **kwargs) | ||
|
||
return inner | ||
|
||
|
||
def apply_kwargs_mapping(ax, func, data, map_targets, *args, **kwargs): | ||
for k, v in map_targets.items(): | ||
if k in kwargs: | ||
raise ValueError(("Trying to map a column ({1} -> {0}) " | ||
"on to a passed in " | ||
"keyword arg ({0})").format(k, v)) | ||
kwargs[k] = data[v].values | ||
return func(ax, *args, **kwargs) | ||
|
||
|
||
def apply_args_mapping(ax, func, data, map_targets, *args, **kwargs): | ||
args = tuple(data[k] for k in map_targets) + args | ||
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. Why are you using the 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. Oversight, but I am not fully sure which way is better. 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. Not using 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. Omitting 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. I think it is always NaN internally in pandas and they don't do masking/sparse at all |
||
return func(ax, *args, **kwargs) | ||
|
||
|
||
# Numpy > 1.6.x deprecates putmask in favor of the new copyto. | ||
# So long as we support versions 1.6.x and less, we need the | ||
# following local version of putmask. We choose to make a | ||
|
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.
"base objects" -> "numpy arrays"?