-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: Timedelta drops decimals if precision is greater than nanoseconds #36771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
e1e4296
78a9938
52463cf
4d6c9dc
f4ff3e0
f79106d
e3c133d
193921e
83111d9
d038085
4ce27c8
8b00890
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -405,9 +405,11 @@ cdef inline int64_t parse_timedelta_string(str ts) except? -1: | |
m = 10**(3 -len(frac)) * 1000 * 1000 | ||
elif len(frac) > 3 and len(frac) <= 6: | ||
m = 10**(6 -len(frac)) * 1000 | ||
else: | ||
elif len(frac) > 6 and len(frac) <= 9: | ||
m = 10**(9 -len(frac)) | ||
|
||
else: | ||
m = 1 | ||
frac = frac[:9] | ||
r = <int64_t>int(''.join(frac)) * m | ||
result += timedelta_as_neg(r, neg) | ||
|
||
|
@@ -1143,6 +1145,9 @@ class Timedelta(_Timedelta): | |
Notes | ||
----- | ||
The ``.value`` attribute is always in ns. | ||
|
||
If the precision is higher than nanoseconds, the precision of the duration is | ||
truncated to nanoseconds. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clarify this is for a string input do additional digits round or just drop (and if just drop, are we sure thats the desired behavior?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dropd additional digits. Was not sure either so I started with the easier implementation |
||
""" | ||
|
||
def __new__(cls, object value=_no_input, unit=None, **kwargs): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -210,3 +210,21 @@ def test_to_timedelta_nullable_int64_dtype(self): | |
result = to_timedelta(Series([1, None], dtype="Int64"), unit="days") | ||
|
||
tm.assert_series_equal(result, expected) | ||
|
||
@pytest.mark.parametrize( | ||
("input", "expected"), | ||
[ | ||
("8:53:08.71800000001", "8:53:08.718"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a case with the trailing digit being 9 to clarify the truncating/rounding distinction There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
("8:53:08.718001", "8:53:08.718001"), | ||
("8:53:08.7180000001", "8:53:08.7180000001"), | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
("-8:53:08.71800000001", "-8:53:08.718"), | ||
("8:53:08.7180000089", "8:53:08.718000008"), | ||
], | ||
) | ||
@pytest.mark.parametrize("func", [pd.Timedelta, pd.to_timedelta]) | ||
def test_to_timedelta_precision_over_nanos(self, input, expected, func): | ||
# GH: 36738 | ||
expected = pd.Timedelta(expected) | ||
result = func(input) | ||
print(result) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the print There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry forgot that, is removed |
||
assert result == expected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you move to section below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done