Skip to content

Commit 23f2f44

Browse files
jorisvandenbosscheAn1k4etAniket0123
authored
[backport 3.0.x] BUG: Suppress unnecessary RuntimeWarning in to_datetime with missing componentsFix runtime warning (#64292) (#64921)
Co-authored-by: Aniket Vijay Vishwakarma <[email protected]> Co-authored-by: An1k4et <[email protected]>
1 parent 83ba804 commit 23f2f44

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

doc/source/whatsnew/v3.0.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Bug fixes
3333
- :func:`col` and expressions derived from it failed with power (``**``) and matrix multiplication (``@``) operators (:issue:`64267`)
3434
- Assigning :attr:`pd.NA` to a string column could trigger a PyArrow error and corrupt data (:issue:`64320`)
3535
- Bug when using :func:`col` with Python functions :func:`bool`, :func:`iter`, :func:`copy`, and :func:`deepcopy` either failed or produced incorrect results; these now all raise a ``TypeError`` (:issue:`64267`)
36+
- Fixed bug in :func:`to_datetime` that could give an unnecessary ``RuntimeWarning`` when converting DataFrame containing missing values (:issue:`64141`)
3637
- Fixed bug in :meth:`Series.var` computing the variance of complex numbers incorrectly (:issue:`62421`)
3738
- Fixed bug in the :meth:`~Series.sum` method with python-backed string dtype returning incorrect value for an empty Series and ignoring the ``min_count`` argument (:issue:`64683`)
3839
- Fixed bug where :meth:`DataFrame.div` ignored the ``axis`` argument when used with ``level`` for MultiIndex columns (:issue:`64428`)

pandas/core/arrays/timedeltas.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,8 @@ def sequence_to_td64ns(
11601160
if unit is not None and unit != "ns":
11611161
# if all non-NaN entries are round, treat these like ints and give
11621162
# back the requested unit (or closest-supported)
1163-
int_data = data.astype(np.int64)
1163+
with np.errstate(invalid="ignore"):
1164+
int_data = data.astype(np.int64)
11641165
all_round = (mask | (data == int_data)).all()
11651166
if all_round:
11661167
result, _ = sequence_to_td64ns(

pandas/tests/tools/test_to_datetime.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3827,3 +3827,19 @@ def test_to_datetime_lxml_elementunicoderesult_with_format(cache):
38273827

38283828
out = to_datetime(Series([val]), format="%Y-%m-%d %H:%M:%S", cache=cache)
38293829
assert out.iloc[0] == Timestamp(s)
3830+
3831+
3832+
def test_to_datetime_missing_component_no_runtime_warning():
3833+
df = DataFrame(
3834+
{
3835+
"year": [2023, 2023],
3836+
"month": [12, 2],
3837+
"day": [1, 2],
3838+
"hour": [2, np.nan],
3839+
}
3840+
)
3841+
3842+
with tm.assert_produces_warning(None):
3843+
result = to_datetime(df)
3844+
3845+
assert result.iloc[1] is NaT

0 commit comments

Comments
 (0)