Skip to content

Commit

Permalink
Back to is_ordered_list_like
Browse files Browse the repository at this point in the history
  • Loading branch information
h-vetinari committed Oct 15, 2018
1 parent cb588d6 commit 3796080
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
6 changes: 3 additions & 3 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
ABCSparseArray, ABCSparseSeries, ABCCategoricalIndex, ABCIndexClass,
ABCDateOffset)
from pandas.core.dtypes.inference import ( # noqa:F401
is_bool, is_integer, is_float, is_number, is_decimal, is_complex,
is_re, is_re_compilable, is_dict_like, is_string_like, is_file_like,
is_list_like, is_nested_list_like, is_sequence, is_named_tuple,
is_bool, is_integer, is_float, is_number, is_decimal, is_complex, is_re,
is_re_compilable, is_dict_like, is_string_like, is_file_like, is_list_like,
is_ordered_list_like, is_nested_list_like, is_sequence, is_named_tuple,
is_hashable, is_iterator, is_array_like, is_scalar, is_interval)


Expand Down
29 changes: 23 additions & 6 deletions pandas/core/dtypes/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def is_re_compilable(obj):
return True


def is_list_like(obj, strict=False):
def is_list_like(obj):
"""
Check if the object is list-like.
Expand All @@ -259,8 +259,6 @@ def is_list_like(obj, strict=False):
Parameters
----------
obj : The object to check.
strict : boolean, default False
If this parameter is True, sets will not be considered list-like
Returns
-------
Expand Down Expand Up @@ -289,9 +287,28 @@ def is_list_like(obj, strict=False):
# we do not count strings/unicode/bytes as list-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)
# exclude sets if ordered_only
and not (strict and isinstance(obj, Set)))
and not (isinstance(obj, np.ndarray) and obj.ndim == 0))


def is_ordered_list_like(obj):
"""
Check if the object is list-like and has a defined order
Works like :meth:`is_list_like` but excludes sets. Note that iterators can
not be inspected for order - this check will return True but it is up to
the user to make sure that their iterators are generated in an ordered way.
Parameters
----------
obj : The object to check.
Returns
-------
is_ordered_list_like : bool
Whether `obj` is an ordered list-like
"""
list_like = is_list_like(obj)
return list_like and not isinstance(obj, Set)


def is_array_like(obj):
Expand Down
9 changes: 5 additions & 4 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
is_object_dtype,
is_string_like,
is_list_like,
is_ordered_list_like,
is_scalar,
is_integer,
is_re)
Expand Down Expand Up @@ -1996,12 +1997,12 @@ def _get_series_list(self, others, ignore_index=False):
elif isinstance(others, np.ndarray) and others.ndim == 2:
others = DataFrame(others, index=idx)
return ([others[x] for x in others], False)
elif is_list_like(others, strict=True):
elif is_ordered_list_like(others):
others = list(others) # ensure iterators do not get read twice etc

# in case of list-like `others`, all elements must be
# either one-dimensional list-likes or scalars
if all(is_list_like(x, strict=True) for x in others):
if all(is_ordered_list_like(x) for x in others):
los = []
join_warn = False
depr_warn = False
Expand Down Expand Up @@ -2029,7 +2030,7 @@ def _get_series_list(self, others, ignore_index=False):
# nested list-likes are forbidden:
# -> elements of nxt must not be list-like
is_legal = ((no_deep and nxt.dtype == object)
or all(not is_list_like(x, strict=True)
or all(not is_ordered_list_like(x)
for x in nxt))

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

Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ def test_is_list_like_fails(ll):
DataFrame(), DataFrame([[1]]), iter([1, 2]), (x for x in [1, 2]),
np.ndarray((2,) * 2), np.ndarray((2,) * 3), np.ndarray((2,) * 4)
])
def test_is_list_like_strict_passes(ll):
assert inference.is_list_like(ll, strict=True)
def test_is_ordered_list_like_strict_passes(ll):
assert inference.is_ordered_list_like(ll)


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


def test_is_array_like():
Expand Down

0 comments on commit 3796080

Please sign in to comment.