8000 Add allow_sets-kwarg to is_list_like by h-vetinari · Pull Request #23065 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

Add allow_sets-kwarg to is_list_like #23065

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 21 commits into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Revert "Back to is_ordered_list_like"
This reverts commit 3796080.
  • Loading branch information
h-vetinari committed Oct 16, 2018
commit 4b91d2e5558d725eaa65f74c2bead41e432be61f
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_ordered_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_nested_list_like, is_sequence, is_named_tuple,
is_hashable, is_iterator, is_array_like, is_scalar, is_interval)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is somewhat of an artefact of the version with is_ordered_list_like, where I tried to group these methods by similarity (i.e. scalar dtypes, regexes, containers), but I decided to keep it because I think it helps. Can revert that part of course

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, on any change, pls try to do the minimal changeset. This will lessen reviewer burden and make things go faster.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"yes, please try to do minimal changeset [next time]" or "yes please revert"?

Copy link
Contributor

Choose a reason for hiding this comment

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

Fine as is for now.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok for now, but generally pls don't change unrelated things.



Expand Down
29 changes: 6 additions & 23 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):
def is_list_like(obj, strict=False):
"""
Check if the object is list-like.

Expand All @@ -259,6 +259,8 @@ def is_list_like(obj):
Parameters
----------
obj : The object to check.
strict : boolean, default False
If this parameter is True, sets will not be considered list-like

Copy link
Contributor

Choose a reason for hiding this comment

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

add a versionadded tag

Returns
-------
Expand Down Expand Up @@ -287,28 +289,9 @@ def is_list_like(obj):
# we do not count strings/unicode/bytes as list-like
and not isinstance(obj, string_and_binary_types)
Copy link
Contributor

Choose a reason for hiding this comment

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

this is not correct, leave the and where it was

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PEP8 is clear about this (https://www.python.org/dev/peps/pep-0008/#should-a-line-break-before-or-after-a-binary-operator)

Binary operators (like and is one) should come after a line-break. It's also more readable.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, changing this is in principle fine, we have been following that PEP8 rule recently (typically we only want such changes on lines that are already touched by the PR, but since you are here already touching the function some lines below, I would say it is fine).

Note that that is a recent change in PEP8, so you will see many places in the code that does it differently.

# exclude zero-dimensional numpy arrays, effectively scalars
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)
and not (isinstance(obj, np.ndarray) and obj.ndim == 0)
# exclude sets if ordered_only
and not (strict and isinstance(obj, Set)))


def is_array_like(obj):
Expand Down
1 change: 0 additions & 1 deletion pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2030,7 +2030,6 @@ def _get_series_list(self, others, ignore_index=False):
# -> elements of nxt must not be list-like
is_legal = ((no_deep and nxt.dtype == object)
or all(not is_list_like(x) for x in nxt))

# DataFrame is false positive of is_legal
# because "x in df" returns column names
if not is_legal or isinstance(nxt, DataFrame):
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_ordered_list_like_strict_passes(ll):
assert inference.is_ordered_list_like(ll)
def test_is_list_like_strict_passes(ll):
assert inference.is_list_like(ll, strict=True)


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


def test_is_array_like():
Expand Down
0