|
11 | 11 | from pandas._libs.tslibs import NaT, Timedelta, Timestamp, iNaT
|
12 | 12 | from pandas._libs.tslibs.fields import get_timedelta_field
|
13 | 13 | from pandas._libs.tslibs.timedeltas import (
|
14 |
| - array_to_timedelta64, parse_timedelta_unit) |
| 14 | + array_to_timedelta64, parse_timedelta_unit, precision_from_unit) |
15 | 15 | import pandas.compat as compat
|
16 | 16 | from pandas.util._decorators import Appender
|
17 | 17 |
|
@@ -918,9 +918,16 @@ def sequence_to_td64ns(data, copy=False, unit="ns", errors="raise"):
|
918 | 918 | copy = copy and not copy_made
|
919 | 919 |
|
920 | 920 | elif is_float_dtype(data.dtype):
|
921 |
| - # object_to_td64ns has custom logic for float -> int conversion |
922 |
| - # to avoid precision issues |
923 |
| - data = objects_to_td64ns(data, unit=unit, errors=errors) |
| 921 | + # cast the unit, multiply base/frace separately |
| 922 | + # to avoid precision issues from float -> int |
| 923 | + mask = np.isnan(data) |
| 924 | + m, p = precision_from_unit(unit) |
| 925 | + base = data.astype(np.int64) |
| 926 | + frac = data - base |
| 927 | + if p: |
| 928 | + frac = np.round(frac, p) |
| 929 | + data = (base * m + (frac * m).astype(np.int64)).view('timedelta64[ns]') |
| 930 | + data[mask] = iNaT |
924 | 931 | copy = False
|
925 | 932 |
|
926 | 933 | elif is_timedelta64_dtype(data.dtype):
|
@@ -999,10 +1006,10 @@ def objects_to_td64ns(data, unit="ns", errors="raise"):
|
999 | 1006 | ----------
|
1000 | 1007 | data : ndarray or Index
|
1001 | 1008 | unit : str, default "ns"
|
1002 |
| - The timedelta unit to treat integers as multiples of. |
1003 |
| - errors : {"raise", "coerce", "ignore"}, default "raise" |
1004 |
| - How to handle elements that cannot be converted to timedelta64[ns]. |
1005 |
| - See ``pandas.to_timedelta`` for details. |
| 1009 | + The timedelta unit to treat integers as array_to_timedelta64 |
| 1010 | + errors : {"raise", "coerce", "ignore"}, defaarray_to_timedelta64 |
| 1011 | + How to handle elements that cannot be coarray_to_timedelta64 |
| 1012 | + See ``pandas.to_timedelta`` for details.array_to_timedelta64 |
1006 | 1013 |
|
1007 | 1014 | Returns
|
1008 | 1015 | -------
|
|
0 commit comments