You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
my opinion depends on how difficult this is to change/"fix". Im guessing somewhere in the indexing code when we do setitem-with-expansion we do something like
Assuming this guess is correct, we could fix the OP example by passing dtype=object when constructing new_ser, but that would mean that result would always end up with object dtype, which we definitely dont want. So maybe just do that when old_ser.dtype == object?
In general if users want tight control over dtypes they shouldn't be using setitem-with-expansion.
Note that we also do inference on the index in setitem-with-expansion, which i think we shouldn't, xref #55257, #51363
So I am curious about how to deal with this problem. Apparently, for numpy.timedelta64 and datetime.timedelta, these two types will be convert to pd.Timedelta according to these unittests
@pytest.mark.parametrize(
"td",
[
Timedelta("9 days"),
Timedelta("9 days").to_timedelta64(),
Timedelta("9 days").to_pytimedelta(),
],
)
def test_append_timedelta_does_not_cast(self, td, using_infer_string, request):
# GH#22717 inserting a Timedelta should _not_ cast to int64
if using_infer_string and not isinstance(td, Timedelta):
# TODO: GH#56010
request.applymarker(pytest.mark.xfail(reason="inferred as string"))
expected = Series(["x", td], index=[0, "td"], dtype=object)
ser = Series(["x"])
ser["td"] = td
tm.assert_series_equal(ser, expected)
assert isinstance(ser["td"], Timedelta)
ser = Series(["x"])
ser.loc["td"] = Timedelta("9 days")
tm.assert_series_equal(ser, expected)
assert isinstance(ser["td"], Timedelta)
We always infer time deltas as pd.Timedelta, see #55538 (comment) for more context
The text was updated successfully, but these errors were encountered: