Skip to content

Commit a27b04b

Browse files
committed
API: Allow non-tuples in pandas.merge
Closes pandas-dev#34741, while retaining the spirit of the spirit of pandas-dev#34208.
1 parent 18f9460 commit a27b04b

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

doc/source/whatsnew/v1.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ Deprecations
746746
- :meth:`DataFrame.to_dict` has deprecated accepting short names for ``orient`` in future versions (:issue:`32515`)
747747
- :meth:`Categorical.to_dense` is deprecated and will be removed in a future version, use ``np.asarray(cat)`` instead (:issue:`32639`)
748748
- The ``fastpath`` keyword in the ``SingleBlockManager`` constructor is deprecated and will be removed in a future version (:issue:`33092`)
749+
- Providing ``suffixes`` as a ``set`` in :func:`pandas.merge` is deprecated. Provide a tuple instead (:issue:`33740`, `34741`).
749750
- :meth:`Index.is_mixed` is deprecated and will be removed in a future version, check ``index.inferred_type`` directly instead (:issue:`32922`)
750751

751752
- Passing any arguments but the first one to :func:`read_html` as

pandas/core/reshape/merge.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,9 +2072,13 @@ def _items_overlap_with_suffix(left: Index, right: Index, suffixes: Tuple[str, s
20722072
If corresponding suffix is empty, the entry is simply converted to string.
20732073
20742074
"""
2075-
if not isinstance(suffixes, tuple):
2076-
raise TypeError(
2077-
f"suffixes should be tuple of (str, str). But got {type(suffixes).__name__}"
2075+
if isinstance(suffixes, set):
2076+
warnings.warn(
2077+
"Passing 'suffixes' as a set, which is unordered, may result in "
2078+
"unexpected results. Provide 'suffixes' as a tuple instead. In the "
2079+
"future a 'TypeError' will be raised.",
2080+
FutureWarning,
2081+
stacklevel=4,
20782082
)
20792083

20802084
to_rename = left.intersection(right)

pandas/tests/reshape/merge/test_merge.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,18 +2069,12 @@ def test_merge_suffix_error(col1, col2, suffixes):
20692069
pd.merge(a, b, left_index=True, right_index=True, suffixes=suffixes)
20702070

20712071

2072-
@pytest.mark.parametrize(
2073-
"col1, col2, suffixes", [("a", "a", {"a", "b"}), ("a", "a", None), (0, 0, None)],
2074-
)
2075-
def test_merge_suffix_type_error(col1, col2, suffixes):
2076-
a = pd.DataFrame({col1: [1, 2, 3]})
2077-
b = pd.DataFrame({col2: [3, 4, 5]})
2072+
def test_merge_suffix_set():
2073+
a = pd.DataFrame({"a": [1, 2, 3]})
2074+
b = pd.DataFrame({"b": [3, 4, 5]})
20782075

2079-
msg = (
2080-
f"suffixes should be tuple of \\(str, str\\). But got {type(suffixes).__name__}"
2081-
)
2082-
with pytest.raises(TypeError, match=msg):
2083-
pd.merge(a, b, left_index=True, right_index=True, suffixes=suffixes)
2076+
with tm.assert_produces_warning(FutureWarning):
2077+
pd.merge(a, b, left_index=True, right_index=True, suffixes={"left", "right"})
20842078

20852079

20862080
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)