BUG: Fix bug in to_datetime that occasionally throws FloatingPointErr… · pandas-dev/pandas@57fd502 (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -649,6 +649,7 @@ Datetimelike
649 649 - Bug in :meth:`DatetimeIndex.union` and :meth:`DatetimeIndex.intersection` when ``unit`` was non-nanosecond (:issue:`59036`)
650 650 - Bug in :meth:`Series.dt.microsecond` producing incorrect results for pyarrow backed :class:`Series`. (:issue:`59154`)
651 651 - 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`)
652 653 - Bug in :meth:`to_datetime` on float32 df with year, month, day etc. columns leads to precision issues and incorrect result. (:issue:`60506`)
653 654 - Bug in :meth:`to_datetime` reports incorrect index in case of any failure scenario. (:issue:`58298`)
654 655 - Bug in :meth:`to_datetime` wrongly converts when ``arg`` is a ``np.datetime64`` object with unit of ``ps``. (:issue:`60341`)
Original file line number Diff line number Diff line change
@@ -137,7 +137,7 @@ def cast_from_unit_vectorized(
137 137
138 138 out = np.empty(shape, dtype="i8")
139 139 base = np.empty(shape, dtype="i8")
140 - frac = np.empty(shape, dtype="f8")
140 + frac = np.zeros(shape, dtype="f8")
141 141
142 142 for i in range(len(values)):
143 143 if is_nan(values[i]):
Original file line number Diff line number Diff line change
@@ -2520,6 +2520,15 @@ def test_to_datetime_overflow(self):
2520 2520 with pytest.raises(OutOfBoundsTimedelta, match=msg):
2521 2521 date_range(start="1/1/1700", freq="B", periods=100000)
2522 2522
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 +
2523 2532 def test_string_invalid_operation(self, cache):
2524 2533 invalid = np.array(["87156549591102612381000001219H5"], dtype=object)
2525 2534 # GH #51084