[docs]classCachedAccessor:""" Custom property-like object. A descriptor for caching accessors. Parameters ---------- name : str Namespace that will be accessed under, e.g. ``df.foo``. accessor : cls Class with the extension methods. Notes ----- For accessor, The class's __init__ method assumes that one of ``Series``, ``DataFrame`` or ``Index`` as the single argument ``data``. """def__init__(self,name:str,accessor)->None:self._name=nameself._accessor=accessordef__get__(self,obj,cls):ifobjisNone:# we're accessing the attribute of the class, i.e., Dataset.georeturnself._accessoraccessor_obj=self._accessor(obj)# Replace the property with the accessor object. Inspired by:# https://www.pydanny.com/cached-property.html# We need to use object.__setattr__ because we overwrite __setattr__ on# NDFrameobject.__setattr__(obj,self._name,accessor_obj)returnaccessor_obj
[docs]@date_operatorsclassDatetimeAccessor(Properties):def__init__(self,series):data=series.valuesifnotisinstance(data,Datetime):raiseAttributeError("Can only use .dt accessor with datetimelike values")self.series=series
[docs]@string_operatorsclassStringAccessor(Properties):def__init__(self,series):data=series.valuesifnot(isinstance(data,Categorical)orisinstance(data,Strings)):raiseAttributeError("Can only use .str accessor with string like values")self.series=series