Skip to content

Commit 3be1a58

Browse files
committed
DEPR: to_datetime call in Series.combine_first
1 parent 46ed6b1 commit 3be1a58

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ Other Deprecations
739739
- Deprecated option "future.no_silent_downcasting", as it is no longer used. In a future version accessing this option will raise (:issue:`59502`)
740740
- Deprecated slicing on a :class:`Series` or :class:`DataFrame` with a :class:`DatetimeIndex` using a ``datetime.date`` object, explicitly cast to :class:`Timestamp` instead (:issue:`35830`)
741741
- Deprecated the 'inplace' keyword from :meth:`Resampler.interpolate`, as passing ``True`` raises ``AttributeError`` (:issue:`58690`)
742+
- Deprecated silent casting of non-datetime 'other' to datetime in :meth:`Series.combine_first` (:issue:`??`)
742743

743744
.. ---------------------------------------------------------------------------
744745
.. _whatsnew_300.prior_deprecations:

pandas/core/series.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3271,6 +3271,15 @@ def combine_first(self, other) -> Series:
32713271
if this.dtype.kind == "M" and other.dtype.kind != "M":
32723272
# TODO: try to match resos?
32733273
other = to_datetime(other)
3274+
warnings.warn(
3275+
"Silently casting non-datetime 'other' to datetime in "
3276+
"Series.combine_first is deprecated and will be removed "
3277+
"in a future version. Explicitly cast before calling "
3278+
"combine_first instead.",
3279+
Pandas4Warning,
3280+
stacklevel=find_stack_level(),
3281+
)
3282+
32743283
combined = concat([this, other])
32753284
combined = combined.reindex(new_index)
32763285
return combined.__finalize__(self, method="combine_first")

pandas/tests/series/methods/test_combine_first.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import numpy as np
44

5+
from pandas.errors import Pandas4Warning
6+
57
import pandas as pd
68
from pandas import (
79
Period,
@@ -75,9 +77,13 @@ def test_combine_first_dt64(self, unit):
7577
xp = to_datetime(Series(["2010", "2011"])).dt.as_unit(unit)
7678
tm.assert_series_equal(rs, xp)
7779

80+
def test_combine_first_dt64_casting_deprecation(self, unit):
7881
s0 = to_datetime(Series(["2010", np.nan])).dt.as_unit(unit)
7982
s1 = Series([np.nan, "2011"])
80-
rs = s0.combine_first(s1)
83+
84+
msg = "Silently casting non-datetime 'other' to datetime"
85+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
86+
rs = s0.combine_first(s1)
8187

8288
xp = Series([datetime(2010, 1, 1), "2011"], dtype=f"datetime64[{unit}]")
8389

0 commit comments

Comments
 (0)