BUG: pandas.Interval-inherited class converted automatically to pandas.Interval (original) (raw)

Research

https://stackoverflow.com/questions/72044363/automatic-conversion-of-a-column-to-the-parent-class-in-pandas

Question about pandas

import pandas

class TimestampsInterval(pandas.Interval): def init(self, left: str, right: str, closed: str = "both") -> None: super().init(pandas.Timestamp(left), pandas.Timestamp(right), closed)

@property
def seconds(self) -> float:
    return self.length.seconds

df = pandas.DataFrame( data={ "intervals": [ TimestampsInterval("1970-01-01 00:00:00", "1970-01-01 00:00:01"), ], } )

I was expecting the DataFrame to keep the class intact but it converts the column

to the parent class pandas.Interval

try: df["intervals"].map(lambda x: x.seconds) except AttributeError as e: print(e) # 'pandas._libs.interval.Interval' object has no attribute 'seconds' print(df["intervals"].dtype) # interval[datetime64[ns], both]

If I "force" pandas not to convert it by embedding the interval into a list, then

the class is indeed kept

df = pandas.DataFrame( data={ "intervals": [ [TimestampsInterval("1970-01-01 00:00:00", "1970-01-01 00:00:01")], ], } )

df["intervals"].map(lambda x: x[0].seconds)