Skip to content

BUG: Fix bug in to_datetime that occasionally throws FloatingPointErr… #61022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ Datetimelike
- Bug in :meth:`DatetimeIndex.union` and :meth:`DatetimeIndex.intersection` when ``unit`` was non-nanosecond (:issue:`59036`)
- Bug in :meth:`Series.dt.microsecond` producing incorrect results for pyarrow backed :class:`Series`. (:issue:`59154`)
- Bug in :meth:`to_datetime` not respecting dayfirst if an uncommon date string was passed. (:issue:`58859`)
- Bug in :meth:`to_datetime` on float array with missing values throwing ``FloatingPointError`` (:issue:`58419`)
- Bug in :meth:`to_datetime` on float32 df with year, month, day etc. columns leads to precision issues and incorrect result. (:issue:`60506`)
- Bug in :meth:`to_datetime` reports incorrect index in case of any failure scenario. (:issue:`58298`)
- Bug in :meth:`to_datetime` wrongly converts when ``arg`` is a ``np.datetime64`` object with unit of ``ps``. (:issue:`60341`)
Expand Down
1 change: 1 addition & 0 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def cast_from_unit_vectorized(
for i in range(len(values)):
if is_nan(values[i]):
base[i] = NPY_NAT
frac[i] = 0
else:
base[i] = <int64_t>values[i]
frac[i] = values[i] - base[i]
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2520,6 +2520,15 @@ def test_to_datetime_overflow(self):
with pytest.raises(OutOfBoundsTimedelta, match=msg):
date_range(start="1/1/1700", freq="B", periods=100000)

def test_to_datetime_float_with_nans_floating_point_error(self):
# GH#58419
ser = Series([np.nan] * 1000 + [1712219033.0], dtype=np.float64)
result = to_datetime(ser, unit="s", errors="coerce")
expected = Series(
[NaT] * 1000 + [Timestamp("2024-04-04 08:23:53")], dtype="datetime64[ns]"
)
tm.assert_series_equal(result, expected)

def test_string_invalid_operation(self, cache):
invalid = np.array(["87156549591102612381000001219H5"], dtype=object)
# GH #51084
Expand Down