Skip to content

Commit 8848344

Browse files
committed
REF: only support object dtype
1 parent a918988 commit 8848344

File tree

8 files changed

+33
-43
lines changed

8 files changed

+33
-43
lines changed

pandas/_libs/src/inference.pyx

+3-2
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,9 @@ def infer_dtype(object value, bint skipna=False):
349349
else:
350350
if not isinstance(value, list):
351351
value = list(value)
352-
from pandas.core.dtypes.cast import construct_1d_array_from_listlike
353-
values = construct_1d_array_from_listlike(value)
352+
from pandas.core.dtypes.cast import (
353+
construct_1d_object_array_from_listlike)
354+
values = construct_1d_object_array_from_listlike(value)
354355

355356
values = getattr(values, 'values', values)
356357
val = _try_infer_map(values)

pandas/core/algorithms.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88

99
from pandas.core.dtypes.cast import (
10-
maybe_promote, construct_1d_array_from_listlike)
10+
maybe_promote, construct_1d_object_array_from_listlike)
1111
from pandas.core.dtypes.generic import (
1212
ABCSeries, ABCIndex,
1313
ABCIndexClass, ABCCategorical)
@@ -172,7 +172,7 @@ def _ensure_arraylike(values):
172172
if inferred in ['mixed', 'string', 'unicode']:
173173
if isinstance(values, tuple):
174174
values = list(values)
175-
values = construct_1d_array_from_listlike(values)
175+
values = construct_1d_object_array_from_listlike(values)
176176
else:
177177
values = np.asarray(values)
178178
return values
@@ -402,7 +402,7 @@ def isin(comps, values):
402402
.format(values_type=type(values).__name__))
403403

404404
if not isinstance(values, (ABCIndex, ABCSeries, np.ndarray)):
405-
values = construct_1d_array_from_listlike(list(values))
405+
values = construct_1d_object_array_from_listlike(list(values))
406406

407407
comps, dtype, _ = _ensure_data(comps)
408408
values, _, _ = _ensure_data(values, dtype=dtype)

pandas/core/common.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +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 construct_1d_array_from_listlike
24+
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
2525

2626
# compat
2727
from pandas.errors import ( # noqa
@@ -382,7 +382,7 @@ def _asarray_tuplesafe(values, dtype=None):
382382
return values.values
383383

384384
if isinstance(values, list) and dtype in [np.object_, object]:
385-
return construct_1d_array_from_listlike(values)
385+
return construct_1d_object_array_from_listlike(values)
386386

387387
result = np.asarray(values, dtype=dtype)
388388

@@ -392,7 +392,7 @@ def _asarray_tuplesafe(values, dtype=None):
392392
if result.ndim == 2:
393393
# Avoid building an array of arrays:
394394
values = [tuple(x) for x in values]
395-
result = construct_1d_array_from_listlike(values)
395+
result = construct_1d_object_array_from_listlike(values)
396396

397397
return result
398398

pandas/core/dtypes/cast.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def maybe_convert_platform(values):
4242
""" try to do platform conversion, allow ndarray or list here """
4343

4444
if isinstance(values, (list, tuple)):
45-
values = construct_1d_array_from_listlike(list(values))
45+
values = construct_1d_object_array_from_listlike(list(values))
4646
if getattr(values, 'dtype', None) == np.object_:
4747
if hasattr(values, '_values'):
4848
values = values._values
@@ -1164,21 +1164,21 @@ def construct_1d_arraylike_from_scalar(value, length, dtype):
11641164
return subarr
11651165

11661166

1167-
def construct_1d_array_from_listlike(values, dtype='object'):
1167+
def construct_1d_object_array_from_listlike(values):
11681168
"""
1169-
Transform any list-like object in a 1-dimensional numpy array.
1169+
Transform any list-like object in a 1-dimensional numpy array of object
1170+
dtype.
11701171
11711172
Parameters
11721173
----------
11731174
values : any iterable which has a len()
1174-
dtype : dtype, default 'object'
11751175
11761176
Returns
11771177
-------
1178-
1-dimensional numpy array of dtype "dtype"
1178+
1-dimensional numpy array of dtype object
11791179
"""
11801180
# numpy will try to interpret nested lists as further dimensions, hence
11811181
# making a 1D array that contains list-likes is a bit tricky:
1182-
result = np.empty(len(values), dtype=dtype)
1182+
result = np.empty(len(values), dtype='object')
11831183
result[:] = values
11841184
return result

pandas/core/ops.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
is_scalar,
3535
_ensure_object)
3636
from pandas.core.dtypes.cast import (
37-
maybe_upcast_putmask, find_common_type, construct_1d_array_from_listlike)
37+
maybe_upcast_putmask, find_common_type,
38+
construct_1d_object_array_from_listlike)
3839
from pandas.core.dtypes.generic import (
3940
ABCSeries,
4041
ABCDataFrame,
@@ -751,7 +752,7 @@ def wrapper(left, right, name=name, na_op=na_op):
751752

752753
def _comp_method_OBJECT_ARRAY(op, x, y):
753754
if isinstance(y, list):
754-
y = construct_1d_array_from_listlike(y)
755+
y = construct_1d_object_array_from_listlike(y)
755756
if isinstance(y, (np.ndarray, ABCSeries, ABCIndex)):
756757
if not is_object_dtype(y.dtype):
757758
y = y.astype(np.object_)
@@ -902,7 +903,7 @@ def na_op(x, y):
902903
result = op(x, y)
903904
except TypeError:
904905
if isinstance(y, list):
905-
y = construct_1d_array_from_listlike(y)
906+
y = construct_1d_object_array_from_listlike(y)
906907

907908
if isinstance(y, (np.ndarray, ABCSeries)):
908909
if (is_bool_dtype(x.dtype) and is_bool_dtype(y.dtype)):

pandas/tests/dtypes/test_cast.py

+6-18
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
maybe_convert_string_to_object,
2323
maybe_convert_scalar,
2424
find_common_type,
25-
construct_1d_array_from_listlike)
25+
construct_1d_object_array_from_listlike)
2626
from pandas.core.dtypes.dtypes import (
2727
CategoricalDtype,
2828
DatetimeTZDtype,
@@ -409,23 +409,11 @@ def test_period_dtype(self):
409409
assert find_common_type([dtype, dtype2]) == np.object
410410
assert find_common_type([dtype2, dtype]) == np.object
411411

412-
@pytest.mark.parametrize('dtype', [int, float, str, object, None])
413412
@pytest.mark.parametrize('datum1', [1, 2., "3", (4, 5), [6, 7], None])
414413
@pytest.mark.parametrize('datum2', [8, 9., "10", (11, 12), [13, 14], None])
415-
def test_cast_1d_array(self, dtype, datum1, datum2):
414+
def test_cast_1d_array(self, datum1, datum2):
416415
data = [datum1, datum2]
417-
try:
418-
# Conversion to 1d array is possible if requested dtype is object
419-
possible = dtype is object
420-
# ... or the following succeeds _and_ the result has dimension 1:
421-
possible = possible or np.array(data, dtype=dtype).ndim == 1
422-
if not possible:
423-
exc = ValueError
424-
except (ValueError, TypeError) as exception:
425-
exc = type(exception)
426-
427-
if possible:
428-
assert list(construct_1d_array_from_listlike(data)) == data
429-
else:
430-
pytest.raises(exc, construct_1d_array_from_listlike,
431-
data, dtype=dtype)
416+
result = construct_1d_object_array_from_listlike(data)
417+
# Direct comparison fails: https://github.com/numpy/numpy/issues/10218
418+
assert result.dtype == 'object'
419+
assert list(result) == data

pandas/tests/frame/test_constructors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
date_range, Categorical)
2323
import pandas as pd
2424
import pandas.util.testing as tm
25-
from pandas.core.dtypes.cast import construct_1d_array_from_listlike
25+
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
2626

2727
from pandas.tests.frame.common import TestData
2828

@@ -1199,7 +1199,7 @@ def test_constructor_from_items(self):
11991199
DataFrame.from_items(row_items, orient='index')
12001200

12011201
# orient='index', but thar be tuples
1202-
arr = construct_1d_array_from_listlike(
1202+
arr = construct_1d_object_array_from_listlike(
12031203
[('bar', 'baz')] * len(self.mixed_frame))
12041204
self.mixed_frame['foo'] = arr
12051205
row_items = [(idx, list(self.mixed_frame.xs(idx)))

pandas/tests/indexes/test_multi.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from pandas.errors import PerformanceWarning, UnsortedIndexError
1919
from pandas.core.dtypes.dtypes import CategoricalDtype
2020
from pandas.core.indexes.base import InvalidIndexError
21-
from pandas.core.dtypes.cast import construct_1d_array_from_listlike
21+
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
2222
from pandas._libs.lib import Timestamp
2323

2424
import pandas.util.testing as tm
@@ -913,7 +913,7 @@ def test_from_product_invalid_input(self):
913913
def test_from_product_datetimeindex(self):
914914
dt_index = date_range('2000-01-01', periods=2)
915915
mi = pd.MultiIndex.from_product([[1, 2], dt_index])
916-
etalon = construct_1d_array_from_listlike([(1, pd.Timestamp(
916+
etalon = construct_1d_object_array_from_listlike([(1, pd.Timestamp(
917917
'2000-01-01')), (1, pd.Timestamp('2000-01-02')), (2, pd.Timestamp(
918918
'2000-01-01')), (2, pd.Timestamp('2000-01-02'))])
919919
tm.assert_numpy_array_equal(mi.values, etalon)
@@ -938,11 +938,11 @@ def test_values_boxed(self):
938938
(1, pd.Timestamp('2000-01-04')),
939939
(2, pd.Timestamp('2000-01-02')),
940940
(3, pd.Timestamp('2000-01-03'))]
941-
mi = pd.MultiIndex.from_tuples(tuples)
942-
tm.assert_numpy_array_equal(mi.values,
943-
construct_1d_array_from_listlike(tuples))
941+
result = pd.MultiIndex.from_tuples(tuples)
942+
expected = construct_1d_object_array_from_listlike(tuples)
943+
tm.assert_numpy_array_equal(result.values, expected)
944944
# Check that code branches for boxed values produce identical results
945-
tm.assert_numpy_array_equal(mi.values[:4], mi[:4].values)
945+
tm.assert_numpy_array_equal(result.values[:4], result[:4].values)
946946

947947
def test_append(self):
948948
result = self.index[:3].append(self.index[3:])

0 commit comments

Comments
 (0)