Skip to content

Commit dfaa507

Browse files
authored
CLN: Move _convert_to_list_like to common (#34127)
1 parent 3b1469f commit dfaa507

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

pandas/core/arrays/categorical.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@
2626
is_dtype_equal,
2727
is_extension_array_dtype,
2828
is_integer_dtype,
29-
is_iterator,
3029
is_list_like,
3130
is_object_dtype,
3231
is_scalar,
33-
is_sequence,
3432
is_timedelta64_dtype,
3533
needs_i8_conversion,
3634
)
@@ -324,7 +322,7 @@ def __init__(
324322
# of numpy
325323
values = maybe_infer_to_datetimelike(values, convert_dates=True)
326324
if not isinstance(values, np.ndarray):
327-
values = _convert_to_list_like(values)
325+
values = com.convert_to_list_like(values)
328326

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

26492647

2650-
def _convert_to_list_like(list_like):
2651-
if hasattr(list_like, "dtype"):
2652-
return list_like
2653-
if isinstance(list_like, list):
2654-
return list_like
2655-
if is_sequence(list_like) or isinstance(list_like, tuple) or is_iterator(list_like):
2656-
return list(list_like)
2657-
2658-
return [list_like]
2659-
2660-
26612648
def factorize_from_iterable(values):
26622649
"""
26632650
Factorize an input `values` into `categories` and `codes`. Preserves

pandas/core/common.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
Note: pandas.core.common is *not* part of the public API.
55
"""
66

7-
import collections
8-
from collections import abc
7+
from collections import abc, defaultdict
98
from datetime import datetime, timedelta
109
from functools import partial
1110
import inspect
12-
from typing import Any, Collection, Iterable, Union
11+
from typing import Any, Collection, Iterable, List, Union
1312

1413
import numpy as np
1514

1615
from pandas._libs import lib, tslibs
17-
from pandas._typing import T
16+
from pandas._typing import AnyArrayLike, Scalar, T
1817
from pandas.compat.numpy import _np_version_under1p17
1918

2019
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
@@ -24,7 +23,12 @@
2423
is_extension_array_dtype,
2524
is_integer,
2625
)
27-
from pandas.core.dtypes.generic import ABCIndex, ABCIndexClass, ABCSeries
26+
from pandas.core.dtypes.generic import (
27+
ABCExtensionArray,
28+
ABCIndex,
29+
ABCIndexClass,
30+
ABCSeries,
31+
)
2832
from pandas.core.dtypes.inference import _iterable_not_string
2933
from pandas.core.dtypes.missing import isna, isnull, notnull # noqa
3034

@@ -367,12 +371,12 @@ def standardize_mapping(into):
367371
Series.to_dict
368372
"""
369373
if not inspect.isclass(into):
370-
if isinstance(into, collections.defaultdict):
371-
return partial(collections.defaultdict, into.default_factory)
374+
if isinstance(into, defaultdict):
375+
return partial(defaultdict, into.default_factory)
372376
into = type(into)
373377
if not issubclass(into, abc.Mapping):
374378
raise TypeError(f"unsupported type: {into}")
375-
elif into == collections.defaultdict:
379+
elif into == defaultdict:
376380
raise TypeError("to_dict() only accepts initialized defaultdicts")
377381
return into
378382

@@ -473,3 +477,18 @@ def f(x):
473477
f = mapper
474478

475479
return f
480+
481+
482+
def convert_to_list_like(
483+
values: Union[Scalar, Iterable, AnyArrayLike]
484+
) -> Union[List, AnyArrayLike]:
485+
"""
486+
Convert list-like or scalar input to list-like. List, numpy and pandas array-like
487+
inputs are returned unmodified whereas others are converted to list.
488+
"""
489+
if isinstance(values, (list, np.ndarray, ABCIndex, ABCSeries, ABCExtensionArray)):
490+
return values
491+
elif isinstance(values, abc.Iterable) and not isinstance(values, str):
492+
return list(values)
493+
494+
return [values]

0 commit comments

Comments
 (0)