Skip to content

Commit 3796080

Browse files
committed
Back to is_ordered_list_like
1 parent cb588d6 commit 3796080

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

pandas/core/dtypes/common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
ABCSparseArray, ABCSparseSeries, ABCCategoricalIndex, ABCIndexClass,
1919
ABCDateOffset)
2020
from pandas.core.dtypes.inference import ( # noqa:F401
21-
is_bool, is_integer, is_float, is_number, is_decimal, is_complex,
22-
is_re, is_re_compilable, is_dict_like, is_string_like, is_file_like,
23-
is_list_like, is_nested_list_like, is_sequence, is_named_tuple,
21+
is_bool, is_integer, is_float, is_number, is_decimal, is_complex, is_re,
22+
is_re_compilable, is_dict_like, is_string_like, is_file_like, is_list_like,
23+
is_ordered_list_like, is_nested_list_like, is_sequence, is_named_tuple,
2424
is_hashable, is_iterator, is_array_like, is_scalar, is_interval)
2525

2626

pandas/core/dtypes/inference.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def is_re_compilable(obj):
247247
return True
248248

249249

250-
def is_list_like(obj, strict=False):
250+
def is_list_like(obj):
251251
"""
252252
Check if the object is list-like.
253253
@@ -259,8 +259,6 @@ def is_list_like(obj, strict=False):
259259
Parameters
260260
----------
261261
obj : The object to check.
262-
strict : boolean, default False
263-
If this parameter is True, sets will not be considered list-like
264262
265263
Returns
266264
-------
@@ -289,9 +287,28 @@ def is_list_like(obj, strict=False):
289287
# we do not count strings/unicode/bytes as list-like
290288
and not isinstance(obj, string_and_binary_types)
291289
# exclude zero-dimensional numpy arrays, effectively scalars
292-
and not (isinstance(obj, np.ndarray) and obj.ndim == 0)
293-
# exclude sets if ordered_only
294-
and not (strict and isinstance(obj, Set)))
290+
and not (isinstance(obj, np.ndarray) and obj.ndim == 0))
291+
292+
293+
def is_ordered_list_like(obj):
294+
"""
295+
Check if the object is list-like and has a defined order
296+
297+
Works like :meth:`is_list_like` but excludes sets. Note that iterators can
298+
not be inspected for order - this check will return True but it is up to
299+
the user to make sure that their iterators are generated in an ordered way.
300+
301+
Parameters
302+
----------
303+
obj : The object to check.
304+
305+
Returns
306+
-------
307+
is_ordered_list_like : bool
308+
Whether `obj` is an ordered list-like
309+
"""
310+
list_like = is_list_like(obj)
311+
return list_like and not isinstance(obj, Set)
295312

296313

297314
def is_array_like(obj):

pandas/core/strings.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
is_object_dtype,
1212
is_string_like,
1313
is_list_like,
14+
is_ordered_list_like,
1415
is_scalar,
1516
is_integer,
1617
is_re)
@@ -1996,12 +1997,12 @@ def _get_series_list(self, others, ignore_index=False):
19961997
elif isinstance(others, np.ndarray) and others.ndim == 2:
19971998
others = DataFrame(others, index=idx)
19981999
return ([others[x] for x in others], False)
1999-
elif is_list_like(others, strict=True):
2000+
elif is_ordered_list_like(others):
20002001
others = list(others) # ensure iterators do not get read twice etc
20012002

20022003
# in case of list-like `others`, all elements must be
20032004
# either one-dimensional list-likes or scalars
2004-
if all(is_list_like(x, strict=True) for x in others):
2005+
if all(is_ordered_list_like(x) for x in others):
20052006
los = []
20062007
join_warn = False
20072008
depr_warn = False
@@ -2029,7 +2030,7 @@ def _get_series_list(self, others, ignore_index=False):
20292030
# nested list-likes are forbidden:
20302031
# -> elements of nxt must not be list-like
20312032
is_legal = ((no_deep and nxt.dtype == object)
2032-
or all(not is_list_like(x, strict=True)
2033+
or all(not is_ordered_list_like(x)
20332034
for x in nxt))
20342035

20352036
# DataFrame is false positive of is_legal
@@ -2048,7 +2049,7 @@ def _get_series_list(self, others, ignore_index=False):
20482049
'deprecated and will be removed in a future '
20492050
'version.', FutureWarning, stacklevel=3)
20502051
return (los, join_warn)
2051-
elif all(not is_list_like(x, strict=True) for x in others):
2052+
elif all(not is_ordered_list_like(x) for x in others):
20522053
return ([Series(others, index=idx)], False)
20532054
raise TypeError(err_msg)
20542055

pandas/tests/dtypes/test_inference.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ def test_is_list_like_fails(ll):
8888
DataFrame(), DataFrame([[1]]), iter([1, 2]), (x for x in [1, 2]),
8989
np.ndarray((2,) * 2), np.ndarray((2,) * 3), np.ndarray((2,) * 4)
9090
])
91-
def test_is_list_like_strict_passes(ll):
92-
assert inference.is_list_like(ll, strict=True)
91+
def test_is_ordered_list_like_strict_passes(ll):
92+
assert inference.is_ordered_list_like(ll)
9393

9494

9595
@pytest.mark.parametrize("ll", [1, '2', object(), str, np.array(2),
9696
{1, 'a'}, frozenset({1, 'a'})])
97-
def test_is_list_like_strict_fails(ll):
97+
def test_is_ordered_list_like_fails(ll):
9898
# GH 23061
99-
assert not inference.is_list_like(ll, strict=True)
99+
assert not inference.is_ordered_list_like(ll)
100100

101101

102102
def test_is_array_like():

0 commit comments

Comments
 (0)