Skip to content

Commit 860d555

Browse files
mchwaliszjreback
authored andcommitted
DOC: timeseries.rst floating point precision (#15817)
closes #15817 Author: Mikolaj Chwalisz <[email protected]> Closes #15919 from mchwalisz/timeseries-precision and squashes the following commits: 7b82e8b [Mikolaj Chwalisz] DOC: timeseries.rst floating point precision (#15817)
1 parent 0aef74f commit 860d555

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

doc/source/timeseries.rst

+22-6
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,23 @@ 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.
271271

272-
pd.to_datetime([1])
272+
.. warning::
273273

274-
pd.to_datetime([1, 3.14], unit='s')
274+
Conversion of float epoch times can lead to inaccurate and unexpected results.
275+
:ref:`Python floats <python:tut-fp-issues>` have about 15 digits precision in
276+
decimal. Rounding during conversion from float to high precision ``Timestamp`` is
277+
unavoidable. The only way to achieve exact precision is to use a fixed-width
278+
types (e.g. an int64).
275279

276-
.. note::
280+
.. ipython:: python
277281
278-
Epoch times will be rounded to the nearest nanosecond.
282+
1490195805.433502912
283+
pd.to_datetime([1490195805.433, 1490195805.433502912], unit='s')
284+
pd.to_datetime(1490195805433502912, unit='ns')
279285
280286
.. _timeseries.origin:
281287

@@ -300,6 +306,16 @@ Commonly called 'unix epoch' or POSIX time.
300306
301307
pd.to_datetime([1, 2, 3], unit='D')
302308
309+
.. note::
310+
311+
Without specifying origin the following examples still evaluate, but the results
312+
may be unexpected.
313+
314+
.. ipython:: python
315+
316+
pd.to_datetime([1])
317+
pd.to_datetime([1, 3.14], unit='s')
318+
303319
.. _timeseries.daterange:
304320

305321
Generating Ranges of Timestamps

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)