From 10f1bdedb72e5f988eaa4ca974a65906f1bbc3f0 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 16 Feb 2021 14:33:14 -0800 Subject: [PATCH 1/7] PERF: is_list_like --- asv_bench/benchmarks/libs.py | 36 ++++++++++++++++++++++++++++++++++++ pandas/_libs/lib.pyx | 5 +++-- 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 asv_bench/benchmarks/libs.py diff --git a/asv_bench/benchmarks/libs.py b/asv_bench/benchmarks/libs.py new file mode 100644 index 0000000000000..8c04b89d99526 --- /dev/null +++ b/asv_bench/benchmarks/libs.py @@ -0,0 +1,36 @@ +""" +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 + +# TODO: share with something in pd._testing? +scalars = [ + 0, + 1.0, + 1 + 2j, + True, + "foo", + b"bar", + None, + np.datetime64(123), + np.timedelta64(123), + NaT, + NA, +] +zero_dims = [np.array("123")] +listlikes = [np.array([1, 2, 3]), {0: "foo"}, set(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) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 3a11e7fbbdf33..be0e777ab7807 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -1044,11 +1044,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)) ) From b8856bec88369ad369803f1354014b5597253729 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 16 Feb 2021 15:01:43 -0800 Subject: [PATCH 2/7] typo fixup --- asv_bench/benchmarks/libs.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/asv_bench/benchmarks/libs.py b/asv_bench/benchmarks/libs.py index 8c04b89d99526..20feaf4f37312 100644 --- a/asv_bench/benchmarks/libs.py +++ b/asv_bench/benchmarks/libs.py @@ -17,8 +17,8 @@ "foo", b"bar", None, - np.datetime64(123), - np.timedelta64(123), + np.datetime64(123, "ns"), + np.timedelta64(123, "ns"), NaT, NA, ] From 60e100857a8f5606413baf6d3d1283720c9cbee2 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 16 Feb 2021 15:12:36 -0800 Subject: [PATCH 3/7] isort fixup --- asv_bench/benchmarks/libs.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/asv_bench/benchmarks/libs.py b/asv_bench/benchmarks/libs.py index 20feaf4f37312..16f669711dfa5 100644 --- a/asv_bench/benchmarks/libs.py +++ b/asv_bench/benchmarks/libs.py @@ -4,9 +4,14 @@ """ import numpy as np -from pandas._libs.lib import is_list_like, is_scalar - -from pandas import NA, NaT +from pandas._libs.lib import ( + is_list_like, + is_scalar, +) +from pandas import ( + NA, + NaT, +) # TODO: share with something in pd._testing? scalars = [ From dda658850cf7fdfb5517466643255daabaa32be0 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 16 Feb 2021 15:21:26 -0800 Subject: [PATCH 4/7] isort fixup --- asv_bench/benchmarks/libs.py | 1 + 1 file changed, 1 insertion(+) diff --git a/asv_bench/benchmarks/libs.py b/asv_bench/benchmarks/libs.py index 16f669711dfa5..1c8de6d9f8e56 100644 --- a/asv_bench/benchmarks/libs.py +++ b/asv_bench/benchmarks/libs.py @@ -8,6 +8,7 @@ is_list_like, is_scalar, ) + from pandas import ( NA, NaT, From caddcfe24e9989b1299375a61ac60ab8bcb83632 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 16 Feb 2021 19:32:36 -0800 Subject: [PATCH 5/7] typo fixup --- asv_bench/benchmarks/libs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asv_bench/benchmarks/libs.py b/asv_bench/benchmarks/libs.py index 1c8de6d9f8e56..e1dbb6b26a546 100644 --- a/asv_bench/benchmarks/libs.py +++ b/asv_bench/benchmarks/libs.py @@ -29,7 +29,7 @@ NA, ] zero_dims = [np.array("123")] -listlikes = [np.array([1, 2, 3]), {0: "foo"}, set(1, 2, 3), [1, 2, 3], (1, 2, 3)] +listlikes = [np.array([1, 2, 3]), {0: "foo"}, set([1, 2, 3]), [1, 2, 3], (1, 2, 3)] class ScalarListLike: From c00dc11718ac743c273e4bd3635ba3586504798e Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 17 Feb 2021 08:11:08 -0800 Subject: [PATCH 6/7] dummy commit to force hook From fec9c1b9fe75ee44ce03d10ee20e53d556ad6c57 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 17 Feb 2021 08:13:06 -0800 Subject: [PATCH 7/7] dummy commit to force hook --- asv_bench/benchmarks/libs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asv_bench/benchmarks/libs.py b/asv_bench/benchmarks/libs.py index e1dbb6b26a546..f5c2397945cea 100644 --- a/asv_bench/benchmarks/libs.py +++ b/asv_bench/benchmarks/libs.py @@ -29,7 +29,7 @@ NA, ] zero_dims = [np.array("123")] -listlikes = [np.array([1, 2, 3]), {0: "foo"}, set([1, 2, 3]), [1, 2, 3], (1, 2, 3)] +listlikes = [np.array([1, 2, 3]), {0: 1}, {1, 2, 3}, [1, 2, 3], (1, 2, 3)] class ScalarListLike: