|
| 1 | +# Licensed to Modin Development Team under one or more contributor license agreements. |
| 2 | +# See the NOTICE file distributed with this work for additional information regarding |
| 3 | +# copyright ownership. The Modin Development Team licenses this file to you under the |
| 4 | +# Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 | +# compliance with the License. You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software distributed under |
| 10 | +# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific language |
| 12 | +# governing permissions and limitations under the License. |
| 13 | + |
| 14 | +from types import ModuleType |
| 15 | +from typing import Any, Union |
| 16 | + |
| 17 | +import modin.pandas as pd |
| 18 | + |
| 19 | + |
| 20 | +def _set_attribute_on_obj( |
| 21 | + name: str, extensions_dict: dict, obj: Union[pd.DataFrame, pd.Series, ModuleType] |
| 22 | +): |
| 23 | + """ |
| 24 | + Create a new or override existing attribute on obj. |
| 25 | +
|
| 26 | + Parameters |
| 27 | + ---------- |
| 28 | + name : str |
| 29 | + The name of the attribute to assign to `obj`. |
| 30 | + extensions_dict : dict |
| 31 | + The dictionary mapping extension name to `new_attr` (assigned below). |
| 32 | + obj : DataFrame, Series, or modin.pandas |
| 33 | + The object we are assigning the new attribute to. |
| 34 | +
|
| 35 | + Returns |
| 36 | + ------- |
| 37 | + decorator |
| 38 | + Returns the decorator function. |
| 39 | + """ |
| 40 | + |
| 41 | + def decorator(new_attr: Any): |
| 42 | + """ |
| 43 | + The decorator for a function or class to be assigned to name |
| 44 | +
|
| 45 | + Parameters |
| 46 | + ---------- |
| 47 | + new_attr : Any |
| 48 | + The new attribute to assign to name. |
| 49 | +
|
| 50 | + Returns |
| 51 | + ------- |
| 52 | + new_attr |
| 53 | + Unmodified new_attr is return from the decorator. |
| 54 | + """ |
| 55 | + extensions_dict[name] = new_attr |
| 56 | + setattr(obj, name, new_attr) |
| 57 | + return new_attr |
| 58 | + |
| 59 | + return decorator |
| 60 | + |
| 61 | + |
| 62 | +def register_dataframe_accessor(name: str): |
| 63 | + """ |
| 64 | + Registers a dataframe attribute with the name provided. |
| 65 | +
|
| 66 | + This is a decorator that assigns a new attribute to DataFrame. It can be used |
| 67 | + with the following syntax: |
| 68 | +
|
| 69 | + ``` |
| 70 | + @register_dataframe_accessor("new_method") |
| 71 | + def my_new_dataframe_method(*args, **kwargs): |
| 72 | + # logic goes here |
| 73 | + return |
| 74 | + ``` |
| 75 | +
|
| 76 | + The new attribute can then be accessed with the name provided: |
| 77 | +
|
| 78 | + ``` |
| 79 | + df.new_method(*my_args, **my_kwargs) |
| 80 | + ``` |
| 81 | +
|
| 82 | + Parameters |
| 83 | + ---------- |
| 84 | + name : str |
| 85 | + The name of the attribute to assign to DataFrame. |
| 86 | +
|
| 87 | + Returns |
| 88 | + ------- |
| 89 | + decorator |
| 90 | + Returns the decorator function. |
| 91 | + """ |
| 92 | + return _set_attribute_on_obj( |
| 93 | + name, pd.dataframe._DATAFRAME_EXTENSIONS_, pd.DataFrame |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +def register_series_accessor(name: str): |
| 98 | + """ |
| 99 | + Registers a series attribute with the name provided. |
| 100 | +
|
| 101 | + This is a decorator that assigns a new attribute to Series. It can be used |
| 102 | + with the following syntax: |
| 103 | +
|
| 104 | + ``` |
| 105 | + @register_series_accessor("new_method") |
| 106 | + def my_new_series_method(*args, **kwargs): |
| 107 | + # logic goes here |
| 108 | + return |
| 109 | + ``` |
| 110 | +
|
| 111 | + The new attribute can then be accessed with the name provided: |
| 112 | +
|
| 113 | + ``` |
| 114 | + s.new_method(*my_args, **my_kwargs) |
| 115 | + ``` |
| 116 | +
|
| 117 | + Parameters |
| 118 | + ---------- |
| 119 | + name : str |
| 120 | + The name of the attribute to assign to Series. |
| 121 | +
|
| 122 | + Returns |
| 123 | + ------- |
| 124 | + decorator |
| 125 | + Returns the decorator function. |
| 126 | + """ |
| 127 | + return _set_attribute_on_obj(name, pd.series._SERIES_EXTENSIONS_, pd.Series) |
| 128 | + |
| 129 | + |
| 130 | +def register_pd_accessor(name: str): |
| 131 | + """ |
| 132 | + Registers a pd namespace attribute with the name provided. |
| 133 | +
|
| 134 | + This is a decorator that assigns a new attribute to modin.pandas. It can be used |
| 135 | + with the following syntax: |
| 136 | +
|
| 137 | + ``` |
| 138 | + @register_pd_accessor("new_function") |
| 139 | + def my_new_pd_function(*args, **kwargs): |
| 140 | + # logic goes here |
| 141 | + return |
| 142 | + ``` |
| 143 | +
|
| 144 | + The new attribute can then be accessed with the name provided: |
| 145 | +
|
| 146 | + ``` |
| 147 | + import modin.pandas as pd |
| 148 | +
|
| 149 | + pd.new_method(*my_args, **my_kwargs) |
| 150 | + ``` |
| 151 | +
|
| 152 | +
|
| 153 | + Parameters |
| 154 | + ---------- |
| 155 | + name : str |
| 156 | + The name of the attribute to assign to modin.pandas. |
| 157 | +
|
| 158 | + Returns |
| 159 | + ------- |
| 160 | + decorator |
| 161 | + Returns the decorator function. |
| 162 | + """ |
| 163 | + return _set_attribute_on_obj(name, pd._PD_EXTENSIONS_, pd) |
0 commit comments