How to use _register_accessor method in pandera

Best Python code snippet using pandera_python

accessor.py

Source:accessor.py Github

copy

Full Screen

...142 # NDFrame143 object.__setattr__(obj, self._name, accessor_obj)144 return accessor_obj145@doc(klass="", others="")146def _register_accessor(name, cls):147 """148 Register a custom accessor on {klass} objects.149 Parameters150 ----------151 name : str152 Name under which the accessor should be registered. A warning is issued153 if this name conflicts with a preexisting attribute.154 Returns155 -------156 callable157 A class decorator.158 See Also159 --------160 register_dataframe_accessor : Register a custom accessor on DataFrame objects.161 register_series_accessor : Register a custom accessor on Series objects.162 register_index_accessor : Register a custom accessor on Index objects.163 Notes164 -----165 When accessed, your accessor will be initialized with the pandas object166 the user is interacting with. So the signature must be167 .. code-block:: python168 def __init__(self, pandas_object): # noqa: E999169 ...170 For consistency with pandas methods, you should raise an ``AttributeError``171 if the data passed to your accessor has an incorrect dtype.172 >>> pd.Series(['a', 'b']).dt173 Traceback (most recent call last):174 ...175 AttributeError: Can only use .dt accessor with datetimelike values176 Examples177 --------178 In your library code::179 import pandas as pd180 @pd.api.extensions.register_dataframe_accessor("geo")181 class GeoAccessor:182 def __init__(self, pandas_obj):183 self._obj = pandas_obj184 @property185 def center(self):186 # return the geographic center point of this DataFrame187 lat = self._obj.latitude188 lon = self._obj.longitude189 return (float(lon.mean()), float(lat.mean()))190 def plot(self):191 # plot this array's data on a map, e.g., using Cartopy192 pass193 Back in an interactive IPython session:194 .. code-block:: ipython195 In [1]: ds = pd.DataFrame({{"longitude": np.linspace(0, 10),196 ...: "latitude": np.linspace(0, 20)}})197 In [2]: ds.geo.center198 Out[2]: (5.0, 10.0)199 In [3]: ds.geo.plot() # plots data on a map200 """201 def decorator(accessor):202 if hasattr(cls, name):203 warnings.warn(204 f"registration of accessor {repr(accessor)} under name "205 f"{repr(name)} for type {repr(cls)} is overriding a preexisting "206 f"attribute with the same name.",207 UserWarning,208 stacklevel=2,209 )210 setattr(cls, name, CachedAccessor(name, accessor))211 cls._accessors.add(name)212 return accessor213 return decorator214@doc(_register_accessor, klass="DataFrame")215def register_dataframe_accessor(name):216 from pandas import DataFrame217 return _register_accessor(name, DataFrame)218@doc(_register_accessor, klass="Series")219def register_series_accessor(name):220 from pandas import Series221 return _register_accessor(name, Series)222@doc(_register_accessor, klass="Index")223def register_index_accessor(name):224 from pandas import Index...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pandera automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful