databricks.koalas.extensions.
register_series_accessor
Register a custom accessor with a Series object
name used when calling the accessor after its registered
A class decorator.
See also
register_dataframe_accessor
Register a custom accessor on DataFrame objects
register_index_accessor
Register a custom accessor on Index objects
Notes
When accessed, your accessor will be initialiazed with the Koalas object the user is interacting with. The code signature must be:
def __init__(self, koalas_obj): # constructor logic ...
In the pandas API, if data passed to your accessor has an incorrect dtype, it’s recommended to raise an AttributeError for consistency purposes. In Koalas, ValueError is more frequently used to annotate when a value’s datatype is unexpected for a given method/function.
AttributeError
ValueError
Ultimately, you can structure this however you like, but Koalas would likely do something like this:
>>> ks.Series(['a', 'b']).dt ... Traceback (most recent call last): ... ValueError: Cannot call DatetimeMethods on type StringType
Examples
In your library code:
from databricks.koalas.extensions import register_series_accessor @register_series_accessor("geo") class GeoAccessor: def __init__(self, koalas_obj): self._obj = koalas_obj @property def is_valid(self): # boolean check to see if series contains valid geometry return True
Then, in an ipython session:
>>> ## Import if the accessor is in the other file. >>> # from my_ext_lib import GeoAccessor >>> kdf = ks.DataFrame({"longitude": np.linspace(0,10), ... "latitude": np.linspace(0, 20)}) >>> kdf.longitude.geo.is_valid True