Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ Other Deprecations
- 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`)
- Deprecated backward-compatibility behavior for :meth:`DataFrame.select_dtypes` matching "str" dtype when ``np.object_`` is specified (:issue:`61916`)
- Deprecated option "future.no_silent_downcasting", as it is no longer used. In a future version accessing this option will raise (:issue:`59502`)
- Deprecated silent casting of non-datetime 'other' to datetime in :meth:`Series.combine_first` (:issue:`62931`)
- 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`)
- Deprecated the 'inplace' keyword from :meth:`Resampler.interpolate`, as passing ``True`` raises ``AttributeError`` (:issue:`58690`)

Expand Down
10 changes: 10 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3271,6 +3271,16 @@ def combine_first(self, other) -> Series:
if this.dtype.kind == "M" and other.dtype.kind != "M":
# TODO: try to match resos?
other = to_datetime(other)
warnings.warn(
# GH#62931
"Silently casting non-datetime 'other' to datetime in "
"Series.combine_first is deprecated and will be removed "
"in a future version. Explicitly cast before calling "
"combine_first instead.",
Pandas4Warning,
stacklevel=find_stack_level(),
)

combined = concat([this, other])
combined = combined.reindex(new_index)
return combined.__finalize__(self, method="combine_first")
Expand Down
9 changes: 8 additions & 1 deletion pandas/tests/series/methods/test_combine_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import numpy as np

from pandas.errors import Pandas4Warning

import pandas as pd
from pandas import (
Period,
Expand Down Expand Up @@ -75,9 +77,14 @@ def test_combine_first_dt64(self, unit):
xp = to_datetime(Series(["2010", "2011"])).dt.as_unit(unit)
tm.assert_series_equal(rs, xp)

def test_combine_first_dt64_casting_deprecation(self, unit):
# GH#62931
s0 = to_datetime(Series(["2010", np.nan])).dt.as_unit(unit)
s1 = Series([np.nan, "2011"])
rs = s0.combine_first(s1)

msg = "Silently casting non-datetime 'other' to datetime"
with tm.assert_produces_warning(Pandas4Warning, match=msg):
rs = s0.combine_first(s1)

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

Expand Down
Loading