Skip to content

Commit

Permalink
feat(core): Make semijoin and antijoin accept a Series or DataFrame a…
Browse files Browse the repository at this point in the history
…rgument (#65)

* feat(core): Make semijoin and antijoin accept a Series or DataFrame argument

* Prepare release v0.6.2

* Add compatibility for python 3.9
  • Loading branch information
coroa authored Feb 25, 2025
1 parent 37ad002 commit 9e51182
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
Changelog
=========

v0.6.2 (2024-02-26)
------------------------------------------------------------
* Make :func:`~core.semijoin` and :func:`~core.antijoin` accept a Series or
DataFrame argument to join against :pull:`64`
* Add py.typed file to signal available type hints to downstream packages :pull:`63`

Special thanks to @znichollscr for his first contribution.

v0.6.1 (2024-12-01)
------------------------------------------------------------
* Fix :meth:`~iamc.resolver.Resolver.iamc_aggregate` to return NaN instead of 0
Expand Down
4 changes: 2 additions & 2 deletions src/pandas_indexing/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ def fixna(
return fixindexna(self._obj, axis=axis)

@doc(antijoin, index_or_data="")
def antijoin(self, other: Index, *, axis: Axis = 0):
def antijoin(self, other: Union[Index, Data], *, axis: Axis = 0):
return antijoin(self._obj, other, axis=axis)


class _DataPixAccessor(_PixAccessor):
@doc(semijoin, frame_or_series="")
def semijoin(
self,
other: Index,
other: Union[Index, Data],
*,
how: Literal["left", "right", "inner", "outer"] = "left",
level: Union[str, int, None] = None,
Expand Down
18 changes: 14 additions & 4 deletions src/pandas_indexing/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ def ensure_multiindex(s: T) -> T:
)
def semijoin(
frame_or_series: S,
other: Index,
other: Union[Index, Data],
*,
how: Literal["left", "right", "inner", "outer"] = "left",
level: Union[str, int, None] = None,
Expand All @@ -518,11 +518,15 @@ def semijoin(
) -> S:
"""Semijoin *frame_or_series* by index *other*.
Joins indexes of both inputs and then reindexes the primary data input with
the resulting joined index allowing for filling values.
Parameters
----------\
{frame_or_series}
other : Index
Other index to join with
other : Index or Data
Other index to join with, if a DataFrame or Series is provided
its axis is extracted.
how : {{'left', 'right', 'inner', 'outer'}}
Join method to use
level : None or str or int or
Expand Down Expand Up @@ -557,6 +561,9 @@ def semijoin(

index = get_axis(frame_or_series, axis)

if isinstance(other, (Series, DataFrame)):
other = get_axis(other, axis)

if level is None:
index = ensure_multiindex(index)
other = ensure_multiindex(other)
Expand Down Expand Up @@ -607,7 +614,7 @@ def semijoin(
)
def antijoin(
index_or_data: S,
other: Index,
other: Union[Index, Data],
*,
level: Union[str, int, None] = None,
axis: Axis = 0,
Expand Down Expand Up @@ -644,6 +651,9 @@ def antijoin(

index = get_axis(index_or_data, axis)

if isinstance(other, (Series, DataFrame)):
other = get_axis(other, axis)

_, left_idx, right_idx = index.join(
other, how="left", level=level, return_indexers=True
)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,15 @@ def test_semijoin(mdf, mseries):
),
)

# Right-join on series with series input
assert_series_equal(
semijoin(mseries, Series(7, index), how="right"),
Series(
r_[mseries.values[1:3], nan],
index=index.reorder_levels(["new", "str", "num"]),
),
)


def test_antijoin(mdf, mseries):
index = MultiIndex.from_tuples(
Expand All @@ -555,6 +564,9 @@ def test_antijoin(mdf, mseries):
# Index
assert_index_equal(antijoin(mseries.index, index), mseries.index[[0]])

# Series with Series other
assert_series_equal(antijoin(mseries, Series(7, index)), mseries.iloc[[0]])


def test_to_tidy(mdf, mseries, midx):
assert_frame_equal(
Expand Down

0 comments on commit 9e51182

Please sign in to comment.