Skip to content

ENH: improve error reporting for dup columns in merge_asof #50150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 13, 2022
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/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Other enhancements
- :func:`timedelta_range` now supports a ``unit`` keyword ("s", "ms", "us", or "ns") to specify the desired resolution of the output index (:issue:`49824`)
- :meth:`DataFrame.to_json` now supports a ``mode`` keyword with supported inputs 'w' and 'a'. Defaulting to 'w', 'a' can be used when lines=True and orient='records' to append record oriented json lines to an existing json file. (:issue:`35849`)
- Added ``name`` parameter to :meth:`IntervalIndex.from_breaks`, :meth:`IntervalIndex.from_arrays` and :meth:`IntervalIndex.from_tuples` (:issue:`48911`)
- Improved error message for :func:`merge_asof` when join-columns were duplicated (:issue:`50102`)
- Added :meth:`Index.infer_objects` analogous to :meth:`Series.infer_objects` (:issue:`50034`)
- Added ``copy`` parameter to :meth:`Series.infer_objects` and :meth:`DataFrame.infer_objects`, passing ``False`` will avoid making copies for series or columns that are already non-object or where no better dtype can be inferred (:issue:`50096`)
- :meth:`DataFrame.plot.hist` now recognizes ``xlabel`` and ``ylabel`` arguments (:issue:`49793`)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1933,7 +1933,7 @@ def _validate_left_right_on(self, left_on, right_on):
lo_dtype = left_on_0.dtype
else:
lo_dtype = (
self.left[left_on_0].dtype
self.left._get_label_or_level_values(left_on_0).dtype
if left_on_0 in self.left.columns
else self.left.index.get_level_values(left_on_0)
)
Expand All @@ -1946,7 +1946,7 @@ def _validate_left_right_on(self, left_on, right_on):
ro_dtype = right_on_0.dtype
else:
ro_dtype = (
self.right[right_on_0].dtype
self.right._get_label_or_level_values(right_on_0).dtype
if right_on_0 in self.right.columns
else self.right.index.get_level_values(right_on_0)
)
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/reshape/merge/test_merge_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -1567,3 +1567,18 @@ def test_merge_asof_array_as_on():
}
)
tm.assert_frame_equal(result, expected)


def test_merge_asof_raise_for_duplicate_columns():
# GH#50102
left = pd.DataFrame([[1, 2, "a"]], columns=["a", "a", "left_val"])
right = pd.DataFrame([[1, 1, 1]], columns=["a", "a", "right_val"])

with pytest.raises(ValueError, match="column label 'a'"):
merge_asof(left, right, on="a")

with pytest.raises(ValueError, match="column label 'a'"):
merge_asof(left, right, left_on="a", right_on="right_val")

with pytest.raises(ValueError, match="column label 'a'"):
merge_asof(left, right, left_on="left_val", right_on="a")