Skip to content

Commit 767cc43

Browse files
committed
BUG: Fix bug in to_datetime that occasionally throws FloatingPointError when called on a float array with missing values
1 parent c28c589 commit 767cc43

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ Datetimelike
649649
- Bug in :meth:`DatetimeIndex.union` and :meth:`DatetimeIndex.intersection` when ``unit`` was non-nanosecond (:issue:`59036`)
650650
- Bug in :meth:`Series.dt.microsecond` producing incorrect results for pyarrow backed :class:`Series`. (:issue:`59154`)
651651
- Bug in :meth:`to_datetime` not respecting dayfirst if an uncommon date string was passed. (:issue:`58859`)
652+
- Bug in :meth:`to_datetime` on float array with missing values throwing ``FloatingPointError`` (:issue:`58419`)
652653
- Bug in :meth:`to_datetime` on float32 df with year, month, day etc. columns leads to precision issues and incorrect result. (:issue:`60506`)
653654
- Bug in :meth:`to_datetime` reports incorrect index in case of any failure scenario. (:issue:`58298`)
654655
- Bug in :meth:`to_datetime` wrongly converts when ``arg`` is a ``np.datetime64`` object with unit of ``ps``. (:issue:`60341`)

pandas/_libs/tslibs/conversion.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ def cast_from_unit_vectorized(
142142
for i in range(len(values)):
143143
if is_nan(values[i]):
144144
base[i] = NPY_NAT
145+
frac[i] = 0
145146
else:
146147
base[i] = <int64_t>values[i]
147148
frac[i] = values[i] - base[i]

pandas/tests/tools/test_to_datetime.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,6 +2520,15 @@ def test_to_datetime_overflow(self):
25202520
with pytest.raises(OutOfBoundsTimedelta, match=msg):
25212521
date_range(start="1/1/1700", freq="B", periods=100000)
25222522

2523+
def test_to_datetime_float_with_nans_floating_point_error(self):
2524+
# GH#58419
2525+
ser = Series([np.nan] * 1000 + [1712219033.0], dtype=np.float64)
2526+
result = to_datetime(ser, unit="s", errors="coerce")
2527+
expected = Series(
2528+
[NaT] * 1000 + [Timestamp("2024-04-04 08:23:53")], dtype="datetime64[ns]"
2529+
)
2530+
tm.assert_series_equal(result, expected)
2531+
25232532
def test_string_invalid_operation(self, cache):
25242533
invalid = np.array(["87156549591102612381000001219H5"], dtype=object)
25252534
# GH #51084

0 commit comments

Comments
 (0)