-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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 5 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 |
---|---|---|
|
@@ -404,9 +404,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) | ||
|
||
|
@@ -1132,6 +1134,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 |
---|---|---|
|
@@ -64,6 +64,12 @@ def to_timedelta(arg, unit=None, errors="raise"): | |
to_datetime : Convert argument to datetime. | ||
convert_dtypes : Convert dtypes. | ||
|
||
Notes | ||
----- | ||
|
||
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. no extra line here 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. Thx, changed |
||
If the precision is higher than nanoseconds, the precision of the duration is | ||
truncated to nanoseconds. | ||
|
||
Examples | ||
-------- | ||
Parsing a single string to a Timedelta: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,3 +179,10 @@ 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("func", ["Timedelta", "to_timedelta"]) | ||
phofl marked this conversation as resolved.
Show resolved
Hide resolved
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. use pd.Timedelta and pd.to_datetime instead of passing these as strings and using getattr below 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 |
||
def test_to_timedelta_precision_over_nanos(self, func): | ||
# GH: 36738 | ||
expected = pd.Timedelta("8:53:08.718") | ||
result = getattr(pd, func)("8:53:08.71800000001") | ||
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.
"incorrectly truncating to sub-second portion of a string input when it has precision higher than nanoseconds"
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