-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
Add allow_sets-kwarg to is_list_like #23065
Changes from 3 commits
5508857
544b7ec
ae9a45b
3d65d25
15e3265
1941376
7871397
8efee57
0826f34
5686c77
cb588d6
3796080
3647bdd
d5ef14f
4b91d2e
514abd9
d1ff6ab
ece9deb
13e0983
2f5e927
ab3ce96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
from pandas.compat import (PY2, string_types, text_type, | ||
string_and_binary_types, re_type) | ||
from pandas._libs import lib | ||
import warnings | ||
|
||
is_bool = lib.is_bool | ||
|
||
|
@@ -247,7 +248,7 @@ def is_re_compilable(obj): | |
return True | ||
|
||
|
||
def is_list_like(obj): | ||
def is_list_like(obj, strict=None): | ||
""" | ||
Check if the object is list-like. | ||
|
||
|
@@ -259,6 +260,8 @@ def is_list_like(obj): | |
Parameters | ||
---------- | ||
obj : The object to check. | ||
strict : boolean, default None | ||
Whether `set` should be counted as list-like | ||
|
||
Returns | ||
------- | ||
|
@@ -282,12 +285,20 @@ def is_list_like(obj): | |
>>> is_list_like(np.array(2))) | ||
False | ||
""" | ||
|
||
return (isinstance(obj, compat.Iterable) and | ||
# we do not count strings/unicode/bytes as list-like | ||
not isinstance(obj, string_and_binary_types) and | ||
# exclude zero-dimensional numpy arrays, effectively scalars | ||
not (isinstance(obj, np.ndarray) and obj.ndim == 0)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aside from adding the kwarg everywhere, this is the only substantial change of this PR. |
||
if strict is None and isinstance(obj, set): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the In [2]: x = list('aabbc')
In [3]: s1 = set(x)
In [4]: s2 = frozenset(x)
In [5]: isinstance(s2, set)
Out[5]: False
In [6]: isinstance(s2, collections.abc.Set)
Out[6]: True
In [7]: isinstance(s1, collections.abc.Set)
Out[7]: True |
||
# only raise warning if necessary | ||
warnings.warn('is_list_like will in the future return False for sets. ' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a consensus that we want this to be the default future behavior instead of a non-default option? I'm not convinced. Would be interesting to see how many of the existing uses of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this whole idea was just that - just an idea the idea was to change it within pandas only (if this is useful) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since Don't see why that shouldn't apply to external API as well (with deprecation cycle etc.) |
||
'To keep the previous behavior, pass `strict=False`. To ' | ||
'adopt the future behavior and silence this warning, ' | ||
'pass `strict=True`', FutureWarning) | ||
strict = False if strict is None else strict | ||
|
||
list_like = (isinstance(obj, compat.Iterable) | ||
# we do not count strings/unicode/bytes as set-like | ||
and not isinstance(obj, string_and_binary_types) | ||
# exclude zero-dimensional numpy arrays, effectively scalars | ||
and not (isinstance(obj, np.ndarray) and obj.ndim == 0)) | ||
return list_like and (not strict or not isinstance(obj, set)) | ||
|
||
|
||
def is_array_like(obj): | ||
|
@@ -320,7 +331,7 @@ def is_array_like(obj): | |
False | ||
""" | ||
|
||
return is_list_like(obj) and hasattr(obj, "dtype") | ||
return is_list_like(obj, strict=False) and hasattr(obj, "dtype") | ||
|
||
|
||
def is_nested_list_like(obj): | ||
|
@@ -363,8 +374,9 @@ def is_nested_list_like(obj): | |
-------- | ||
is_list_like | ||
""" | ||
return (is_list_like(obj) and hasattr(obj, '__len__') and | ||
len(obj) > 0 and all(is_list_like(item) for item in obj)) | ||
return (is_list_like(obj, strict=False) and hasattr(obj, '__len__') | ||
and len(obj) > 0 and all(is_list_like(item, strict=False) | ||
for item in obj)) | ||
|
||
|
||
def is_dict_like(obj): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a versionadded tag