Skip to content

Commit 30f433b

Browse files
authored
DEPR: automatic alignment on frame.__cmp__(series) (#36795)
1 parent 089fad9 commit 30f433b

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ Deprecations
266266
- Deprecated indexing :class:`DataFrame` rows with datetime-like strings ``df[string]``, use ``df.loc[string]`` instead (:issue:`36179`)
267267
- Deprecated casting an object-dtype index of ``datetime`` objects to :class:`DatetimeIndex` in the :class:`Series` constructor (:issue:`23598`)
268268
- Deprecated :meth:`Index.is_all_dates` (:issue:`27744`)
269+
- Deprecated automatic alignment on comparison operations between :class:`DataFrame` and :class:`Series`, do ``frame, ser = frame.align(ser, axis=1, copy=False)`` before e.g. ``frame == ser`` (:issue:`28759`)
269270
- :meth:`Rolling.count` with ``min_periods=None`` will default to the size of the window in a future version (:issue:`31302`)
270271

271272
.. ---------------------------------------------------------------------------

pandas/core/ops/__init__.py

+13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66
import operator
77
from typing import TYPE_CHECKING, Optional, Set, Type
8+
import warnings
89

910
import numpy as np
1011

@@ -488,6 +489,18 @@ def to_series(right):
488489
elif isinstance(right, ABCSeries):
489490
# axis=1 is default for DataFrame-with-Series op
490491
axis = left._get_axis_number(axis) if axis is not None else 1
492+
493+
if not flex:
494+
if not left.axes[axis].equals(right.index):
495+
warnings.warn(
496+
"Automatic reindexing on DataFrame vs Series comparisons "
497+
"is deprecated and will raise ValueError in a future version. "
498+
"Do `left, right = left.align(right, axis=1, copy=False)` "
499+
"before e.g. `left == right`",
500+
FutureWarning,
501+
stacklevel=3,
502+
)
503+
491504
left, right = left.align(
492505
right, join="outer", axis=axis, level=level, copy=False
493506
)

pandas/tests/arithmetic/test_datetime64.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -318,23 +318,32 @@ def test_dt64arr_timestamp_equality(self, box_with_array):
318318
expected = tm.box_expected([False, True], xbox)
319319
tm.assert_equal(result, expected)
320320

321-
result = ser != ser[0]
321+
warn = FutureWarning if box_with_array is pd.DataFrame else None
322+
with tm.assert_produces_warning(warn):
323+
# alignment for frame vs series comparisons deprecated
324+
result = ser != ser[0]
322325
expected = tm.box_expected([False, True], xbox)
323326
tm.assert_equal(result, expected)
324327

325-
result = ser != ser[1]
328+
with tm.assert_produces_warning(warn):
329+
# alignment for frame vs series comparisons deprecated
330+
result = ser != ser[1]
326331
expected = tm.box_expected([True, True], xbox)
327332
tm.assert_equal(result, expected)
328333

329334
result = ser == ser
330335
expected = tm.box_expected([True, False], xbox)
331336
tm.assert_equal(result, expected)
332337

333-
result = ser == ser[0]
338+
with tm.assert_produces_warning(warn):
339+
# alignment for frame vs series comparisons deprecated
340+
result = ser == ser[0]
334341
expected = tm.box_expected([True, False], xbox)
335342
tm.assert_equal(result, expected)
336343

337-
result = ser == ser[1]
344+
with tm.assert_produces_warning(warn):
345+
# alignment for frame vs series comparisons deprecated
346+
result = ser == ser[1]
338347
expected = tm.box_expected([False, False], xbox)
339348
tm.assert_equal(result, expected)
340349

pandas/tests/frame/test_arithmetic.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -795,13 +795,17 @@ def test_frame_with_zero_len_series_corner_cases():
795795
expected = pd.DataFrame(df.values * np.nan, columns=df.columns)
796796
tm.assert_frame_equal(result, expected)
797797

798-
result = df == ser
798+
with tm.assert_produces_warning(FutureWarning):
799+
# Automatic alignment for comparisons deprecated
800+
result = df == ser
799801
expected = pd.DataFrame(False, index=df.index, columns=df.columns)
800802
tm.assert_frame_equal(result, expected)
801803

802804
# non-float case should not raise on comparison
803805
df2 = pd.DataFrame(df.values.view("M8[ns]"), columns=df.columns)
804-
result = df2 == ser
806+
with tm.assert_produces_warning(FutureWarning):
807+
# Automatic alignment for comparisons deprecated
808+
result = df2 == ser
805809
expected = pd.DataFrame(False, index=df.index, columns=df.columns)
806810
tm.assert_frame_equal(result, expected)
807811

0 commit comments

Comments
 (0)