Is there a pandas accessor for whatever is the underlying value in the object in each cell?
As per Pandas documentation, you can register custom accessors using special decorators, like this:
import pandas as pd
@pd.api.extensions.register_series_accessor("spec")
class SpecAccessor:
def __init__(self, pandas_obj: pd.Series):
self._obj = pandas_obj
for i in range(len(self._obj)):
for attr in self._obj[i].__class__.__dict__:
# set objects methods on the accessor
if not attr.startswith("__"):
ser = pd.Series(
[getattr(self._obj[i], attr)() for i in range(len(self._obj))]
)
setattr(self, attr, ser)
So that with the following classes and instances:
class Car:
def __init__(self, speed: float):
self._speed = speed
def max_speed(self) -> float:
return self._speed * 1.5
class Plane:
def __init__(self, max_height: float):
self._max_height = max_height
def cruise_height(self) -> float:
return self._max_height * 0.6
car1 = Car(10.0)
car2 = Car(30.5)
car3 = Car(50.9)
plane1 = Plane(5_000.0)
plane2 = Plane(3_000.5)
plane3 = Plane(9_000.9)
You can do:
print(pd.Series([car1, car2, car3]).spec.max_speed)
# Ouputs
0 15.00
1 45.75
2 76.35
dtype: float64
print(pd.Series([plane1, plane2, plane3]).spec.cruise_height)
# Outputs
0 3000.00
1 1800.30
2 5400.54
dtype: float64