-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
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
Changes from all commits
b9c32a1
fd7aee0
64b1eed
dbae893
0a10a8d
6ff0316
f48dff9
ef10a00
9a6cc20
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 |
---|---|---|
|
@@ -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 | ||
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 | ||
|
@@ -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 | ||
|
||
|
@@ -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 | ||
|
||
|
@@ -473,3 +477,18 @@ def f(x): | |
f = mapper | ||
|
||
return f | ||
|
||
|
||
def convert_to_list_like( | ||
values: Union[Scalar, Iterable, AnyArrayLike] | ||
) -> Union[List, AnyArrayLike]: | ||
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. Had to rewrite this a bit to get it to type check 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. @dsaxton i noticed you used ABCIndex instead of ABCIndexClass, was that intentional? 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 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] |
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.
Slightly unrelated, but this seems better than mixing the two import styles
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.
agreed