Skip to content

Commit 40e621f

Browse files
Backport PR #57548 on branch 2.2.x (Fix accidental loss-of-precision for to_datetime(str, unit=...)) (#58034)
Backport PR #57548: Fix accidental loss-of-precision for to_datetime(str, unit=...) Co-authored-by: Elliott Sales de Andrade <[email protected]>
1 parent 7e8d492 commit 40e621f

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

doc/source/whatsnew/v2.2.2.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Fixed regressions
1515
~~~~~~~~~~~~~~~~~
1616
- :meth:`DataFrame.__dataframe__` was producing incorrect data buffers when the a column's type was a pandas nullable on with missing values (:issue:`56702`)
1717
- :meth:`DataFrame.__dataframe__` was producing incorrect data buffers when the a column's type was a pyarrow nullable on with missing values (:issue:`57664`)
18-
-
18+
- Fixed regression in precision of :func:`to_datetime` with string and ``unit`` input (:issue:`57051`)
1919

2020
.. ---------------------------------------------------------------------------
2121
.. _whatsnew_222.bug_fixes:

pandas/_libs/tslib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def array_with_unit_to_datetime(
277277
bint is_raise = errors == "raise"
278278
ndarray[int64_t] iresult
279279
tzinfo tz = None
280-
float fval
280+
double fval
281281

282282
assert is_ignore or is_coerce or is_raise
283283

pandas/tests/tools/test_to_datetime.py

+8
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,14 @@ def test_unit(self, cache):
19121912
with pytest.raises(ValueError, match=msg):
19131913
to_datetime([1], unit="D", format="%Y%m%d", cache=cache)
19141914

1915+
def test_unit_str(self, cache):
1916+
# GH 57051
1917+
# Test that strs aren't dropping precision to 32-bit accidentally.
1918+
with tm.assert_produces_warning(FutureWarning):
1919+
res = to_datetime(["1704660000"], unit="s", origin="unix")
1920+
expected = to_datetime([1704660000], unit="s", origin="unix")
1921+
tm.assert_index_equal(res, expected)
1922+
19151923
def test_unit_array_mixed_nans(self, cache):
19161924
values = [11111111111111111, 1, 1.0, iNaT, NaT, np.nan, "NaT", ""]
19171925
result = to_datetime(values, unit="D", errors="ignore", cache=cache)

0 commit comments

Comments
 (0)