-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_date.py
357 lines (317 loc) · 10.8 KB
/
test_date.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import operator
import numpy
import numpy.testing
import pandas
import pandas.testing
import pytest
import db_dtypes
from db_dtypes import pandas_backports
VALUE_PARSING_TEST_CASES = [
# Min/Max values for pandas.Timestamp.
("1677-09-22", datetime.date(1677, 9, 22)),
("2262-04-11", datetime.date(2262, 4, 11)),
# Typical "zero" values.
("1900-01-01", datetime.date(1900, 1, 1)),
("1970-01-01", datetime.date(1970, 1, 1)),
# Assorted values.
("1993-10-31", datetime.date(1993, 10, 31)),
(datetime.date(1993, 10, 31), datetime.date(1993, 10, 31)),
("2012-02-29", datetime.date(2012, 2, 29)),
(numpy.datetime64("2012-02-29"), datetime.date(2012, 2, 29)),
("2021-12-17", datetime.date(2021, 12, 17)),
(pandas.Timestamp("2021-12-17"), datetime.date(2021, 12, 17)),
("2038-01-19", datetime.date(2038, 1, 19)),
]
NULL_VALUE_TEST_CASES = [
None,
pandas.NaT,
float("nan"),
]
if hasattr(pandas, "NA"):
NULL_VALUE_TEST_CASES.append(pandas.NA)
def test_box_func():
input_array = db_dtypes.DateArray([])
input_datetime = datetime.datetime(2022, 3, 16)
input_np = numpy.datetime64(input_datetime)
boxed_value = input_array._box_func(input_np)
assert boxed_value.year == 2022
assert boxed_value.month == 3
assert boxed_value.day == 16
input_delta = input_datetime - datetime.datetime(1970, 1, 1)
input_nanoseconds = (
1_000 * input_delta.microseconds
+ 1_000_000_000 * input_delta.seconds
+ 1_000_000_000 * 60 * 60 * 24 * input_delta.days
)
boxed_value = input_array._box_func(input_nanoseconds)
assert boxed_value.year == 2022
assert boxed_value.month == 3
assert boxed_value.day == 16
def test_construct_from_string_with_nonstring():
with pytest.raises(TypeError):
db_dtypes.DateDtype.construct_from_string(object())
def test__cmp_method_with_scalar():
input_array = db_dtypes.DateArray([datetime.date(1900, 1, 1)])
got = input_array._cmp_method(datetime.date(1900, 1, 1), operator.eq)
assert got[0]
@pytest.mark.parametrize("value, expected", VALUE_PARSING_TEST_CASES)
def test_date_parsing(value, expected):
assert pandas.Series([value], dtype="dbdate")[0] == expected
@pytest.mark.parametrize("value", NULL_VALUE_TEST_CASES)
def test_date_parsing_null(value):
assert pandas.Series([value], dtype="dbdate")[0] is pandas.NaT
@pytest.mark.parametrize("value, expected", VALUE_PARSING_TEST_CASES)
def test_date_set_item(value, expected):
series = pandas.Series([None], dtype="dbdate")
series[0] = value
assert series[0] == expected
@pytest.mark.parametrize("value", NULL_VALUE_TEST_CASES)
def test_date_set_item_null(value):
series = pandas.Series(["1970-01-01"], dtype="dbdate")
series[0] = value
assert series[0] is pandas.NaT
def test_date_set_slice():
series = pandas.Series([None, None, None], dtype="dbdate")
series[:] = [
datetime.date(2022, 3, 21),
"2011-12-13",
numpy.datetime64("1998-09-04"),
]
assert series[0] == datetime.date(2022, 3, 21)
assert series[1] == datetime.date(2011, 12, 13)
assert series[2] == datetime.date(1998, 9, 4)
def test_date_set_slice_null():
series = pandas.Series(["1970-01-01"] * len(NULL_VALUE_TEST_CASES), dtype="dbdate")
series[:] = NULL_VALUE_TEST_CASES
for row_index in range(len(NULL_VALUE_TEST_CASES)):
assert series[row_index] is pandas.NaT
@pytest.mark.parametrize(
"value, error",
[
("thursday", "Bad date string: 'thursday'"),
("1-2-thursday", "Bad date string: '1-2-thursday'"),
("1-2-3-4", "Bad date string: '1-2-3-4'"),
("1-2-3.f", "Bad date string: '1-2-3.f'"),
("1-d-3", "Bad date string: '1-d-3'"),
("1-3", "Bad date string: '1-3'"),
("1", "Bad date string: '1'"),
("", "Bad date string: ''"),
("2021-2-99", "day is out of range for month"),
("2021-99-1", "month must be in 1[.][.]12"),
("10000-1-1", "year 10000 is out of range"),
# Outside of min/max values pandas.Timestamp.
("0001-01-01", "Out of bounds"),
("9999-12-31", "Out of bounds"),
("1677-09-21", "Out of bounds"),
("2262-04-12", "Out of bounds"),
],
)
def test_date_parsing_errors(value, error):
with pytest.raises(ValueError, match=error):
pandas.Series([value], dtype="dbdate")
def test_date_max_2d():
input_array = db_dtypes.DateArray(
numpy.array(
[
[
numpy.datetime64("1970-01-01"),
numpy.datetime64("1980-02-02"),
numpy.datetime64("1990-03-03"),
],
[
numpy.datetime64("1971-02-02"),
numpy.datetime64("1981-03-03"),
numpy.datetime64("1991-04-04"),
],
[
numpy.datetime64("1972-03-03"),
numpy.datetime64("1982-04-04"),
numpy.datetime64("1992-05-05"),
],
],
dtype="datetime64[ns]",
)
)
numpy.testing.assert_array_equal(
input_array.max(axis=0)._ndarray,
numpy.array(
[
numpy.datetime64("1972-03-03"),
numpy.datetime64("1982-04-04"),
numpy.datetime64("1992-05-05"),
],
dtype="datetime64[ns]",
),
)
numpy.testing.assert_array_equal(
input_array.max(axis=1)._ndarray,
numpy.array(
[
numpy.datetime64("1990-03-03"),
numpy.datetime64("1991-04-04"),
numpy.datetime64("1992-05-05"),
],
dtype="datetime64[ns]",
),
)
def test_date_min_2d():
input_array = db_dtypes.DateArray(
numpy.array(
[
[
numpy.datetime64("1970-01-01"),
numpy.datetime64("1980-02-02"),
numpy.datetime64("1990-03-03"),
],
[
numpy.datetime64("1971-02-02"),
numpy.datetime64("1981-03-03"),
numpy.datetime64("1991-04-04"),
],
[
numpy.datetime64("1972-03-03"),
numpy.datetime64("1982-04-04"),
numpy.datetime64("1992-05-05"),
],
],
dtype="datetime64[ns]",
)
)
numpy.testing.assert_array_equal(
input_array.min(axis=0)._ndarray,
numpy.array(
[
numpy.datetime64("1970-01-01"),
numpy.datetime64("1980-02-02"),
numpy.datetime64("1990-03-03"),
],
dtype="datetime64[ns]",
),
)
numpy.testing.assert_array_equal(
input_array.min(axis=1)._ndarray,
numpy.array(
[
numpy.datetime64("1970-01-01"),
numpy.datetime64("1971-02-02"),
numpy.datetime64("1972-03-03"),
],
dtype="datetime64[ns]",
),
)
@pytest.mark.skipif(
not hasattr(pandas_backports, "numpy_validate_median"),
reason="median not available with this version of pandas",
)
@pytest.mark.parametrize(
"values, expected",
[
(["1970-01-01", "1900-01-01", "2000-01-01"], datetime.date(1970, 1, 1)),
(
[
None,
"1900-01-01",
pandas.NA if hasattr(pandas, "NA") else None,
pandas.NaT,
float("nan"),
],
datetime.date(1900, 1, 1),
),
(["2222-02-01", "2222-02-03"], datetime.date(2222, 2, 2)),
],
)
def test_date_median(values, expected):
series = pandas.Series(values, dtype="dbdate")
assert series.median() == expected
@pytest.mark.skipif(
not hasattr(pandas_backports, "numpy_validate_median"),
reason="median not available with this version of pandas",
)
def test_date_median_2d():
input_array = db_dtypes.DateArray(
numpy.array(
[
[
numpy.datetime64("1970-01-01"),
numpy.datetime64("1980-02-02"),
numpy.datetime64("1990-03-03"),
],
[
numpy.datetime64("1971-02-02"),
numpy.datetime64("1981-03-03"),
numpy.datetime64("1991-04-04"),
],
[
numpy.datetime64("1972-03-03"),
numpy.datetime64("1982-04-04"),
numpy.datetime64("1992-05-05"),
],
],
dtype="datetime64[ns]",
)
)
pandas.testing.assert_extension_array_equal(
input_array.median(axis=0),
db_dtypes.DateArray(
numpy.array(
[
numpy.datetime64("1971-02-02"),
numpy.datetime64("1981-03-03"),
numpy.datetime64("1991-04-04"),
],
dtype="datetime64[ns]",
)
),
)
pandas.testing.assert_extension_array_equal(
input_array.median(axis=1),
db_dtypes.DateArray(
numpy.array(
[
numpy.datetime64("1980-02-02"),
numpy.datetime64("1981-03-03"),
numpy.datetime64("1982-04-04"),
],
dtype="datetime64[ns]",
)
),
)
@pytest.mark.parametrize(
("search_term", "expected_index"),
(
(datetime.date(1899, 12, 31), 0),
(datetime.date(1900, 1, 1), 0),
(datetime.date(1920, 2, 2), 1),
(datetime.date(1930, 3, 3), 1),
(datetime.date(1950, 5, 5), 2),
(datetime.date(1990, 9, 9), 3),
(datetime.date(2012, 12, 12), 3),
(datetime.date(2022, 3, 24), 4),
),
)
def test_date_searchsorted(search_term, expected_index):
test_series = pandas.Series(
[
datetime.date(1900, 1, 1),
datetime.date(1930, 3, 3),
datetime.date(1980, 8, 8),
datetime.date(2012, 12, 12),
],
dtype="dbdate",
)
got = test_series.searchsorted(search_term)
assert got == expected_index