Skip to content

CLN: Move _convert_to_list_like to common #34127

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 9 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 1 addition & 14 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
is_dtype_equal,
is_extension_array_dtype,
is_integer_dtype,
is_iterator,
is_list_like,
is_object_dtype,
is_scalar,
is_sequence,
is_timedelta64_dtype,
needs_i8_conversion,
)
Expand Down Expand Up @@ -324,7 +322,7 @@ def __init__(
# of numpy
values = maybe_infer_to_datetimelike(values, convert_dates=True)
if not isinstance(values, np.ndarray):
values = _convert_to_list_like(values)
values = com.convert_to_list_like(values)

# By convention, empty lists result in object dtype:
sanitize_dtype = np.dtype("O") if len(values) == 0 else None
Expand Down Expand Up @@ -2647,17 +2645,6 @@ def recode_for_categories(codes: np.ndarray, old_categories, new_categories):
return new_codes


def _convert_to_list_like(list_like):
if hasattr(list_like, "dtype"):
return list_like
if isinstance(list_like, list):
return list_like
if is_sequence(list_like) or isinstance(list_like, tuple) or is_iterator(list_like):
return list(list_like)

return [list_like]


def factorize_from_iterable(values):
"""
Factorize an input `values` into `categories` and `codes`. Preserves
Expand Down
35 changes: 27 additions & 8 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
Note: pandas.core.common is *not* part of the public API.
"""

import collections
from collections import abc
from collections import abc, defaultdict
Copy link
Member Author

Choose a reason for hiding this comment

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

Slightly unrelated, but this seems better than mixing the two import styles

Copy link
Member

Choose a reason for hiding this comment

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

agreed

from datetime import datetime, timedelta
from functools import partial
import inspect
from typing import Any, Collection, Iterable, Union
from typing import Any, Collection, Iterable, List, Union

import numpy as np

from pandas._libs import lib, tslibs
from pandas._typing import T
from pandas._typing import AnyArrayLike, Scalar, T
from pandas.compat.numpy import _np_version_under1p17

from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
Expand All @@ -24,7 +23,12 @@
is_extension_array_dtype,
is_integer,
)
from pandas.core.dtypes.generic import ABCIndex, ABCIndexClass, ABCSeries
from pandas.core.dtypes.generic import (
ABCExtensionArray,
ABCIndex,
ABCIndexClass,
ABCSeries,
)
from pandas.core.dtypes.inference import _iterable_not_string
from pandas.core.dtypes.missing import isna, isnull, notnull # noqa

Expand Down Expand Up @@ -367,12 +371,12 @@ def standardize_mapping(into):
Series.to_dict
"""
if not inspect.isclass(into):
if isinstance(into, collections.defaultdict):
return partial(collections.defaultdict, into.default_factory)
if isinstance(into, defaultdict):
return partial(defaultdict, into.default_factory)
into = type(into)
if not issubclass(into, abc.Mapping):
raise TypeError(f"unsupported type: {into}")
elif into == collections.defaultdict:
elif into == defaultdict:
raise TypeError("to_dict() only accepts initialized defaultdicts")
return into

Expand Down Expand Up @@ -473,3 +477,18 @@ def f(x):
f = mapper

return f


def convert_to_list_like(
values: Union[Scalar, Iterable, AnyArrayLike]
) -> Union[List, AnyArrayLike]:
Copy link
Member Author

Choose a reason for hiding this comment

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

Had to rewrite this a bit to get it to type check

Copy link
Member

Choose a reason for hiding this comment

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

@dsaxton i noticed you used ABCIndex instead of ABCIndexClass, was that intentional?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think so, definitely change if you feel it's not correct

"""
Convert list-like or scalar input to list-like. List, numpy and pandas array-like
inputs are returned unmodified whereas others are converted to list.
"""
if isinstance(values, (list, np.ndarray, ABCIndex, ABCSeries, ABCExtensionArray)):
return values
elif isinstance(values, abc.Iterable) and not isinstance(values, str):
return list(values)

return [values]