Skip to content

Commit

Permalink
Backport PR pandas-dev#57548: Fix accidental loss-of-precision for to…
Browse files Browse the repository at this point in the history
…_datetime(str, unit=...)
  • Loading branch information
QuLogic authored and meeseeksmachine committed Mar 27, 2024
1 parent 7e8d492 commit 9d350db
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.2.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Fixed regressions
~~~~~~~~~~~~~~~~~
- :meth:`DataFrame.__dataframe__` was producing incorrect data buffers when the a column's type was a pandas nullable on with missing values (:issue:`56702`)
- :meth:`DataFrame.__dataframe__` was producing incorrect data buffers when the a column's type was a pyarrow nullable on with missing values (:issue:`57664`)
-
- Fixed regression in precision of :func:`to_datetime` with string and ``unit`` input (:issue:`57051`)

.. ---------------------------------------------------------------------------
.. _whatsnew_222.bug_fixes:
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def array_with_unit_to_datetime(
bint is_raise = errors == "raise"
ndarray[int64_t] iresult
tzinfo tz = None
float fval
double fval

assert is_ignore or is_coerce or is_raise

Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1912,6 +1912,14 @@ def test_unit(self, cache):
with pytest.raises(ValueError, match=msg):
to_datetime([1], unit="D", format="%Y%m%d", cache=cache)

def test_unit_str(self, cache):
# GH 57051
# Test that strs aren't dropping precision to 32-bit accidentally.
with tm.assert_produces_warning(FutureWarning):
res = to_datetime(["1704660000"], unit="s", origin="unix")
expected = to_datetime([1704660000], unit="s", origin="unix")
tm.assert_index_equal(res, expected)

def test_unit_array_mixed_nans(self, cache):
values = [11111111111111111, 1, 1.0, iNaT, NaT, np.nan, "NaT", ""]
result = to_datetime(values, unit="D", errors="ignore", cache=cache)
Expand Down

0 comments on commit 9d350db

Please sign in to comment.