Skip to content

Commit af82b2a

Browse files
gfyoungjreback
authored andcommitted
ERR/TST: Add pytest idiom to dtypes/test_cast.py (#24847)
1 parent 518b237 commit af82b2a

11 files changed

+452
-396
lines changed

pandas/core/dtypes/cast.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ def infer_dtype_from_scalar(val, pandas_dtype=False):
354354

355355
# a 1-element ndarray
356356
if isinstance(val, np.ndarray):
357-
msg = "invalid ndarray passed to _infer_dtype_from_scalar"
357+
msg = "invalid ndarray passed to infer_dtype_from_scalar"
358358
if val.ndim != 0:
359359
raise ValueError(msg)
360360

pandas/tests/dtypes/cast/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from pandas.core.dtypes.cast import construct_1d_arraylike_from_scalar
4+
from pandas.core.dtypes.dtypes import CategoricalDtype
5+
6+
from pandas import Categorical
7+
from pandas.util import testing as tm
8+
9+
10+
def test_cast_1d_array_like_from_scalar_categorical():
11+
# see gh-19565
12+
#
13+
# Categorical result from scalar did not maintain
14+
# categories and ordering of the passed dtype.
15+
cats = ["a", "b", "c"]
16+
cat_type = CategoricalDtype(categories=cats, ordered=False)
17+
expected = Categorical(["a", "a"], categories=cats)
18+
19+
result = construct_1d_arraylike_from_scalar("a", len(expected), cat_type)
20+
tm.assert_categorical_equal(result, expected,
21+
check_category_order=True,
22+
check_dtype=True)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import numpy as np
4+
import pytest
5+
6+
from pandas.core.dtypes.cast import construct_1d_ndarray_preserving_na
7+
8+
from pandas.util import testing as tm
9+
10+
11+
@pytest.mark.parametrize('values, dtype, expected', [
12+
([1, 2, 3], None, np.array([1, 2, 3])),
13+
(np.array([1, 2, 3]), None, np.array([1, 2, 3])),
14+
(['1', '2', None], None, np.array(['1', '2', None])),
15+
(['1', '2', None], np.dtype('str'), np.array(['1', '2', None])),
16+
([1, 2, None], np.dtype('str'), np.array(['1', '2', None])),
17+
])
18+
def test_construct_1d_ndarray_preserving_na(values, dtype, expected):
19+
result = construct_1d_ndarray_preserving_na(values, dtype=dtype)
20+
tm.assert_numpy_array_equal(result, expected)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import pytest
4+
5+
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
6+
7+
8+
@pytest.mark.parametrize("datum1", [1, 2., "3", (4, 5), [6, 7], None])
9+
@pytest.mark.parametrize("datum2", [8, 9., "10", (11, 12), [13, 14], None])
10+
def test_cast_1d_array(datum1, datum2):
11+
data = [datum1, datum2]
12+
result = construct_1d_object_array_from_listlike(data)
13+
14+
# Direct comparison fails: https://github.com/numpy/numpy/issues/10218
15+
assert result.dtype == "object"
16+
assert list(result) == data
17+
18+
19+
@pytest.mark.parametrize("val", [1, 2., None])
20+
def test_cast_1d_array_invalid_scalar(val):
21+
with pytest.raises(TypeError, match="has no len()"):
22+
construct_1d_object_array_from_listlike(val)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import numpy as np
4+
import pytest
5+
6+
from pandas.core.dtypes.cast import maybe_convert_objects
7+
8+
9+
@pytest.mark.parametrize("data", [[1, 2], ["apply", "banana"]])
10+
@pytest.mark.parametrize("copy", [True, False])
11+
def test_maybe_convert_objects_copy(data, copy):
12+
arr = np.array(data)
13+
out = maybe_convert_objects(arr, copy=copy)
14+
15+
assert (arr is out) is (not copy)
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import numpy as np
4+
import pytest
5+
6+
from pandas.core.dtypes.cast import maybe_downcast_to_dtype
7+
8+
from pandas import DatetimeIndex, Series, Timestamp
9+
from pandas.util import testing as tm
10+
11+
12+
@pytest.mark.parametrize("arr,dtype,expected", [
13+
(np.array([8.5, 8.6, 8.7, 8.8, 8.9999999999995]), "infer",
14+
np.array([8.5, 8.6, 8.7, 8.8, 8.9999999999995])),
15+
16+
(np.array([8., 8., 8., 8., 8.9999999999995]), "infer",
17+
np.array([8, 8, 8, 8, 9], dtype=np.int64)),
18+
19+
(np.array([8., 8., 8., 8., 9.0000000000005]), "infer",
20+
np.array([8, 8, 8, 8, 9], dtype=np.int64)),
21+
])
22+
def test_downcast(arr, expected, dtype):
23+
result = maybe_downcast_to_dtype(arr, dtype)
24+
tm.assert_numpy_array_equal(result, expected)
25+
26+
27+
def test_downcast_booleans():
28+
# see gh-16875: coercing of booleans.
29+
ser = Series([True, True, False])
30+
result = maybe_downcast_to_dtype(ser, np.dtype(np.float64))
31+
32+
expected = ser
33+
tm.assert_series_equal(result, expected)
34+
35+
36+
def test_downcast_conversion_no_nan(any_real_dtype):
37+
dtype = any_real_dtype
38+
expected = np.array([1, 2])
39+
arr = np.array([1.0, 2.0], dtype=dtype)
40+
41+
result = maybe_downcast_to_dtype(arr, "infer")
42+
tm.assert_almost_equal(result, expected, check_dtype=False)
43+
44+
45+
def test_downcast_conversion_nan(float_dtype):
46+
dtype = float_dtype
47+
data = [1.0, 2.0, np.nan]
48+
49+
expected = np.array(data, dtype=dtype)
50+
arr = np.array(data, dtype=dtype)
51+
52+
result = maybe_downcast_to_dtype(arr, "infer")
53+
tm.assert_almost_equal(result, expected)
54+
55+
56+
def test_downcast_conversion_empty(any_real_dtype):
57+
dtype = any_real_dtype
58+
arr = np.array([], dtype=dtype)
59+
result = maybe_downcast_to_dtype(arr, "int64")
60+
tm.assert_numpy_array_equal(result, np.array([], dtype=np.int64))
61+
62+
63+
@pytest.mark.parametrize("klass", [np.datetime64, np.timedelta64])
64+
def test_datetime_likes_nan(klass):
65+
dtype = klass.__name__ + "[ns]"
66+
arr = np.array([1, 2, np.nan])
67+
68+
exp = np.array([1, 2, klass("NaT")], dtype)
69+
res = maybe_downcast_to_dtype(arr, dtype)
70+
tm.assert_numpy_array_equal(res, exp)
71+
72+
73+
@pytest.mark.parametrize("as_asi", [True, False])
74+
def test_datetime_with_timezone(as_asi):
75+
# see gh-15426
76+
ts = Timestamp("2016-01-01 12:00:00", tz="US/Pacific")
77+
exp = DatetimeIndex([ts, ts])
78+
79+
obj = exp.asi8 if as_asi else exp
80+
res = maybe_downcast_to_dtype(obj, exp.dtype)
81+
82+
tm.assert_index_equal(res, exp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import numpy as np
4+
import pytest
5+
6+
from pandas.core.dtypes.cast import find_common_type
7+
from pandas.core.dtypes.dtypes import (
8+
CategoricalDtype, DatetimeTZDtype, PeriodDtype)
9+
10+
11+
@pytest.mark.parametrize("source_dtypes,expected_common_dtype", [
12+
((np.int64,), np.int64),
13+
((np.uint64,), np.uint64),
14+
((np.float32,), np.float32),
15+
((np.object,), np.object),
16+
17+
# Into ints.
18+
((np.int16, np.int64), np.int64),
19+
((np.int32, np.uint32), np.int64),
20+
((np.uint16, np.uint64), np.uint64),
21+
22+
# Into floats.
23+
((np.float16, np.float32), np.float32),
24+
((np.float16, np.int16), np.float32),
25+
((np.float32, np.int16), np.float32),
26+
((np.uint64, np.int64), np.float64),
27+
((np.int16, np.float64), np.float64),
28+
((np.float16, np.int64), np.float64),
29+
30+
# Into others.
31+
((np.complex128, np.int32), np.complex128),
32+
((np.object, np.float32), np.object),
33+
((np.object, np.int16), np.object),
34+
35+
# Bool with int.
36+
((np.dtype("bool"), np.int64), np.object),
37+
((np.dtype("bool"), np.int32), np.object),
38+
((np.dtype("bool"), np.int16), np.object),
39+
((np.dtype("bool"), np.int8), np.object),
40+
((np.dtype("bool"), np.uint64), np.object),
41+
((np.dtype("bool"), np.uint32), np.object),
42+
((np.dtype("bool"), np.uint16), np.object),
43+
((np.dtype("bool"), np.uint8), np.object),
44+
45+
# Bool with float.
46+
((np.dtype("bool"), np.float64), np.object),
47+
((np.dtype("bool"), np.float32), np.object),
48+
49+
((np.dtype("datetime64[ns]"), np.dtype("datetime64[ns]")),
50+
np.dtype("datetime64[ns]")),
51+
((np.dtype("timedelta64[ns]"), np.dtype("timedelta64[ns]")),
52+
np.dtype("timedelta64[ns]")),
53+
54+
((np.dtype("datetime64[ns]"), np.dtype("datetime64[ms]")),
55+
np.dtype("datetime64[ns]")),
56+
((np.dtype("timedelta64[ms]"), np.dtype("timedelta64[ns]")),
57+
np.dtype("timedelta64[ns]")),
58+
59+
((np.dtype("datetime64[ns]"), np.dtype("timedelta64[ns]")), np.object),
60+
((np.dtype("datetime64[ns]"), np.int64), np.object)
61+
])
62+
def test_numpy_dtypes(source_dtypes, expected_common_dtype):
63+
assert find_common_type(source_dtypes) == expected_common_dtype
64+
65+
66+
def test_raises_empty_input():
67+
with pytest.raises(ValueError, match="no types given"):
68+
find_common_type([])
69+
70+
71+
@pytest.mark.parametrize("dtypes,exp_type", [
72+
([CategoricalDtype()], "category"),
73+
([np.object, CategoricalDtype()], np.object),
74+
([CategoricalDtype(), CategoricalDtype()], "category"),
75+
])
76+
def test_categorical_dtype(dtypes, exp_type):
77+
assert find_common_type(dtypes) == exp_type
78+
79+
80+
def test_datetimetz_dtype_match():
81+
dtype = DatetimeTZDtype(unit="ns", tz="US/Eastern")
82+
assert find_common_type([dtype, dtype]) == "datetime64[ns, US/Eastern]"
83+
84+
85+
@pytest.mark.parametrize("dtype2", [
86+
DatetimeTZDtype(unit="ns", tz="Asia/Tokyo"),
87+
np.dtype("datetime64[ns]"), np.object, np.int64
88+
])
89+
def test_datetimetz_dtype_mismatch(dtype2):
90+
dtype = DatetimeTZDtype(unit="ns", tz="US/Eastern")
91+
assert find_common_type([dtype, dtype2]) == np.object
92+
assert find_common_type([dtype2, dtype]) == np.object
93+
94+
95+
def test_period_dtype_match():
96+
dtype = PeriodDtype(freq="D")
97+
assert find_common_type([dtype, dtype]) == "period[D]"
98+
99+
100+
@pytest.mark.parametrize("dtype2", [
101+
DatetimeTZDtype(unit="ns", tz="Asia/Tokyo"),
102+
PeriodDtype(freq="2D"), PeriodDtype(freq="H"),
103+
np.dtype("datetime64[ns]"), np.object, np.int64
104+
])
105+
def test_period_dtype_mismatch(dtype2):
106+
dtype = PeriodDtype(freq="D")
107+
assert find_common_type([dtype, dtype2]) == np.object
108+
assert find_common_type([dtype2, dtype]) == np.object
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import numpy as np
4+
import pytest
5+
6+
from pandas import DataFrame, NaT, Series, Timestamp
7+
8+
9+
@pytest.mark.parametrize("data,exp_size", [
10+
# see gh-16362.
11+
([[NaT, "a", "b", 0], [NaT, "b", "c", 1]], 8),
12+
([[NaT, "a", 0], [NaT, "b", 1]], 6)
13+
])
14+
def test_maybe_infer_to_datetimelike_df_construct(data, exp_size):
15+
result = DataFrame(np.array(data))
16+
assert result.size == exp_size
17+
18+
19+
def test_maybe_infer_to_datetimelike_ser_construct():
20+
# see gh-19671.
21+
result = Series(["M1701", Timestamp("20130101")])
22+
assert result.dtype.kind == "O"

0 commit comments

Comments
 (0)