-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
ENH: Add left_semi merge #62961
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
base: main
Are you sure you want to change the base?
ENH: Add left_semi merge #62961
Changes from 11 commits
a23a2e0
267e29d
58ea845
e39bc4c
dca4fe9
cf7a325
0285efb
3060169
3929b17
94593d4
f2613c3
0bed787
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1391,6 +1391,33 @@ cdef class PyObjectHashTable(HashTable): | |
| k = kh_put_pymap(self.table, <PyObject*>val, &ret) | ||
| self.table.vals[k] = i | ||
|
|
||
| @cython.wraparound(False) | ||
| @cython.boundscheck(False) | ||
| def hash_inner_join(self, ndarray[object] values, object mask = None) -> tuple[ndarray, ndarray]: | ||
| cdef: | ||
| Py_ssize_t i, n = len(values) | ||
| object val | ||
| khiter_t k | ||
| Int64Vector locs = Int64Vector() | ||
| Int64Vector self_locs = Int64Vector() | ||
| Int64VectorData *l | ||
| Int64VectorData *sl | ||
| # mask not implemented | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth raising |
||
|
|
||
| l = &locs.data | ||
| sl = &self_locs.data | ||
|
|
||
| for i in range(n): | ||
| val = values[i] | ||
| hash(val) | ||
|
|
||
| k = kh_get_pymap(self.table, <PyObject*>val) | ||
| if k != self.table.n_buckets: | ||
| append_data_int64(l, i) | ||
| append_data_int64(sl, self.table.vals[k]) | ||
|
|
||
| return self_locs.to_array(), locs.to_array() | ||
|
|
||
| def lookup(self, ndarray[object] values, object mask = None) -> ndarray: | ||
| # -> np.ndarray[np.intp] | ||
| # mask not yet implemented | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -325,7 +325,8 @@ | |
| ----------%s | ||
| right : DataFrame or named Series | ||
| Object to merge with. | ||
| how : {'left', 'right', 'outer', 'inner', 'cross', 'left_anti', 'right_anti'}, | ||
| how : {'left', 'right', 'outer', 'inner', 'left_semi', 'cross', 'left_anti', | ||
| 'right_anti'}, | ||
| default 'inner' | ||
| Type of merge to be performed. | ||
|
|
||
|
|
@@ -337,6 +338,10 @@ | |
| join; sort keys lexicographically. | ||
| * inner: use intersection of keys from both frames, similar to a SQL inner | ||
| join; preserve the order of the left keys. | ||
| * left_semi: Filter for rows in the left that have a match on the right; | ||
| preserve the order of the left keys. | ||
|
||
|
|
||
| .. versionadded:: 3.0 | ||
| * cross: creates the cartesian product from both frames, preserves the order | ||
| of the left keys. | ||
| * left_anti: use only keys from left frame that are not in right frame, similar | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import pytest | ||
|
|
||
| import pandas.util._test_decorators as td | ||
|
|
||
| import pandas as pd | ||
| import pandas._testing as tm | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "vals_left, vals_right, dtype", | ||
| [ | ||
| ([1, 2, 3], [1, 2], "int64"), | ||
| ([1.5, 2.5, 3.5], [1.5, 2.5], "float64"), | ||
| ([True, True, False], [True, True], "bool"), | ||
| (["a", "b", "c"], ["a", "b"], "object"), | ||
| pytest.param( | ||
| ["a", "b", "c"], | ||
| ["a", "b"], | ||
| "string[pyarrow]", | ||
| marks=td.skip_if_no("pyarrow"), | ||
| ), | ||
| pytest.param( | ||
| ["a", "b", "c"], | ||
| ["a", "b"], | ||
| "str", | ||
| marks=td.skip_if_no("pyarrow"), | ||
| ), | ||
| ], | ||
| ) | ||
| def test_left_semi(vals_left, vals_right, dtype): | ||
| vals_left = pd.Series(vals_left, dtype=dtype) | ||
| vals_right = pd.Series(vals_right, dtype=dtype) | ||
| left = pd.DataFrame({"a": vals_left, "b": [1, 2, 3]}) | ||
| right = pd.DataFrame({"a": vals_right, "c": 1}) | ||
| expected = pd.DataFrame({"a": vals_right, "b": [1, 2]}) | ||
| result = left.merge(right, how="left_semi") | ||
| tm.assert_frame_equal(result, expected) | ||
|
|
||
| result = left.join(right.set_index("a"), how="left_semi", on="a") | ||
| tm.assert_frame_equal(result, expected) | ||
|
|
||
| result = left.set_index("a").join(right.set_index("a"), how="left_semi") | ||
| tm.assert_frame_equal(result, expected.set_index("a")) | ||
|
|
||
| result = left.set_index("a").merge( | ||
| right.set_index("a"), how="left_semi", left_index=True, right_index=True | ||
| ) | ||
| tm.assert_frame_equal(result, expected.set_index("a")) | ||
|
|
||
| result = left.set_index("a").merge( | ||
| right, how="left_semi", left_index=True, right_on="a" | ||
| ) | ||
| tm.assert_frame_equal(result, expected.set_index("a")) | ||
|
|
||
| result = left.merge( | ||
| right.set_index("a"), how="left_semi", right_index=True, left_on="a" | ||
| ) | ||
| tm.assert_frame_equal(result, expected) | ||
|
|
||
| right = pd.DataFrame({"d": vals_right, "c": 1}) | ||
| result = left.merge(right, how="left_semi", left_on="a", right_on="d") | ||
| tm.assert_frame_equal(result, expected) | ||
|
|
||
| right = pd.DataFrame({"d": vals_right, "c": 1}) | ||
| result = left.merge(right, how="left_semi", left_on=["a", "b"], right_on=["d", "c"]) | ||
| tm.assert_frame_equal(result, expected.head(1)) | ||
|
|
||
|
|
||
| def test_left_semi_invalid(): | ||
| left = pd.DataFrame({"a": [1, 2, 3], "b": [1, 2, 3]}) | ||
| right = pd.DataFrame({"a": [1, 2], "c": 1}) | ||
| msg = "indicator is not supported for semi-join." | ||
| with pytest.raises(NotImplementedError, match=msg): | ||
| left.merge(right, how="left_semi", indicator=True) | ||
|
|
||
| msg = "sort is not supported for semi-join. Sort your DataFrame afterwards." | ||
| with pytest.raises(NotImplementedError, match=msg): | ||
| left.merge(right, how="left_semi", sort=True) |
Uh oh!
There was an error while loading. Please reload this page.