Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 57fd502

Browse filesBrowse files
authored
BUG: Fix bug in to_datetime that occasionally throws FloatingPointErr… (#61022)
* BUG: Fix bug in to_datetime that occasionally throws FloatingPointError when called on a float array with missing values * Address review comment
1 parent bc34e24 commit 57fd502
Copy full SHA for 57fd502

File tree

Expand file treeCollapse file tree

3 files changed

+11
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+11
-1
lines changed

‎doc/source/whatsnew/v3.0.0.rst

Copy file name to clipboardExpand all lines: doc/source/whatsnew/v3.0.0.rst
+1Lines 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

Copy file name to clipboardExpand all lines: pandas/_libs/tslibs/conversion.pyx
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def cast_from_unit_vectorized(
137137

138138
out = np.empty(shape, dtype="i8")
139139
base = np.empty(shape, dtype="i8")
140-
frac = np.empty(shape, dtype="f8")
140+
frac = np.zeros(shape, dtype="f8")
141141

142142
for i in range(len(values)):
143143
if is_nan(values[i]):

‎pandas/tests/tools/test_to_datetime.py

Copy file name to clipboardExpand all lines: pandas/tests/tools/test_to_datetime.py
+9Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.