Pandas version checks
-
[x] I have checked that this issue has not already been reported.
-
[x] I have confirmed this bug exists on the latest version of pandas.
-
[x] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
from typing import reveal_type
import pandas as pd
from datetime import timedelta
reveal_type(pd.Index([1]) * timedelta(days=1))
reveal_type(pd.Index([1]) * [timedelta(days=1)])
reveal_type(pd.Index([1]) * pd.Timedelta(1, "d"))
reveal_type(pd.Index([1]) * [pd.Timedelta(1, "d")])
Issue Description
Result is inconsistent
Runtime type is 'TimedeltaIndex'
Runtime type is 'Index'
Runtime type is 'TimedeltaIndex'
Runtime type is 'Index'
Expected Behavior
Results should all be `TimedeltaIndex
Installed Versions
Comment From: rhshadrach
Thanks for the report. I think your expectation is somewhat reasonable, but this would lead to value-dependent behavior.
import random
if random.choice(["heads", "tails"]) == "heads":
value = 1
else:
value = timedelta(days=1)
reveal_type(pd.Index([1, 2]) * [timedelta(days=1), value])
Because lists can hold arbitrary types, if we were to return TimedeltaIndex
as you expected in the OP, the multiplication would either have to raise when it has a valid answer, or have a different dtype depending on the values in the list.
I think Index
of object dtype is the proper result here.
cc @jbrockmendel
Comment From: cmp0xff
Thank you for the reply. From my point of view, if the resulting dtype
of an Index
is not object
but instead Timedelta
or Timestamp
, I would imagine that it got "promoted" to TimedeltaIndex
or TimestampIndex
.
Comment From: jbrockmendel
Neither is crazy.
What to do when we see a list is a Hard Problem. Do we cast it to ndarray(like)? Or do we treat it as a scalar that the user wants to operate on pointwise? xref #62423, #33807, #27911. ATM we do wrap lists for comparison ops but not for arithmetic/boolean ops.
I suspect that ultimately the correct thing will be to always wrap, but only after we have something like pd.Scalar that a user can use to specify "treat this sequence like a scalar".
Comment From: rhshadrach
@jbrockmendel - do you see value in keeping this issue open?
Comment From: jbrockmendel
i think the failure mode ("unexpected type/dtype") is distinct from the others ("it raises and shouldn't"), so yes
Comment From: Ganasekhar-gif
Hi! I’m interested in working on this issue. My approach will be to investigate the type promotion behavior in Index
multiplication with Timedelta
objects and implement a fix to ensure consistent TimedeltaIndex
results where applicable. I will also add tests to cover different cases.