Skip to content

Commit 58d0faa

Browse files
DEPR: deprecate passing Series to Index.join
- Add Pandas4Warning when Series is passed to Index.join - Add test for deprecation warning - Update whatsnew for v3.0.0 Closes #62897
1 parent 82fa271 commit 58d0faa

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 passing a :class:`Series` to :meth:`Index.join`, pass ``Index.join(other.index)`` instead (:issue:`62897`)
740741
- Deprecated silent casting of non-datetime 'other' to datetime in :meth:`Series.combine_first` (:issue:`62931`)
741742
- 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`)
742743
- Deprecated the 'inplace' keyword from :meth:`Resampler.interpolate`, as passing ``True`` raises ``AttributeError`` (:issue:`58690`)

pandas/core/indexes/base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
from pandas.errors import (
7070
DuplicateLabelError,
7171
InvalidIndexError,
72+
Pandas4Warning
7273
)
7374
from pandas.util._decorators import (
7475
Appender,
@@ -4426,6 +4427,15 @@ def join(
44264427
(Index([1, 2, 3, 4, 5, 6], dtype='int64'),
44274428
array([ 0, 1, 2, -1, -1, -1]), array([-1, -1, -1, 0, 1, 2]))
44284429
"""
4430+
if not isinstance(other, Index):
4431+
warnings.warn(
4432+
f"Passing a Series to {type(self).__name__}.join is deprecated "
4433+
"and will raise in a future version. "
4434+
"Pass Index.join(other.index) instead.",
4435+
Pandas4Warning,
4436+
stacklevel=find_stack_level()
4437+
)
4438+
44294439
other = ensure_index(other)
44304440
sort = sort or how == "outer"
44314441

pandas/tests/indexes/test_common.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
RangeIndex,
2929
)
3030
import pandas._testing as tm
31-
31+
from pandas.errors import Pandas4Warning
3232

3333
class TestCommon:
3434
@pytest.mark.parametrize("name", [None, "new_name"])
@@ -523,3 +523,10 @@ def test_to_frame_name_tuple_multiindex():
523523
result = idx.to_frame(name=(1, 2))
524524
expected = pd.DataFrame([1], columns=MultiIndex.from_arrays([[1], [2]]), index=idx)
525525
tm.assert_frame_equal(result, expected)
526+
527+
def test_join_series_deprecated():
528+
# GH#62897
529+
idx = pd.Index([1, 2])
530+
ser = pd.Series([1, 2, 2])
531+
with tm.assert_produces_warning(Pandas4Warning, match="Passing a Series"):
532+
idx.join(ser)

0 commit comments

Comments
 (0)