Custom property-like object (descriptor) for caching accessors. Parameters ---------- name : str The namespace this will be accessed under, e.g. ``df.foo`` accessor : cls The class with the extension methods. The class' __init__ method should expect one
| 55 | # Ported from pandas |
| 56 | # https://github.com/pandas-dev/pandas/blob/master/pandas/core/accessor.py |
| 57 | class CachedAccessor: |
| 58 | """ |
| 59 | Custom property-like object (descriptor) for caching accessors. |
| 60 | |
| 61 | Parameters |
| 62 | ---------- |
| 63 | name : str |
| 64 | The namespace this will be accessed under, e.g. ``df.foo`` |
| 65 | accessor : cls |
| 66 | The class with the extension methods. The class' __init__ method |
| 67 | should expect one of a ``Series``, ``DataFrame`` or ``Index`` as |
| 68 | the single argument ``data`` |
| 69 | """ |
| 70 | |
| 71 | def __init__(self, name, accessor): |
| 72 | self._name = name |
| 73 | self._accessor = accessor |
| 74 | |
| 75 | def __get__(self, obj, cls): |
| 76 | if obj is None: |
| 77 | # we're accessing the attribute of the class, i.e., Dataset.geo |
| 78 | return self._accessor |
| 79 | accessor_obj = self._accessor(obj) |
| 80 | # Replace the property with the accessor object. Inspired by: |
| 81 | # http://www.pydanny.com/cached-property.html |
| 82 | # We need to use object.__setattr__ because we overwrite __setattr__ on |
| 83 | # NDFrame |
| 84 | object.__setattr__(obj, self._name, accessor_obj) |
| 85 | return accessor_obj |
| 86 | |
| 87 | |
| 88 | def _register_accessor(name, cls): |