-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
/
Copy pathtest_datetime.py
239 lines (178 loc) · 7.1 KB
/
test_datetime.py
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import numpy as np
import pytest
from pandas.core.dtypes.dtypes import DatetimeTZDtype
import pandas as pd
from pandas.core.arrays import DatetimeArray
from pandas.tests.extension import base
@pytest.fixture(params=["US/Central"])
def dtype(request):
return DatetimeTZDtype(unit="ns", tz=request.param)
@pytest.fixture
def data(dtype):
data = DatetimeArray(pd.date_range("2000", periods=100, tz=dtype.tz), dtype=dtype)
return data
@pytest.fixture
def data_missing(dtype):
return DatetimeArray(
np.array(["NaT", "2000-01-01"], dtype="datetime64[ns]"), dtype=dtype
)
@pytest.fixture
def data_for_sorting(dtype):
a = pd.Timestamp("2000-01-01")
b = pd.Timestamp("2000-01-02")
c = pd.Timestamp("2000-01-03")
return DatetimeArray(np.array([b, c, a], dtype="datetime64[ns]"), dtype=dtype)
@pytest.fixture
def data_missing_for_sorting(dtype):
a = pd.Timestamp("2000-01-01")
b = pd.Timestamp("2000-01-02")
return DatetimeArray(np.array([b, "NaT", a], dtype="datetime64[ns]"), dtype=dtype)
@pytest.fixture
def data_for_grouping(dtype):
"""
Expected to be like [B, B, NA, NA, A, A, B, C]
Where A < B < C and NA is missing
"""
a = pd.Timestamp("2000-01-01")
b = pd.Timestamp("2000-01-02")
c = pd.Timestamp("2000-01-03")
na = "NaT"
return DatetimeArray(
np.array([b, b, na, na, a, a, b, c], dtype="datetime64[ns]"), dtype=dtype
)
@pytest.fixture
def na_cmp():
def cmp(a, b):
return a is pd.NaT and a is b
return cmp
@pytest.fixture
def na_value():
return pd.NaT
# ----------------------------------------------------------------------------
class BaseDatetimeTests:
pass
# ----------------------------------------------------------------------------
# Tests
class TestDatetimeDtype(BaseDatetimeTests, base.BaseDtypeTests):
pass
class TestConstructors(BaseDatetimeTests, base.BaseConstructorsTests):
pass
class TestGetitem(BaseDatetimeTests, base.BaseGetitemTests):
pass
class TestMethods(BaseDatetimeTests, base.BaseMethodsTests):
@pytest.mark.skip(reason="Incorrect expected")
def test_value_counts(self, all_data, dropna):
pass
def test_combine_add(self, data_repeated):
# Timestamp.__add__(Timestamp) not defined
pass
class TestInterface(BaseDatetimeTests, base.BaseInterfaceTests):
def test_array_interface(self, data):
if data.tz:
# np.asarray(DTA) is currently always tz-naive.
pytest.skip("GH-23569")
else:
super().test_array_interface(data)
class TestArithmeticOps(BaseDatetimeTests, base.BaseArithmeticOpsTests):
implements = {"__sub__", "__rsub__"}
def test_arith_frame_with_scalar(self, data, all_arithmetic_operators):
# frame & scalar
if all_arithmetic_operators in self.implements:
df = pd.DataFrame({"A": data})
self.check_opname(df, all_arithmetic_operators, data[0], exc=None)
else:
# ... but not the rest.
super().test_arith_frame_with_scalar(data, all_arithmetic_operators)
def test_arith_series_with_scalar(self, data, all_arithmetic_operators):
if all_arithmetic_operators in self.implements:
s = pd.Series(data)
self.check_opname(s, all_arithmetic_operators, s.iloc[0], exc=None)
else:
# ... but not the rest.
super().test_arith_series_with_scalar(data, all_arithmetic_operators)
def test_add_series_with_extension_array(self, data):
# Datetime + Datetime not implemented
s = pd.Series(data)
msg = "cannot add DatetimeArray and DatetimeArray"
with pytest.raises(TypeError, match=msg):
s + data
def test_arith_series_with_array(self, data, all_arithmetic_operators):
if all_arithmetic_operators in self.implements:
s = pd.Series(data)
self.check_opname(s, all_arithmetic_operators, s.iloc[0], exc=None)
else:
# ... but not the rest.
super().test_arith_series_with_scalar(data, all_arithmetic_operators)
def test_error(self, data, all_arithmetic_operators):
pass
def test_divmod_series_array(self):
# GH 23287
# skipping because it is not implemented
pass
class TestCasting(BaseDatetimeTests, base.BaseCastingTests):
pass
class TestComparisonOps(BaseDatetimeTests, base.BaseComparisonOpsTests):
def _compare_other(self, s, data, op_name, other):
# the base test is not appropriate for us. We raise on comparison
# with (some) integers, depending on the value.
pass
def test_compare_with_Categorical(self):
result = pd.date_range("2020", periods=3)
expected = pd.Categorical(result)
assert all(result == expected)
result = pd.date_range("2020", periods=3, tz="UTC")
expected = pd.Categorical(result)
assert all(result == expected)
result = pd.timedelta_range("0 days", periods=3)
expected = pd.Categorical(result)
assert all(result == expected)
result = pd.period_range("2020Q1", periods=3, freq="Q")
expected = pd.Categorical(result)
assert all(result == expected)
class TestMissing(BaseDatetimeTests, base.BaseMissingTests):
pass
class TestReshaping(BaseDatetimeTests, base.BaseReshapingTests):
@pytest.mark.skip(reason="We have DatetimeTZBlock")
def test_concat(self, data, in_frame):
pass
def test_concat_mixed_dtypes(self, data):
# concat(Series[datetimetz], Series[category]) uses a
# plain np.array(values) on the DatetimeArray, which
# drops the tz.
super().test_concat_mixed_dtypes(data)
@pytest.mark.parametrize("obj", ["series", "frame"])
def test_unstack(self, obj):
# GH-13287: can't use base test, since building the expected fails.
dtype = DatetimeTZDtype(tz="US/Central")
data = DatetimeArray._from_sequence(
["2000", "2001", "2002", "2003"],
dtype=dtype,
)
index = pd.MultiIndex.from_product(([["A", "B"], ["a", "b"]]), names=["a", "b"])
if obj == "series":
ser = pd.Series(data, index=index)
expected = pd.DataFrame(
{"A": data.take([0, 1]), "B": data.take([2, 3])},
index=pd.Index(["a", "b"], name="b"),
)
expected.columns.name = "a"
else:
ser = pd.DataFrame({"A": data, "B": data}, index=index)
expected = pd.DataFrame(
{
("A", "A"): data.take([0, 1]),
("A", "B"): data.take([2, 3]),
("B", "A"): data.take([0, 1]),
("B", "B"): data.take([2, 3]),
},
index=pd.Index(["a", "b"], name="b"),
)
expected.columns.names = [None, "a"]
result = ser.unstack(0)
self.assert_equal(result, expected)
class TestSetitem(BaseDatetimeTests, base.BaseSetitemTests):
pass
class TestGroupby(BaseDatetimeTests, base.BaseGroupbyTests):
pass
class TestPrinting(BaseDatetimeTests, base.BasePrintingTests):
pass