Skip to content

Commit 87bd5ab

Browse files
siddheshproost
authored andcommitted
BUG: Avoid undefined behaviour when converting from float to timedelta (pandas-dev#28918)
Summation of timedelta series with NaTs in them result in undefined behaviour because the final wrapping step of the summation ends up converting the NaNs in the sum through a direct cast to int64. This cast is undefined for NaN and just happens to work on x86_64 because of the way cvttd2si works. On Aarch64, the corresponding fcvtzs sets the result to 0 on undefined input. This fix trivially sets the conversion target to m8 instead of i8 so that numpy correctly casts from NaN to NaT. Note that the fix in numpy for the same is pending in PR #numpy/numpy/14669 . There is an existing test (test_sum_nanops_timedelta in frame/test_analytics.py) that exercises this bug and has been verified to have been fixed with this and the numpy patch.
1 parent 81ff51a commit 87bd5ab

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

pandas/core/nanops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def _wrap_results(result, dtype, fill_value=None):
360360

361361
result = tslibs.Timedelta(result, unit="ns")
362362
else:
363-
result = result.astype("i8").view(dtype)
363+
result = result.astype("m8[ns]").view(dtype)
364364

365365
return result
366366

0 commit comments

Comments
 (0)