Open
Description
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
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)