Skip to content

BUG: pandas.Interval-inherited class converted automatically to pandas.Interval #46945

Open
@PetitLepton

Description

@PetitLepton

Research

  • I have searched the [pandas] tag on StackOverflow for similar questions.

  • I have asked my usage related question on StackOverflow.

Link to question on StackOverflow

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIntervalInterval data typeSubclassingSubclassing pandas objects

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions