Skip to content

Commit cd10e92

Browse files
committed
DOC: timeseries.rst floating point precision (pandas-dev#15817)
1 parent 1fbdc23 commit cd10e92

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

doc/source/timeseries.rst

+22-5
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,34 @@ Typical epoch stored units
265265
pd.to_datetime([1349720105100, 1349720105200, 1349720105300,
266266
1349720105400, 1349720105500 ], unit='ms')
267267
268-
These *work*, but the results may be unexpected.
268+
.. note::
269269

270-
.. ipython:: python
270+
Epoch times will be rounded to the nearest nanosecond.
271+
272+
.. warning::
273+
274+
Conversion of float epoch times can lead to inaccurate and unexpected results.
275+
Python floats are unbounded precision, with about 15 significant digits (depends on
276+
operating system). Rounding in conversion from to high precision ``Timestamp`` is
277+
unavoidable. The only way to have exact precision is to use a fixed-width exact type
278+
(e.g. an int64).
271279

272-
pd.to_datetime([1])
280+
.. ipython:: python
273281
274-
pd.to_datetime([1, 3.14], unit='s')
282+
1490195805.433502912
283+
pd.to_datetime([1490195805.433, 1490195805.433502912], unit='s')
284+
1490195805433502912
285+
pd.to_datetime(1490195805433502912, unit='ns')
275286
276287
.. note::
277288

278-
Epoch times will be rounded to the nearest nanosecond.
289+
These *work*, but the results may be unexpected.
290+
291+
.. ipython:: python
292+
293+
pd.to_datetime([1])
294+
pd.to_datetime([1, 3.14], unit='s')
295+
279296
280297
.. _timeseries.origin:
281298

pandas/tseries/tools.py

+10
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,16 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
315315
>>> %timeit pd.to_datetime(s,infer_datetime_format=False)
316316
1 loop, best of 3: 471 ms per loop
317317
318+
Using a unix epoch time
319+
320+
>>> pd.to_datetime(1490195805, unit='s')
321+
Timestamp('2017-03-22 15:16:45')
322+
>>> pd.to_datetime(1490195805433502912, unit='ns')
323+
Timestamp('2017-03-22 15:16:45.433502912')
324+
325+
.. warning:: For float arg, precision rounding might happen. To prevent
326+
unexpected behavior use a fixed-width exact type.
327+
318328
Using a non-unix epoch origin
319329
320330
>>> pd.to_datetime([1, 2, 3], unit='D',

0 commit comments

Comments
 (0)