|
| 1 | +"""Custom accessor functionality for modin. |
| 2 | +
|
| 3 | +Source code adapted from koalas implementation: |
| 4 | +https://koalas.readthedocs.io/en/latest/_modules/databricks/koalas/extensions.html#register_dataframe_accessor |
| 5 | +""" |
| 6 | + |
| 7 | +import warnings |
| 8 | + |
| 9 | +from pandera.pandas_accessor import ( |
| 10 | + PanderaDataFrameAccessor, |
| 11 | + PanderaSeriesAccessor, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +# pylint: disable=too-few-public-methods |
| 16 | +class CachedAccessor: |
| 17 | + """ |
| 18 | + Custom property-like object. |
| 19 | +
|
| 20 | + A descriptor for caching accessors: |
| 21 | +
|
| 22 | + :param name: Namespace that accessor's methods, properties, etc will be |
| 23 | + accessed under, e.g. "foo" for a dataframe accessor yields the accessor |
| 24 | + ``df.foo`` |
| 25 | + :param cls: Class with the extension methods. |
| 26 | +
|
| 27 | + For accessor, the class's __init__ method assumes that you are registering |
| 28 | + an accessor for one of ``Series``, ``DataFrame``, or ``Index``. |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__(self, name, accessor): |
| 32 | + self._name = name |
| 33 | + self._accessor = accessor |
| 34 | + |
| 35 | + def __get__(self, obj, cls): |
| 36 | + if obj is None: # pragma: no cover |
| 37 | + return self._accessor |
| 38 | + accessor_obj = self._accessor(obj) |
| 39 | + object.__setattr__(obj, self._name, accessor_obj) |
| 40 | + return accessor_obj |
| 41 | + |
| 42 | + |
| 43 | +def _register_accessor(name, cls): |
| 44 | + """ |
| 45 | + Register a custom accessor on {class} objects. |
| 46 | +
|
| 47 | + :param name: Name under which the accessor should be registered. A warning |
| 48 | + is issued if this name conflicts with a preexisting attribute. |
| 49 | + :returns: A class decorator callable. |
| 50 | + """ |
| 51 | + |
| 52 | + def decorator(accessor): |
| 53 | + if hasattr(cls, name): |
| 54 | + msg = ( |
| 55 | + f"registration of accessor {accessor} under name '{name}' for " |
| 56 | + "type {cls.__name__} is overriding a preexisting attribute " |
| 57 | + "with the same name." |
| 58 | + ) |
| 59 | + |
| 60 | + warnings.warn( |
| 61 | + msg, |
| 62 | + UserWarning, |
| 63 | + stacklevel=2, |
| 64 | + ) |
| 65 | + setattr(cls, name, CachedAccessor(name, accessor)) |
| 66 | + return accessor |
| 67 | + |
| 68 | + return decorator |
| 69 | + |
| 70 | + |
| 71 | +def register_dataframe_accessor(name): |
| 72 | + """ |
| 73 | + Register a custom accessor with a DataFrame |
| 74 | +
|
| 75 | + :param name: name used when calling the accessor after its registered |
| 76 | + :returns: a class decorator callable. |
| 77 | + """ |
| 78 | + # pylint: disable=import-outside-toplevel |
| 79 | + from modin.pandas import DataFrame |
| 80 | + |
| 81 | + return _register_accessor(name, DataFrame) |
| 82 | + |
| 83 | + |
| 84 | +def register_series_accessor(name): |
| 85 | + """ |
| 86 | + Register a custom accessor with a Series object |
| 87 | +
|
| 88 | + :param name: name used when calling the accessor after its registered |
| 89 | + :returns: a callable class decorator |
| 90 | + """ |
| 91 | + # pylint: disable=import-outside-toplevel |
| 92 | + from modin.pandas import Series |
| 93 | + |
| 94 | + return _register_accessor(name, Series) |
| 95 | + |
| 96 | + |
| 97 | +register_dataframe_accessor("pandera")(PanderaDataFrameAccessor) |
| 98 | +register_series_accessor("pandera")(PanderaSeriesAccessor) |
0 commit comments