Skip to content

PERF: Reject non-string object arrays faster in factorize #51921

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
Mar 13, 2023
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
7 changes: 6 additions & 1 deletion asv_bench/benchmarks/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Factorize:
"uint",
"float",
"object",
"object_str",
"datetime64[ns]",
"datetime64[ns, tz]",
"Int64",
Expand All @@ -46,7 +47,8 @@ def setup(self, unique, sort, dtype):
"int": pd.Index(np.arange(N), dtype="int64"),
"uint": pd.Index(np.arange(N), dtype="uint64"),
"float": pd.Index(np.random.randn(N), dtype="float64"),
"object": string_index,
"object_str": string_index,
"object": pd.Index(np.arange(N), dtype="object"),
"datetime64[ns]": pd.date_range("2011-01-01", freq="H", periods=N),
"datetime64[ns, tz]": pd.date_range(
"2011-01-01", freq="H", periods=N, tz="Asia/Tokyo"
Expand All @@ -62,6 +64,9 @@ def setup(self, unique, sort, dtype):
def time_factorize(self, unique, sort, dtype):
pd.factorize(self.data, sort=sort)

def peakmem_factorize(self, unique, sort, dtype):
pd.factorize(self.data, sort=sort)


class Duplicated:
params = [
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Performance improvements
- Performance improvement when parsing strings to ``boolean[pyarrow]`` dtype (:issue:`51730`)
- Performance improvement when searching an :class:`Index` sliced from other indexes (:issue:`51738`)
- Performance improvement in :meth:`Series.combine_first` (:issue:`51777`)
- Performance improvement in :func:`factorize` for object columns not containing strings (:issue:`51921`)

.. ---------------------------------------------------------------------------
.. _whatsnew_210.bug_fixes:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def _check_object_for_strings(values: np.ndarray) -> str:
# it's cheaper to use a String Hash Table than Object; we infer
# including nulls because that is the only difference between
# StringHashTable and ObjectHashtable
if lib.infer_dtype(values, skipna=False) in ["string"]:
if lib.is_string_array(values, skipna=False):
ndtype = "string"
return ndtype

Expand Down