Skip to content

PERF: is_list_like #39852

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 8 commits into from
Feb 17, 2021
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
42 changes: 42 additions & 0 deletions asv_bench/benchmarks/libs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
Benchmarks for code in pandas/_libs, excluding pandas/_libs/tslibs,
which has its own directory
"""
import numpy as np

from pandas._libs.lib import (
is_list_like,
is_scalar,
)

from pandas import (
NA,
NaT,
)
Copy link
Member Author

Choose a reason for hiding this comment

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

@MarcoGorelli when the pre-commit hook runs locally, it resets these to single-line imports, which then fails the CI check. thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

@jbrockmendel have you already merged the latest upstream changes? If not, it might be because your setup.cfg file didn't have force_grid_wrap=True yet?

Copy link
Member Author

Choose a reason for hiding this comment

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

that did it, thanks


# TODO: share with something in pd._testing?
scalars = [
0,
1.0,
1 + 2j,
True,
"foo",
b"bar",
None,
np.datetime64(123, "ns"),
np.timedelta64(123, "ns"),
NaT,
NA,
]
zero_dims = [np.array("123")]
listlikes = [np.array([1, 2, 3]), {0: 1}, {1, 2, 3}, [1, 2, 3], (1, 2, 3)]


class ScalarListLike:
params = scalars + zero_dims + listlikes

def time_is_list_like(self, param):
is_list_like(param)

def time_is_scalar(self, param):
is_scalar(param)
5 changes: 3 additions & 2 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1059,11 +1059,12 @@ def is_list_like(obj: object, allow_sets: bool = True) -> bool:

cdef inline bint c_is_list_like(object obj, bint allow_sets) except -1:
return (
isinstance(obj, abc.Iterable)
# equiv: `isinstance(obj, abc.Iterable)`
hasattr(obj, "__iter__") and not isinstance(obj, type)
# we do not count strings/unicode/bytes as list-like
and not isinstance(obj, (str, bytes))
# exclude zero-dimensional numpy arrays, effectively scalars
and not (util.is_array(obj) and obj.ndim == 0)
and not cnp.PyArray_IsZeroDim(obj)
# exclude sets if allow_sets is False
and not (allow_sets is False and isinstance(obj, abc.Set))
)
Expand Down