improve accuracy of to_pytimedelta by rohanjain101 · Pull Request #57841 · pandas-dev/pandas (original) (raw)
Thanks for the contribution @rohanjain101
I think both the title of the PR and the description are misleading. You are not really improving the accuracy of to_pytimedelta
, and an increase in a nanosecond doesn't cause an increase in a microsecond when calling to_pytimedelta
. You simply found the point where the rounding change to the next nanosecond.
I do agree that the way the function works is misleading. The problem is that most people would expect this code:
datetime.timedelta(microseconds=12.5) datetime.timedelta(microseconds=12)
to round up to 13
instead to 12
. For good or for bad, this is how Python rounding works:
The round
function or the same algorithm is probably used inside of datetime.timedelta
, so we get the behavior your experienced when using pandas to_pytimedelta
.
The reasoning for how round
works is complex, but it's the default rounding strategy for a reason. I'm not sure if in this particular case the rounding strategy you propose could be better. If you think there is a reason to change it, please let us know the reason in the PR description, or maybe better, open an issue to discuss.
I assume from the information provided that you just found Python's rounding strategy misleading, so I'll close this issue, but if there is a better reason than that for this PR, please feel free to add a comment and reopen.