|
21 | 21 | from pandas.core.dtypes.missing import isna, isnull, notnull # noqa
|
22 | 22 | from pandas.api import types
|
23 | 23 | from pandas.core.dtypes import common
|
| 24 | +from pandas.core.dtypes.cast import cast_to_1d_array |
24 | 25 |
|
25 | 26 | # compat
|
26 | 27 | from pandas.errors import ( # noqa
|
@@ -381,25 +382,17 @@ def _asarray_tuplesafe(values, dtype=None):
|
381 | 382 | return values.values
|
382 | 383 |
|
383 | 384 | 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) |
385 | 386 |
|
386 | 387 | result = np.asarray(values, dtype=dtype)
|
387 | 388 |
|
388 | 389 | if issubclass(result.dtype.type, compat.string_types):
|
389 | 390 | result = np.asarray(values, dtype=object)
|
390 | 391 |
|
391 | 392 | 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) |
403 | 396 |
|
404 | 397 | return result
|
405 | 398 |
|
|
0 commit comments