Assigning Accessor to a specific dtype? · Issue #20579 · pandas-dev/pandas (original) (raw)

Problem description

With the v.23 accessors, I think they are really great, but for Series accessors, can you limit the functionality to just a specific dtype or a custom dtype?

Take the example on the page, the geo accessor.

Currently this applies to the whole series (if a series accessor is used), but if the functionality is limited to operate on a specific data type, why should the accessor be accessible to say an int or string type series.

I am trying to move away from extending by inheritance, but I want to limit the scope of the operations.

So going back to the geo sample, say I create a dtype extension called SpatialType or something like that, and then assign it to a column. I would like then register that functionality to that type only.

so:

@pd.api.extensions.register_series_accessor("geo", dtype="SpatialExt")
class GeoAccessor(object):
    def __init__(self, pandas_obj):
        self._obj = pandas_obj

    @property
    def center(self):
        # return the geographic center point of this DataFrame
        lat = self._obj.latitude
        lon = self._obj.longitude
        return (float(lon.mean()), float(lat.mean()))

    def plot(self):
        # plot this array's data on a map, e.g., using Cartopy
        pass
data = [
   [1,2,113,{'x':-75, 'y':35}],
   [2,20,213,{'x':-76, 'y':36}],
   [3,-20,123,{'x':-77, 'y':37}]
]
columns = ['OID', 'Elevation', 'measurement', 'geometry']
df = pd.DataFrame(data, columns)
df.set_geometry('geometry') # define the spatial column using some dataframe accessor

df.geometry.geo.plot() # This would show
df.OID.geo.plot() # this would not work.

This would provide a more controlled extension platform allowing users to limit the extensions to defined types.