Skip to content

REF: make indicator_name a cache_readonly #48040

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 1 commit into from
Aug 12, 2022
Merged
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
32 changes: 17 additions & 15 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from pandas.util._decorators import (
Appender,
Substitution,
cache_readonly,
)
from pandas.util._exceptions import find_stack_level

Expand Down Expand Up @@ -651,16 +652,6 @@ def __init__(

self.indicator = indicator

self.indicator_name: str | None
if isinstance(self.indicator, str):
self.indicator_name = self.indicator
elif isinstance(self.indicator, bool):
self.indicator_name = "_merge" if self.indicator else None
else:
raise ValueError(
"indicator option can only accept boolean or string arguments"
)

if not is_bool(left_index):
raise ValueError(
f"left_index parameter must be of type bool, not {type(left_index)}"
Expand Down Expand Up @@ -753,6 +744,17 @@ def _maybe_drop_cross_column(
if cross_col is not None:
del result[cross_col]

@cache_readonly
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside, maybe we can use

from functools import cache (PY3.9)/ lru_cache

@property
@lru_cache(maxsize=None)/@cache
def _indicator_name(...)

in the future instead

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specific to here or replace cache_readonly in general?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To replace cache_readonly in general. Could gain some nice benefits like cache clearing and thread safety (if I'm viewing the implementation correctly)

def _indicator_name(self) -> str | None:
if isinstance(self.indicator, str):
return self.indicator
elif isinstance(self.indicator, bool):
return "_merge" if self.indicator else None
else:
raise ValueError(
"indicator option can only accept boolean or string arguments"
)

def _indicator_pre_merge(
self, left: DataFrame, right: DataFrame
) -> tuple[DataFrame, DataFrame]:
Expand All @@ -765,7 +767,7 @@ def _indicator_pre_merge(
"Cannot use `indicator=True` option when "
f"data contains a column named {i}"
)
if self.indicator_name in columns:
if self._indicator_name in columns:
raise ValueError(
"Cannot use name of an existing column for indicator column"
)
Expand All @@ -786,13 +788,13 @@ def _indicator_post_merge(self, result: DataFrame) -> DataFrame:
result["_left_indicator"] = result["_left_indicator"].fillna(0)
result["_right_indicator"] = result["_right_indicator"].fillna(0)

result[self.indicator_name] = Categorical(
result[self._indicator_name] = Categorical(
(result["_left_indicator"] + result["_right_indicator"]),
categories=[1, 2, 3],
)
result[self.indicator_name] = result[self.indicator_name].cat.rename_categories(
["left_only", "right_only", "both"]
)
result[self._indicator_name] = result[
self._indicator_name
].cat.rename_categories(["left_only", "right_only", "both"])

result = result.drop(labels=["_left_indicator", "_right_indicator"], axis=1)
return result
Expand Down