zmc
2023-10-12 ed135d79df12a2466b52dae1a82326941211dcc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from datetime import (
    date,
    datetime,
    timedelta,
)
 
import numpy as np
import pytest
 
from pandas.core.dtypes.cast import (
    infer_dtype_from,
    infer_dtype_from_array,
    infer_dtype_from_scalar,
)
from pandas.core.dtypes.common import is_dtype_equal
 
from pandas import (
    Categorical,
    Interval,
    Period,
    Series,
    Timedelta,
    Timestamp,
    date_range,
)
 
 
@pytest.fixture(params=[True, False])
def pandas_dtype(request):
    return request.param
 
 
def test_infer_dtype_from_int_scalar(any_int_numpy_dtype):
    # Test that infer_dtype_from_scalar is
    # returning correct dtype for int and float.
    data = np.dtype(any_int_numpy_dtype).type(12)
    dtype, val = infer_dtype_from_scalar(data)
    assert dtype == type(data)
 
 
def test_infer_dtype_from_float_scalar(float_numpy_dtype):
    float_numpy_dtype = np.dtype(float_numpy_dtype).type
    data = float_numpy_dtype(12)
 
    dtype, val = infer_dtype_from_scalar(data)
    assert dtype == float_numpy_dtype
 
 
@pytest.mark.parametrize(
    "data,exp_dtype", [(12, np.int64), (np.float_(12), np.float64)]
)
def test_infer_dtype_from_python_scalar(data, exp_dtype):
    dtype, val = infer_dtype_from_scalar(data)
    assert dtype == exp_dtype
 
 
@pytest.mark.parametrize("bool_val", [True, False])
def test_infer_dtype_from_boolean(bool_val):
    dtype, val = infer_dtype_from_scalar(bool_val)
    assert dtype == np.bool_
 
 
def test_infer_dtype_from_complex(complex_dtype):
    data = np.dtype(complex_dtype).type(1)
    dtype, val = infer_dtype_from_scalar(data)
    assert dtype == np.complex_
 
 
@pytest.mark.parametrize(
    "data", [np.datetime64(1, "ns"), Timestamp(1), datetime(2000, 1, 1, 0, 0)]
)
def test_infer_dtype_from_datetime(data):
    dtype, val = infer_dtype_from_scalar(data)
    assert dtype == "M8[ns]"
 
 
@pytest.mark.parametrize("data", [np.timedelta64(1, "ns"), Timedelta(1), timedelta(1)])
def test_infer_dtype_from_timedelta(data):
    dtype, val = infer_dtype_from_scalar(data)
    assert dtype == "m8[ns]"
 
 
@pytest.mark.parametrize("freq", ["M", "D"])
def test_infer_dtype_from_period(freq, pandas_dtype):
    p = Period("2011-01-01", freq=freq)
    dtype, val = infer_dtype_from_scalar(p, pandas_dtype=pandas_dtype)
 
    if pandas_dtype:
        exp_dtype = f"period[{freq}]"
    else:
        exp_dtype = np.object_
 
    assert dtype == exp_dtype
    assert val == p
 
 
@pytest.mark.parametrize(
    "data", [date(2000, 1, 1), "foo", Timestamp(1, tz="US/Eastern")]
)
def test_infer_dtype_misc(data):
    dtype, val = infer_dtype_from_scalar(data)
    assert dtype == np.object_
 
 
@pytest.mark.parametrize("tz", ["UTC", "US/Eastern", "Asia/Tokyo"])
def test_infer_from_scalar_tz(tz, pandas_dtype):
    dt = Timestamp(1, tz=tz)
    dtype, val = infer_dtype_from_scalar(dt, pandas_dtype=pandas_dtype)
 
    if pandas_dtype:
        exp_dtype = f"datetime64[ns, {tz}]"
    else:
        exp_dtype = np.object_
 
    assert dtype == exp_dtype
    assert val == dt
 
 
@pytest.mark.parametrize(
    "left, right, subtype",
    [
        (0, 1, "int64"),
        (0.0, 1.0, "float64"),
        (Timestamp(0), Timestamp(1), "datetime64[ns]"),
        (Timestamp(0, tz="UTC"), Timestamp(1, tz="UTC"), "datetime64[ns, UTC]"),
        (Timedelta(0), Timedelta(1), "timedelta64[ns]"),
    ],
)
def test_infer_from_interval(left, right, subtype, closed, pandas_dtype):
    # GH 30337
    interval = Interval(left, right, closed)
    result_dtype, result_value = infer_dtype_from_scalar(interval, pandas_dtype)
    expected_dtype = f"interval[{subtype}, {closed}]" if pandas_dtype else np.object_
    assert result_dtype == expected_dtype
    assert result_value == interval
 
 
def test_infer_dtype_from_scalar_errors():
    msg = "invalid ndarray passed to infer_dtype_from_scalar"
 
    with pytest.raises(ValueError, match=msg):
        infer_dtype_from_scalar(np.array([1]))
 
 
@pytest.mark.parametrize(
    "value, expected, pandas_dtype",
    [
        ("foo", np.object_, False),
        (b"foo", np.object_, False),
        (1, np.int64, False),
        (1.5, np.float_, False),
        (np.datetime64("2016-01-01"), np.dtype("M8[ns]"), False),
        (Timestamp("20160101"), np.dtype("M8[ns]"), False),
        (Timestamp("20160101", tz="UTC"), np.object_, False),
        (Timestamp("20160101", tz="UTC"), "datetime64[ns, UTC]", True),
    ],
)
def test_infer_dtype_from_scalar(value, expected, pandas_dtype):
    dtype, _ = infer_dtype_from_scalar(value, pandas_dtype=pandas_dtype)
    assert is_dtype_equal(dtype, expected)
 
    with pytest.raises(TypeError, match="must be list-like"):
        infer_dtype_from_array(value, pandas_dtype=pandas_dtype)
 
 
@pytest.mark.parametrize(
    "arr, expected, pandas_dtype",
    [
        ([1], np.int_, False),
        (np.array([1], dtype=np.int64), np.int64, False),
        ([np.nan, 1, ""], np.object_, False),
        (np.array([[1.0, 2.0]]), np.float_, False),
        (Categorical(list("aabc")), np.object_, False),
        (Categorical([1, 2, 3]), np.int64, False),
        (Categorical(list("aabc")), "category", True),
        (Categorical([1, 2, 3]), "category", True),
        (date_range("20160101", periods=3), np.dtype("=M8[ns]"), False),
        (
            date_range("20160101", periods=3, tz="US/Eastern"),
            "datetime64[ns, US/Eastern]",
            True,
        ),
        (Series([1.0, 2, 3]), np.float64, False),
        (Series(list("abc")), np.object_, False),
        (
            Series(date_range("20160101", periods=3, tz="US/Eastern")),
            "datetime64[ns, US/Eastern]",
            True,
        ),
    ],
)
def test_infer_dtype_from_array(arr, expected, pandas_dtype):
    dtype, _ = infer_dtype_from_array(arr, pandas_dtype=pandas_dtype)
    assert is_dtype_equal(dtype, expected)
 
 
@pytest.mark.parametrize("cls", [np.datetime64, np.timedelta64])
def test_infer_dtype_from_scalar_zerodim_datetimelike(cls):
    # ndarray.item() can incorrectly return int instead of td64/dt64
    val = cls(1234, "ns")
    arr = np.array(val)
 
    dtype, res = infer_dtype_from_scalar(arr)
    assert dtype.type is cls
    assert isinstance(res, cls)
 
    dtype, res = infer_dtype_from(arr)
    assert dtype.type is cls