Skip to content

Commit

Permalink
TST: test_cast_1d_array
Browse files Browse the repository at this point in the history
  • Loading branch information
toobaz committed Dec 15, 2017
1 parent 90d37f6 commit 61ebefb
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion pandas/tests/dtypes/test_cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
infer_dtype_from_array,
maybe_convert_string_to_object,
maybe_convert_scalar,
find_common_type)
find_common_type,
construct_1d_array_from_listlike)
from pandas.core.dtypes.dtypes import (
CategoricalDtype,
DatetimeTZDtype,
Expand Down Expand Up @@ -407,3 +408,24 @@ def test_period_dtype(self):
np.dtype('datetime64[ns]'), np.object, np.int64]:
assert find_common_type([dtype, dtype2]) == np.object
assert find_common_type([dtype2, dtype]) == np.object

@pytest.mark.parametrize('dtype', [int, float, str, object, None])
@pytest.mark.parametrize('datum1', [1, 2., "3", (4, 5), [6, 7], None])
@pytest.mark.parametrize('datum2', [8, 9., "10", (11, 12), [13, 14], None])
def test_cast_1d_array(self, dtype, datum1, datum2):
data = [datum1, datum2]
try:
# Conversion to 1d array is possible if requested dtype is object
possible = dtype is object
# ... or the following succeeds _and_ the result has dimension 1:
possible = possible or np.array(data, dtype=dtype).ndim == 1
if not possible:
exc = ValueError
except (ValueError, TypeError) as exception:
exc = type(exception)

if possible:
assert list(construct_1d_array_from_listlike(data)) == data
else:
pytest.raises(exc, construct_1d_array_from_listlike,
data, dtype=dtype)

0 comments on commit 61ebefb

Please sign in to comment.