Skip to content

Commit bdc9a7b

Browse files
authored
DEPR: to_datetime call in Series.combine_first (#62931)
1 parent f59cbcb commit bdc9a7b

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ Other Deprecations
737737
- Deprecated allowing ``fill_value`` that cannot be held in the original dtype (excepting NA values for integer and bool dtypes) in :meth:`Series.shift` and :meth:`DataFrame.shift` (:issue:`53802`)
738738
- Deprecated backward-compatibility behavior for :meth:`DataFrame.select_dtypes` matching "str" dtype when ``np.object_`` is specified (:issue:`61916`)
739739
- Deprecated option "future.no_silent_downcasting", as it is no longer used. In a future version accessing this option will raise (:issue:`59502`)
740+
- Deprecated silent casting of non-datetime 'other' to datetime in :meth:`Series.combine_first` (:issue:`62931`)
740741
- 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`)
741742
- Deprecated the 'inplace' keyword from :meth:`Resampler.interpolate`, as passing ``True`` raises ``AttributeError`` (:issue:`58690`)
742743

pandas/core/series.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3267,6 +3267,16 @@ def combine_first(self, other) -> Series:
32673267
if this.dtype.kind == "M" and other.dtype.kind != "M":
32683268
# TODO: try to match resos?
32693269
other = to_datetime(other)
3270+
warnings.warn(
3271+
# GH#62931
3272+
"Silently casting non-datetime 'other' to datetime in "
3273+
"Series.combine_first is deprecated and will be removed "
3274+
"in a future version. Explicitly cast before calling "
3275+
"combine_first instead.",
3276+
Pandas4Warning,
3277+
stacklevel=find_stack_level(),
3278+
)
3279+
32703280
combined = concat([this, other])
32713281
combined = combined.reindex(new_index)
32723282
return combined.__finalize__(self, method="combine_first")

pandas/tests/series/methods/test_combine_first.py

Lines changed: 8 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,14 @@ 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):
81+
# GH#62931
7882
s0 = to_datetime(Series(["2010", np.nan])).dt.as_unit(unit)
7983
s1 = Series([np.nan, "2011"])
80-
rs = s0.combine_first(s1)
84+
85+
msg = "Silently casting non-datetime 'other' to datetime"
86+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
87+
rs = s0.combine_first(s1)
8188

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

0 commit comments

Comments
 (0)