forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_inference.py
531 lines (437 loc) · 13.8 KB
/
test_inference.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
from datetime import (
datetime,
timedelta,
)
import numpy as np
import pytest
from pandas._libs.tslibs.ccalendar import (
DAYS,
MONTHS,
)
from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG
from pandas.compat import is_platform_windows
from pandas import (
DatetimeIndex,
Index,
RangeIndex,
Series,
Timestamp,
date_range,
period_range,
)
import pandas._testing as tm
from pandas.core.arrays import (
DatetimeArray,
TimedeltaArray,
)
from pandas.core.tools.datetimes import to_datetime
import pandas.tseries.frequencies as frequencies
import pandas.tseries.offsets as offsets
@pytest.fixture(
params=[
(timedelta(1), "D"),
(timedelta(hours=1), "H"),
(timedelta(minutes=1), "T"),
(timedelta(seconds=1), "S"),
(np.timedelta64(1, "ns"), "N"),
(timedelta(microseconds=1), "U"),
(timedelta(microseconds=1000), "L"),
]
)
def base_delta_code_pair(request):
return request.param
freqs = (
[f"Q-{month}" for month in MONTHS]
+ [f"{annual}-{month}" for annual in ["A", "BA"] for month in MONTHS]
+ ["M", "BM", "BMS"]
+ [f"WOM-{count}{day}" for count in range(1, 5) for day in DAYS]
+ [f"W-{day}" for day in DAYS]
)
@pytest.mark.parametrize("freq", freqs)
@pytest.mark.parametrize("periods", [5, 7])
def test_infer_freq_range(periods, freq):
freq = freq.upper()
gen = date_range("1/1/2000", periods=periods, freq=freq)
index = DatetimeIndex(gen.values)
if not freq.startswith("Q-"):
assert frequencies.infer_freq(index) == gen.freqstr
else:
inf_freq = frequencies.infer_freq(index)
is_dec_range = inf_freq == "Q-DEC" and gen.freqstr in (
"Q",
"Q-DEC",
"Q-SEP",
"Q-JUN",
"Q-MAR",
)
is_nov_range = inf_freq == "Q-NOV" and gen.freqstr in (
"Q-NOV",
"Q-AUG",
"Q-MAY",
"Q-FEB",
)
is_oct_range = inf_freq == "Q-OCT" and gen.freqstr in (
"Q-OCT",
"Q-JUL",
"Q-APR",
"Q-JAN",
)
assert is_dec_range or is_nov_range or is_oct_range
def test_raise_if_period_index():
index = period_range(start="1/1/1990", periods=20, freq="M")
msg = "Check the `freq` attribute instead of using infer_freq"
with pytest.raises(TypeError, match=msg):
frequencies.infer_freq(index)
def test_raise_if_too_few():
index = DatetimeIndex(["12/31/1998", "1/3/1999"])
msg = "Need at least 3 dates to infer frequency"
with pytest.raises(ValueError, match=msg):
frequencies.infer_freq(index)
def test_business_daily():
index = DatetimeIndex(["01/01/1999", "1/4/1999", "1/5/1999"])
assert frequencies.infer_freq(index) == "B"
def test_business_daily_look_alike():
# see gh-16624
#
# Do not infer "B when "weekend" (2-day gap) in wrong place.
index = DatetimeIndex(["12/31/1998", "1/3/1999", "1/4/1999"])
assert frequencies.infer_freq(index) is None
def test_day_corner():
index = DatetimeIndex(["1/1/2000", "1/2/2000", "1/3/2000"])
assert frequencies.infer_freq(index) == "D"
def test_non_datetime_index():
dates = to_datetime(["1/1/2000", "1/2/2000", "1/3/2000"])
assert frequencies.infer_freq(dates) == "D"
def test_fifth_week_of_month_infer():
# see gh-9425
#
# Only attempt to infer up to WOM-4.
index = DatetimeIndex(["2014-03-31", "2014-06-30", "2015-03-30"])
assert frequencies.infer_freq(index) is None
def test_week_of_month_fake():
# All of these dates are on same day
# of week and are 4 or 5 weeks apart.
index = DatetimeIndex(["2013-08-27", "2013-10-01", "2013-10-29", "2013-11-26"])
assert frequencies.infer_freq(index) != "WOM-4TUE"
def test_fifth_week_of_month():
# see gh-9425
#
# Only supports freq up to WOM-4.
msg = (
"Of the four parameters: start, end, periods, "
"and freq, exactly three must be specified"
)
with pytest.raises(ValueError, match=msg):
date_range("2014-01-01", freq="WOM-5MON")
def test_monthly_ambiguous():
rng = DatetimeIndex(["1/31/2000", "2/29/2000", "3/31/2000"])
assert rng.inferred_freq == "M"
def test_annual_ambiguous():
rng = DatetimeIndex(["1/31/2000", "1/31/2001", "1/31/2002"])
assert rng.inferred_freq == "A-JAN"
@pytest.mark.parametrize("count", range(1, 5))
def test_infer_freq_delta(base_delta_code_pair, count):
b = Timestamp(datetime.now())
base_delta, code = base_delta_code_pair
inc = base_delta * count
index = DatetimeIndex([b + inc * j for j in range(3)])
exp_freq = f"{count:d}{code}" if count > 1 else code
assert frequencies.infer_freq(index) == exp_freq
@pytest.mark.parametrize(
"constructor",
[
lambda now, delta: DatetimeIndex(
[now + delta * 7] + [now + delta * j for j in range(3)]
),
lambda now, delta: DatetimeIndex(
[now + delta * j for j in range(3)] + [now + delta * 7]
),
],
)
def test_infer_freq_custom(base_delta_code_pair, constructor):
b = Timestamp(datetime.now())
base_delta, _ = base_delta_code_pair
index = constructor(b, base_delta)
assert frequencies.infer_freq(index) is None
@pytest.mark.parametrize(
"freq,expected", [("Q", "Q-DEC"), ("Q-NOV", "Q-NOV"), ("Q-OCT", "Q-OCT")]
)
def test_infer_freq_index(freq, expected):
rng = period_range("1959Q2", "2009Q3", freq=freq)
rng = Index(rng.to_timestamp("D", how="e").astype(object))
assert rng.inferred_freq == expected
@pytest.mark.parametrize(
"expected,dates",
list(
{
"AS-JAN": ["2009-01-01", "2010-01-01", "2011-01-01", "2012-01-01"],
"Q-OCT": ["2009-01-31", "2009-04-30", "2009-07-31", "2009-10-31"],
"M": ["2010-11-30", "2010-12-31", "2011-01-31", "2011-02-28"],
"W-SAT": ["2010-12-25", "2011-01-01", "2011-01-08", "2011-01-15"],
"D": ["2011-01-01", "2011-01-02", "2011-01-03", "2011-01-04"],
"H": [
"2011-12-31 22:00",
"2011-12-31 23:00",
"2012-01-01 00:00",
"2012-01-01 01:00",
],
}.items()
),
)
def test_infer_freq_tz(tz_naive_fixture, expected, dates):
# see gh-7310
tz = tz_naive_fixture
idx = DatetimeIndex(dates, tz=tz)
assert idx.inferred_freq == expected
@pytest.mark.parametrize(
"date_pair",
[
["2013-11-02", "2013-11-5"], # Fall DST
["2014-03-08", "2014-03-11"], # Spring DST
["2014-01-01", "2014-01-03"], # Regular Time
],
)
@pytest.mark.parametrize(
"freq", ["H", "3H", "10T", "3601S", "3600001L", "3600000001U", "3600000000001N"]
)
def test_infer_freq_tz_transition(tz_naive_fixture, date_pair, freq):
# see gh-8772
tz = tz_naive_fixture
idx = date_range(date_pair[0], date_pair[1], freq=freq, tz=tz)
assert idx.inferred_freq == freq
def test_infer_freq_tz_transition_custom():
index = date_range("2013-11-03", periods=5, freq="3H").tz_localize(
"America/Chicago"
)
assert index.inferred_freq is None
@pytest.mark.parametrize(
"data,expected",
[
# Hourly freq in a day must result in "H"
(
[
"2014-07-01 09:00",
"2014-07-01 10:00",
"2014-07-01 11:00",
"2014-07-01 12:00",
"2014-07-01 13:00",
"2014-07-01 14:00",
],
"H",
),
(
[
"2014-07-01 09:00",
"2014-07-01 10:00",
"2014-07-01 11:00",
"2014-07-01 12:00",
"2014-07-01 13:00",
"2014-07-01 14:00",
"2014-07-01 15:00",
"2014-07-01 16:00",
"2014-07-02 09:00",
"2014-07-02 10:00",
"2014-07-02 11:00",
],
"BH",
),
(
[
"2014-07-04 09:00",
"2014-07-04 10:00",
"2014-07-04 11:00",
"2014-07-04 12:00",
"2014-07-04 13:00",
"2014-07-04 14:00",
"2014-07-04 15:00",
"2014-07-04 16:00",
"2014-07-07 09:00",
"2014-07-07 10:00",
"2014-07-07 11:00",
],
"BH",
),
(
[
"2014-07-04 09:00",
"2014-07-04 10:00",
"2014-07-04 11:00",
"2014-07-04 12:00",
"2014-07-04 13:00",
"2014-07-04 14:00",
"2014-07-04 15:00",
"2014-07-04 16:00",
"2014-07-07 09:00",
"2014-07-07 10:00",
"2014-07-07 11:00",
"2014-07-07 12:00",
"2014-07-07 13:00",
"2014-07-07 14:00",
"2014-07-07 15:00",
"2014-07-07 16:00",
"2014-07-08 09:00",
"2014-07-08 10:00",
"2014-07-08 11:00",
"2014-07-08 12:00",
"2014-07-08 13:00",
"2014-07-08 14:00",
"2014-07-08 15:00",
"2014-07-08 16:00",
],
"BH",
),
],
)
def test_infer_freq_business_hour(data, expected):
# see gh-7905
idx = DatetimeIndex(data)
assert idx.inferred_freq == expected
def test_not_monotonic():
rng = DatetimeIndex(["1/31/2000", "1/31/2001", "1/31/2002"])
rng = rng[::-1]
assert rng.inferred_freq == "-1A-JAN"
def test_non_datetime_index2():
rng = DatetimeIndex(["1/31/2000", "1/31/2001", "1/31/2002"])
vals = rng.to_pydatetime()
result = frequencies.infer_freq(vals)
assert result == rng.inferred_freq
@pytest.mark.parametrize(
"idx",
[
tm.makeIntIndex(10),
tm.makeFloatIndex(10),
tm.makePeriodIndex(10),
RangeIndex(10), # see gh-48439
],
)
def test_invalid_index_types(idx):
msg = "|".join(
[
"cannot infer freq from a non-convertible",
"Check the `freq` attribute instead of using infer_freq",
]
)
with pytest.raises(TypeError, match=msg):
frequencies.infer_freq(idx)
@pytest.mark.skipif(is_platform_windows(), reason="see gh-10822: Windows issue")
def test_invalid_index_types_unicode():
# see gh-10822
#
# Odd error message on conversions to datetime for unicode.
msg = "Unknown string format"
with pytest.raises(ValueError, match=msg):
frequencies.infer_freq(tm.makeStringIndex(10))
def test_string_datetime_like_compat():
# see gh-6463
data = ["2004-01", "2004-02", "2004-03", "2004-04"]
expected = frequencies.infer_freq(data)
result = frequencies.infer_freq(Index(data))
assert result == expected
def test_series():
# see gh-6407
s = Series(date_range("20130101", "20130110"))
inferred = frequencies.infer_freq(s)
assert inferred == "D"
@pytest.mark.parametrize("end", [10, 10.0])
def test_series_invalid_type(end):
# see gh-6407
msg = "cannot infer freq from a non-convertible dtype on a Series"
s = Series(np.arange(end))
with pytest.raises(TypeError, match=msg):
frequencies.infer_freq(s)
def test_series_inconvertible_string():
# see gh-6407
msg = "Unknown string format"
with pytest.raises(ValueError, match=msg):
frequencies.infer_freq(Series(["foo", "bar"]))
@pytest.mark.parametrize("freq", [None, "L"])
def test_series_period_index(freq):
# see gh-6407
#
# Cannot infer on PeriodIndex
msg = "cannot infer freq from a non-convertible dtype on a Series"
s = Series(period_range("2013", periods=10, freq=freq))
with pytest.raises(TypeError, match=msg):
frequencies.infer_freq(s)
@pytest.mark.parametrize("freq", ["M", "L", "S"])
def test_series_datetime_index(freq):
s = Series(date_range("20130101", periods=10, freq=freq))
inferred = frequencies.infer_freq(s)
assert inferred == freq
@pytest.mark.parametrize(
"offset_func",
[
frequencies._get_offset,
lambda freq: date_range("2011-01-01", periods=5, freq=freq),
],
)
@pytest.mark.parametrize(
"freq",
[
"WEEKDAY",
"EOM",
"W@MON",
"W@TUE",
"W@WED",
"W@THU",
"W@FRI",
"W@SAT",
"W@SUN",
"Q@JAN",
"Q@FEB",
"Q@MAR",
"A@JAN",
"A@FEB",
"A@MAR",
"A@APR",
"A@MAY",
"A@JUN",
"A@JUL",
"A@AUG",
"A@SEP",
"A@OCT",
"A@NOV",
"A@DEC",
"Y@JAN",
"WOM@1MON",
"WOM@2MON",
"WOM@3MON",
"WOM@4MON",
"WOM@1TUE",
"WOM@2TUE",
"WOM@3TUE",
"WOM@4TUE",
"WOM@1WED",
"WOM@2WED",
"WOM@3WED",
"WOM@4WED",
"WOM@1THU",
"WOM@2THU",
"WOM@3THU",
"WOM@4THU",
"WOM@1FRI",
"WOM@2FRI",
"WOM@3FRI",
"WOM@4FRI",
],
)
def test_legacy_offset_warnings(offset_func, freq):
with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG):
offset_func(freq)
def test_ms_vs_capital_ms():
left = frequencies._get_offset("ms")
right = frequencies._get_offset("MS")
assert left == offsets.Milli()
assert right == offsets.MonthBegin()
def test_infer_freq_warn_deprecated():
with tm.assert_produces_warning(FutureWarning):
frequencies.infer_freq(date_range(2022, periods=3), warn=False)
def test_infer_freq_non_nano():
arr = np.arange(10).astype(np.int64).view("M8[s]")
dta = DatetimeArray._simple_new(arr, dtype=arr.dtype)
res = frequencies.infer_freq(dta)
assert res == "S"
arr2 = arr.view("m8[ms]")
tda = TimedeltaArray._simple_new(arr2, dtype=arr2.dtype)
res2 = frequencies.infer_freq(tda)
assert res2 == "L"