Skip to content

Commit 02d1855

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent dfd0052 commit 02d1855

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ If you have a feature request, please open an issue or submit a PR!
147147

148148
## TL;DR
149149

150-
Pandas 0.23 introduced a simpler API for [extending Pandas](https://pandas.pydata.org/pandas-docs/stable/development/extending.html#extending-pandas). This API provided two key decorators, `register_dataframe_accessor` and `register_series_accessor`, that enable users to register **accessors** with Pandas DataFrames and Series.
150+
Pandas 0.23 introduced a simpler API for [extending Pandas](https://pandas.pydata.org/pandas-docs/stable/development/extending.html#extending-pandas). This API provided two key decorators, `register_dataframe_accessor` and `register_series_accessor`, that enable users to register **accessors** with Pandas DataFrames and Series.
151151

152-
Pandas Flavor originated as a library to backport these decorators to older versions of Pandas (<0.23). While doing the backporting, it became clear that registering **methods** directly to Pandas objects might be a desired feature as well.[*](#footnote)
152+
Pandas Flavor originated as a library to backport these decorators to older versions of Pandas (<0.23). While doing the backporting, it became clear that registering **methods** directly to Pandas objects might be a desired feature as well.[*](#footnote)
153153

154-
<a name="footnote">*</a>*It is likely that Pandas deliberately chose not implement to this feature. If everyone starts monkeypatching DataFrames with their custom methods, it could lead to confusion in the Pandas community. The preferred Pandas approach is to namespace your methods by registering an accessor that contains your custom methods.*
154+
<a name="footnote">*</a>*It is likely that Pandas deliberately chose not implement to this feature. If everyone starts monkeypatching DataFrames with their custom methods, it could lead to confusion in the Pandas community. The preferred Pandas approach is to namespace your methods by registering an accessor that contains your custom methods.*
155155

156156
**So how does method registration work?**
157157

pandas_flavor/register.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
from functools import wraps
2-
from pandas.api.extensions import register_series_accessor, register_dataframe_accessor
2+
from pandas.api.extensions import (
3+
register_series_accessor,
4+
register_dataframe_accessor,
5+
)
36
import inspect
47
from contextlib import nullcontext
58

69
method_call_ctx_factory = None
710

11+
812
def register_dataframe_method(method):
913
"""Register a function as a method attached to the Pandas DataFrame.
1014
@@ -29,12 +33,22 @@ def __init__(self, pandas_obj):
2933
@wraps(method)
3034
def __call__(self, *args, **kwargs):
3135
global method_call_ctx_factory
32-
method_call_ctx = method_call_ctx_factory(method.__name__, args, kwargs) if method_call_ctx_factory else nullcontext()
36+
method_call_ctx = (
37+
method_call_ctx_factory(method.__name__, args, kwargs)
38+
if method_call_ctx_factory
39+
else nullcontext()
40+
)
3341
with method_call_ctx:
3442
if not isinstance(method_call_ctx, nullcontext):
3543
all_args = tuple([self._obj] + list(args))
36-
new_args, new_kwargs = method_call_ctx.handle_start_method_call(method.__name__, method_signature, all_args, kwargs)
37-
args = new_args[1:]; kwargs = new_kwargs
44+
(
45+
new_args,
46+
new_kwargs,
47+
) = method_call_ctx.handle_start_method_call(
48+
method.__name__, method_signature, all_args, kwargs
49+
)
50+
args = new_args[1:]
51+
kwargs = new_kwargs
3852

3953
ret = method(self._obj, *args, **kwargs)
4054

@@ -54,7 +68,7 @@ def register_series_method(method):
5468
"""Register a function as a method attached to the Pandas Series."""
5569

5670
method_signature = inspect.signature(method)
57-
71+
5872
def inner(*args, **kwargs):
5973
class AccessorMethod(object):
6074
__doc__ = method.__doc__
@@ -65,12 +79,22 @@ def __init__(self, pandas_obj):
6579
@wraps(method)
6680
def __call__(self, *args, **kwargs):
6781
global method_call_ctx_factory
68-
method_call_ctx = method_call_ctx_factory(method.__name__, args, kwargs) if method_call_ctx_factory else nullcontext()
82+
method_call_ctx = (
83+
method_call_ctx_factory(method.__name__, args, kwargs)
84+
if method_call_ctx_factory
85+
else nullcontext()
86+
)
6987
with method_call_ctx:
7088
if not isinstance(method_call_ctx, nullcontext):
7189
all_args = tuple([self._obj] + list(args))
72-
new_args, new_kwargs = method_call_ctx.handle_start_method_call(method.__name__, method_signature, all_args, kwargs)
73-
args = new_args[1:]; kwargs = new_kwargs
90+
(
91+
new_args,
92+
new_kwargs,
93+
) = method_call_ctx.handle_start_method_call(
94+
method.__name__, method_signature, all_args, kwargs
95+
)
96+
args = new_args[1:]
97+
kwargs = new_kwargs
7498

7599
ret = method(self._obj, *args, **kwargs)
76100

0 commit comments

Comments
 (0)