Skip to content

Commit 3ee6282

Browse files
committed
REF: implement and use cast_to_1d_array
1 parent b9dbdbb commit 3ee6282

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

pandas/core/common.py

+5-12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pandas.core.dtypes.missing import isna, isnull, notnull # noqa
2222
from pandas.api import types
2323
from pandas.core.dtypes import common
24+
from pandas.core.dtypes.cast import cast_to_1d_array
2425

2526
# compat
2627
from pandas.errors import ( # noqa
@@ -381,25 +382,17 @@ def _asarray_tuplesafe(values, dtype=None):
381382
return values.values
382383

383384
if isinstance(values, list) and dtype in [np.object_, object]:
384-
return lib.list_to_object_array(values)
385+
return cast_to_1d_array(values)
385386

386387
result = np.asarray(values, dtype=dtype)
387388

388389
if issubclass(result.dtype.type, compat.string_types):
389390
result = np.asarray(values, dtype=object)
390391

391392
if result.ndim == 2:
392-
if isinstance(values, list):
393-
return lib.list_to_object_array(values)
394-
else:
395-
# Making a 1D array that safely contains tuples is a bit tricky
396-
# in numpy, leading to the following
397-
try:
398-
result = np.empty(len(values), dtype=object)
399-
result[:] = values
400-
except ValueError:
401-
# we have a list-of-list
402-
result[:] = [tuple(x) for x in values]
393+
if hasattr(values, '__array__'):
394+
values = [tuple(x) for x in values]
395+
result = cast_to_1d_array(values)
403396

404397
return result
405398

pandas/core/dtypes/cast.py

+20
Original file line numberDiff line numberDiff line change
@@ -1162,3 +1162,23 @@ def construct_1d_arraylike_from_scalar(value, length, dtype):
11621162
subarr.fill(value)
11631163

11641164
return subarr
1165+
1166+
1167+
def cast_to_1d_array(values, dtype='object'):
1168+
"""
1169+
Transform any list-like object in a 1-dimensional numpy array.
1170+
1171+
Parameters
1172+
----------
1173+
values : any iterable which has a len()
1174+
dtype : dtype, default 'object'
1175+
1176+
Returns
1177+
-------
1178+
1-dimensional numpy array
1179+
"""
1180+
# numpy will try to interpret nested lists as further dimensions, hence
1181+
# making a 1D array that contains list-likes is a bit tricky:
1182+
result = np.empty(len(values), dtype=dtype)
1183+
result[:] = values
1184+
return result

pandas/core/series.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
maybe_upcast, infer_dtype_from_scalar,
3636
maybe_convert_platform,
3737
maybe_cast_to_datetime, maybe_castable,
38-
construct_1d_arraylike_from_scalar)
38+
construct_1d_arraylike_from_scalar, cast_to_1d_array)
3939
from pandas.core.dtypes.missing import isna, notna, remove_na_arraylike
4040

4141
from pandas.core.common import (is_bool_indexer,
@@ -3214,9 +3214,7 @@ def _try_cast(arr, take_fast_path):
32143214

32153215
elif isinstance(data, (list, tuple)) and len(data) > 0:
32163216
if all(is_list_like(item) for item in data):
3217-
# Ensure nested lists are not interpreted as further dimensions:
3218-
subarr = np.empty(len(data), dtype='object')
3219-
subarr[:] = data
3217+
subarr = cast_to_1d_array(data)
32203218
elif dtype is not None:
32213219
try:
32223220
subarr = _try_cast(data, False)

0 commit comments

Comments
 (0)