Skip to content

Commit 61ebefb

Browse files
committed
TST: test_cast_1d_array
1 parent 90d37f6 commit 61ebefb

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

pandas/tests/dtypes/test_cast.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
infer_dtype_from_array,
2222
maybe_convert_string_to_object,
2323
maybe_convert_scalar,
24-
find_common_type)
24+
find_common_type,
25+
construct_1d_array_from_listlike)
2526
from pandas.core.dtypes.dtypes import (
2627
CategoricalDtype,
2728
DatetimeTZDtype,
@@ -407,3 +408,24 @@ def test_period_dtype(self):
407408
np.dtype('datetime64[ns]'), np.object, np.int64]:
408409
assert find_common_type([dtype, dtype2]) == np.object
409410
assert find_common_type([dtype2, dtype]) == np.object
411+
412+
@pytest.mark.parametrize('dtype', [int, float, str, object, None])
413+
@pytest.mark.parametrize('datum1', [1, 2., "3", (4, 5), [6, 7], None])
414+
@pytest.mark.parametrize('datum2', [8, 9., "10", (11, 12), [13, 14], None])
415+
def test_cast_1d_array(self, dtype, datum1, datum2):
416+
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)

0 commit comments

Comments
 (0)