Skip to content

Enh mappable remapper #4490

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
wants to merge 7 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ENH: add apply version of the decorators
  • Loading branch information
tacaswell committed Jun 2, 2015
commit 270144b01a9da4e691390ef2009df84a6e9ee23d
15 changes: 15 additions & 0 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -2515,6 +2515,21 @@ def inner(ax, data, *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
Copy link
Member

Choose a reason for hiding this comment

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

Why are you using the values attribute for the kwargs cases but not for the args cases?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oversight, but I am not fully sure which way is better. .values gets us a numpy array full-stop, but just [] gets use a Series which more-or-less behave like a numpy array.

Copy link
Member Author

Choose a reason for hiding this comment

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

Not using .values also means that these decorators will work with dicts of numpy arrays. It might be better to use np.asarray.

Copy link
Member

Choose a reason for hiding this comment

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

Omitting .values also means it will work with a numpy structured array; but I'm not sure if there would be any point in this.
Maybe use np.asanyarray in case something might yield a masked array?
I presume with a DataFrame, missing data will end up as NaN after conversion, correct?

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Expand Down