forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_period.py
1462 lines (1168 loc) · 50.8 KB
/
test_period.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pytest
import pytz
import numpy as np
from datetime import datetime, date, timedelta
import pandas as pd
from pandas import Timedelta, NaT, Period, Timestamp, offsets
import pandas.util.testing as tm
import pandas.core.indexes.period as period
from pandas.compat import text_type, iteritems
from pandas.compat.numpy import np_datetime64_compat
from pandas._libs.tslibs import iNaT, period as libperiod
from pandas._libs.tslibs.ccalendar import DAYS, MONTHS
from pandas._libs.tslibs.parsing import DateParseError
class TestPeriodProperties(object):
"Test properties such as year, month, weekday, etc...."
@pytest.mark.parametrize('freq', ['A', 'M', 'D', 'H'])
def test_is_leap_year(self, freq):
# GH 13727
p = Period('2000-01-01 00:00:00', freq=freq)
assert p.is_leap_year
assert isinstance(p.is_leap_year, bool)
p = Period('1999-01-01 00:00:00', freq=freq)
assert not p.is_leap_year
p = Period('2004-01-01 00:00:00', freq=freq)
assert p.is_leap_year
p = Period('2100-01-01 00:00:00', freq=freq)
assert not p.is_leap_year
def test_quarterly_negative_ordinals(self):
p = Period(ordinal=-1, freq='Q-DEC')
assert p.year == 1969
assert p.quarter == 4
assert isinstance(p, Period)
p = Period(ordinal=-2, freq='Q-DEC')
assert p.year == 1969
assert p.quarter == 3
assert isinstance(p, Period)
p = Period(ordinal=-2, freq='M')
assert p.year == 1969
assert p.month == 11
assert isinstance(p, Period)
@pytest.mark.parametrize('month', MONTHS)
def test_period_cons_quarterly(self, month):
# bugs in scikits.timeseries
freq = 'Q-%s' % month
exp = Period('1989Q3', freq=freq)
assert '1989Q3' in str(exp)
stamp = exp.to_timestamp('D', how='end')
p = Period(stamp, freq=freq)
assert p == exp
stamp = exp.to_timestamp('3D', how='end')
p = Period(stamp, freq=freq)
assert p == exp
@pytest.mark.parametrize('month', MONTHS)
def test_period_cons_annual(self, month):
# bugs in scikits.timeseries
freq = 'A-%s' % month
exp = Period('1989', freq=freq)
stamp = exp.to_timestamp('D', how='end') + timedelta(days=30)
p = Period(stamp, freq=freq)
with tm.assert_produces_warning(FutureWarning):
assert p == exp + 1
assert isinstance(p, Period)
@pytest.mark.parametrize('day', DAYS)
@pytest.mark.parametrize('num', range(10, 17))
def test_period_cons_weekly(self, num, day):
daystr = '2011-02-%d' % num
freq = 'W-%s' % day
result = Period(daystr, freq=freq)
expected = Period(daystr, freq='D').asfreq(freq)
assert result == expected
assert isinstance(result, Period)
def test_period_from_ordinal(self):
p = Period('2011-01', freq='M')
res = Period._from_ordinal(p.ordinal, freq='M')
assert p == res
assert isinstance(res, Period)
def test_period_cons_nat(self):
p = Period('NaT', freq='M')
assert p is NaT
p = Period('nat', freq='W-SUN')
assert p is NaT
p = Period(iNaT, freq='D')
assert p is NaT
p = Period(iNaT, freq='3D')
assert p is NaT
p = Period(iNaT, freq='1D1H')
assert p is NaT
p = Period('NaT')
assert p is NaT
p = Period(iNaT)
assert p is NaT
def test_period_cons_mult(self):
p1 = Period('2011-01', freq='3M')
p2 = Period('2011-01', freq='M')
assert p1.ordinal == p2.ordinal
assert p1.freq == offsets.MonthEnd(3)
assert p1.freqstr == '3M'
assert p2.freq == offsets.MonthEnd()
assert p2.freqstr == 'M'
with tm.assert_produces_warning(FutureWarning):
result = p1 + 1
assert result.ordinal == (p2 + 3).ordinal
assert result.freq == p1.freq
assert result.freqstr == '3M'
with tm.assert_produces_warning(FutureWarning):
result = p1 - 1
assert result.ordinal == (p2 - 3).ordinal
assert result.freq == p1.freq
assert result.freqstr == '3M'
msg = ('Frequency must be positive, because it'
' represents span: -3M')
with pytest.raises(ValueError, match=msg):
Period('2011-01', freq='-3M')
msg = ('Frequency must be positive, because it' ' represents span: 0M')
with pytest.raises(ValueError, match=msg):
Period('2011-01', freq='0M')
def test_period_cons_combined(self):
p = [(Period('2011-01', freq='1D1H'),
Period('2011-01', freq='1H1D'),
Period('2011-01', freq='H')),
(Period(ordinal=1, freq='1D1H'),
Period(ordinal=1, freq='1H1D'),
Period(ordinal=1, freq='H'))]
for p1, p2, p3 in p:
assert p1.ordinal == p3.ordinal
assert p2.ordinal == p3.ordinal
assert p1.freq == offsets.Hour(25)
assert p1.freqstr == '25H'
assert p2.freq == offsets.Hour(25)
assert p2.freqstr == '25H'
assert p3.freq == offsets.Hour()
assert p3.freqstr == 'H'
with tm.assert_produces_warning(FutureWarning):
result = p1 + 1
assert result.ordinal == (p3 + 25).ordinal
assert result.freq == p1.freq
assert result.freqstr == '25H'
with tm.assert_produces_warning(FutureWarning):
result = p2 + 1
assert result.ordinal == (p3 + 25).ordinal
assert result.freq == p2.freq
assert result.freqstr == '25H'
with tm.assert_produces_warning(FutureWarning):
result = p1 - 1
assert result.ordinal == (p3 - 25).ordinal
assert result.freq == p1.freq
assert result.freqstr == '25H'
with tm.assert_produces_warning(FutureWarning):
result = p2 - 1
assert result.ordinal == (p3 - 25).ordinal
assert result.freq == p2.freq
assert result.freqstr == '25H'
msg = ('Frequency must be positive, because it'
' represents span: -25H')
with pytest.raises(ValueError, match=msg):
Period('2011-01', freq='-1D1H')
with pytest.raises(ValueError, match=msg):
Period('2011-01', freq='-1H1D')
with pytest.raises(ValueError, match=msg):
Period(ordinal=1, freq='-1D1H')
with pytest.raises(ValueError, match=msg):
Period(ordinal=1, freq='-1H1D')
msg = ('Frequency must be positive, because it'
' represents span: 0D')
with pytest.raises(ValueError, match=msg):
Period('2011-01', freq='0D0H')
with pytest.raises(ValueError, match=msg):
Period(ordinal=1, freq='0D0H')
# You can only combine together day and intraday offsets
msg = ('Invalid frequency: 1W1D')
with pytest.raises(ValueError, match=msg):
Period('2011-01', freq='1W1D')
msg = ('Invalid frequency: 1D1W')
with pytest.raises(ValueError, match=msg):
Period('2011-01', freq='1D1W')
@pytest.mark.parametrize('tzstr', ['Europe/Brussels',
'Asia/Tokyo', 'US/Pacific'])
def test_timestamp_tz_arg(self, tzstr):
p = Period('1/1/2005', freq='M').to_timestamp(tz=tzstr)
exp = Timestamp('1/1/2005', tz='UTC').tz_convert(tzstr)
exp_zone = pytz.timezone(tzstr).normalize(p)
assert p == exp
assert p.tz == exp_zone.tzinfo
assert p.tz == exp.tz
p = Period('1/1/2005', freq='3H').to_timestamp(tz=tzstr)
exp = Timestamp('1/1/2005', tz='UTC').tz_convert(tzstr)
exp_zone = pytz.timezone(tzstr).normalize(p)
assert p == exp
assert p.tz == exp_zone.tzinfo
assert p.tz == exp.tz
p = Period('1/1/2005', freq='A').to_timestamp(freq='A', tz=tzstr)
exp = Timestamp('31/12/2005', tz='UTC').tz_convert(tzstr)
exp_zone = pytz.timezone(tzstr).normalize(p)
assert p == exp
assert p.tz == exp_zone.tzinfo
assert p.tz == exp.tz
p = Period('1/1/2005', freq='A').to_timestamp(freq='3H', tz=tzstr)
exp = Timestamp('1/1/2005', tz='UTC').tz_convert(tzstr)
exp_zone = pytz.timezone(tzstr).normalize(p)
assert p == exp
assert p.tz == exp_zone.tzinfo
assert p.tz == exp.tz
@pytest.mark.parametrize('tzstr', ['dateutil/Europe/Brussels',
'dateutil/Asia/Tokyo',
'dateutil/US/Pacific'])
def test_timestamp_tz_arg_dateutil(self, tzstr):
from pandas._libs.tslibs.timezones import dateutil_gettz
from pandas._libs.tslibs.timezones import maybe_get_tz
tz = maybe_get_tz(tzstr)
p = Period('1/1/2005', freq='M').to_timestamp(tz=tz)
exp = Timestamp('1/1/2005', tz='UTC').tz_convert(tzstr)
assert p == exp
assert p.tz == dateutil_gettz(tzstr.split('/', 1)[1])
assert p.tz == exp.tz
p = Period('1/1/2005', freq='M').to_timestamp(freq='3H', tz=tz)
exp = Timestamp('1/1/2005', tz='UTC').tz_convert(tzstr)
assert p == exp
assert p.tz == dateutil_gettz(tzstr.split('/', 1)[1])
assert p.tz == exp.tz
def test_timestamp_tz_arg_dateutil_from_string(self):
from pandas._libs.tslibs.timezones import dateutil_gettz
p = Period('1/1/2005',
freq='M').to_timestamp(tz='dateutil/Europe/Brussels')
assert p.tz == dateutil_gettz('Europe/Brussels')
def test_timestamp_mult(self):
p = Period('2011-01', freq='M')
assert p.to_timestamp(how='S') == Timestamp('2011-01-01')
expected = Timestamp('2011-02-01') - Timedelta(1, 'ns')
assert p.to_timestamp(how='E') == expected
p = Period('2011-01', freq='3M')
assert p.to_timestamp(how='S') == Timestamp('2011-01-01')
expected = Timestamp('2011-04-01') - Timedelta(1, 'ns')
assert p.to_timestamp(how='E') == expected
def test_construction(self):
i1 = Period('1/1/2005', freq='M')
i2 = Period('Jan 2005')
assert i1 == i2
i1 = Period('2005', freq='A')
i2 = Period('2005')
i3 = Period('2005', freq='a')
assert i1 == i2
assert i1 == i3
i4 = Period('2005', freq='M')
i5 = Period('2005', freq='m')
pytest.raises(ValueError, i1.__ne__, i4)
assert i4 == i5
i1 = Period.now('Q')
i2 = Period(datetime.now(), freq='Q')
i3 = Period.now('q')
assert i1 == i2
assert i1 == i3
i1 = Period('1982', freq='min')
i2 = Period('1982', freq='MIN')
assert i1 == i2
i2 = Period('1982', freq=('Min', 1))
assert i1 == i2
i1 = Period(year=2005, month=3, day=1, freq='D')
i2 = Period('3/1/2005', freq='D')
assert i1 == i2
i3 = Period(year=2005, month=3, day=1, freq='d')
assert i1 == i3
i1 = Period('2007-01-01 09:00:00.001')
expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1000), freq='L')
assert i1 == expected
expected = Period(np_datetime64_compat(
'2007-01-01 09:00:00.001Z'), freq='L')
assert i1 == expected
i1 = Period('2007-01-01 09:00:00.00101')
expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1010), freq='U')
assert i1 == expected
expected = Period(np_datetime64_compat('2007-01-01 09:00:00.00101Z'),
freq='U')
assert i1 == expected
pytest.raises(ValueError, Period, ordinal=200701)
pytest.raises(ValueError, Period, '2007-1-1', freq='X')
def test_construction_bday(self):
# Biz day construction, roll forward if non-weekday
i1 = Period('3/10/12', freq='B')
i2 = Period('3/10/12', freq='D')
assert i1 == i2.asfreq('B')
i2 = Period('3/11/12', freq='D')
assert i1 == i2.asfreq('B')
i2 = Period('3/12/12', freq='D')
assert i1 == i2.asfreq('B')
i3 = Period('3/10/12', freq='b')
assert i1 == i3
i1 = Period(year=2012, month=3, day=10, freq='B')
i2 = Period('3/12/12', freq='B')
assert i1 == i2
def test_construction_quarter(self):
i1 = Period(year=2005, quarter=1, freq='Q')
i2 = Period('1/1/2005', freq='Q')
assert i1 == i2
i1 = Period(year=2005, quarter=3, freq='Q')
i2 = Period('9/1/2005', freq='Q')
assert i1 == i2
i1 = Period('2005Q1')
i2 = Period(year=2005, quarter=1, freq='Q')
i3 = Period('2005q1')
assert i1 == i2
assert i1 == i3
i1 = Period('05Q1')
assert i1 == i2
lower = Period('05q1')
assert i1 == lower
i1 = Period('1Q2005')
assert i1 == i2
lower = Period('1q2005')
assert i1 == lower
i1 = Period('1Q05')
assert i1 == i2
lower = Period('1q05')
assert i1 == lower
i1 = Period('4Q1984')
assert i1.year == 1984
lower = Period('4q1984')
assert i1 == lower
def test_construction_month(self):
expected = Period('2007-01', freq='M')
i1 = Period('200701', freq='M')
assert i1 == expected
i1 = Period('200701', freq='M')
assert i1 == expected
i1 = Period(200701, freq='M')
assert i1 == expected
i1 = Period(ordinal=200701, freq='M')
assert i1.year == 18695
i1 = Period(datetime(2007, 1, 1), freq='M')
i2 = Period('200701', freq='M')
assert i1 == i2
i1 = Period(date(2007, 1, 1), freq='M')
i2 = Period(datetime(2007, 1, 1), freq='M')
i3 = Period(np.datetime64('2007-01-01'), freq='M')
i4 = Period(np_datetime64_compat('2007-01-01 00:00:00Z'), freq='M')
i5 = Period(np_datetime64_compat('2007-01-01 00:00:00.000Z'), freq='M')
assert i1 == i2
assert i1 == i3
assert i1 == i4
assert i1 == i5
def test_period_constructor_offsets(self):
assert (Period('1/1/2005', freq=offsets.MonthEnd()) ==
Period('1/1/2005', freq='M'))
assert (Period('2005', freq=offsets.YearEnd()) ==
Period('2005', freq='A'))
assert (Period('2005', freq=offsets.MonthEnd()) ==
Period('2005', freq='M'))
assert (Period('3/10/12', freq=offsets.BusinessDay()) ==
Period('3/10/12', freq='B'))
assert (Period('3/10/12', freq=offsets.Day()) ==
Period('3/10/12', freq='D'))
assert (Period(year=2005, quarter=1,
freq=offsets.QuarterEnd(startingMonth=12)) ==
Period(year=2005, quarter=1, freq='Q'))
assert (Period(year=2005, quarter=2,
freq=offsets.QuarterEnd(startingMonth=12)) ==
Period(year=2005, quarter=2, freq='Q'))
assert (Period(year=2005, month=3, day=1, freq=offsets.Day()) ==
Period(year=2005, month=3, day=1, freq='D'))
assert (Period(year=2012, month=3, day=10, freq=offsets.BDay()) ==
Period(year=2012, month=3, day=10, freq='B'))
expected = Period('2005-03-01', freq='3D')
assert (Period(year=2005, month=3, day=1,
freq=offsets.Day(3)) == expected)
assert Period(year=2005, month=3, day=1, freq='3D') == expected
assert (Period(year=2012, month=3, day=10,
freq=offsets.BDay(3)) ==
Period(year=2012, month=3, day=10, freq='3B'))
assert (Period(200701, freq=offsets.MonthEnd()) ==
Period(200701, freq='M'))
i1 = Period(ordinal=200701, freq=offsets.MonthEnd())
i2 = Period(ordinal=200701, freq='M')
assert i1 == i2
assert i1.year == 18695
assert i2.year == 18695
i1 = Period(datetime(2007, 1, 1), freq='M')
i2 = Period('200701', freq='M')
assert i1 == i2
i1 = Period(date(2007, 1, 1), freq='M')
i2 = Period(datetime(2007, 1, 1), freq='M')
i3 = Period(np.datetime64('2007-01-01'), freq='M')
i4 = Period(np_datetime64_compat('2007-01-01 00:00:00Z'), freq='M')
i5 = Period(np_datetime64_compat('2007-01-01 00:00:00.000Z'), freq='M')
assert i1 == i2
assert i1 == i3
assert i1 == i4
assert i1 == i5
i1 = Period('2007-01-01 09:00:00.001')
expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1000), freq='L')
assert i1 == expected
expected = Period(np_datetime64_compat(
'2007-01-01 09:00:00.001Z'), freq='L')
assert i1 == expected
i1 = Period('2007-01-01 09:00:00.00101')
expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1010), freq='U')
assert i1 == expected
expected = Period(np_datetime64_compat('2007-01-01 09:00:00.00101Z'),
freq='U')
assert i1 == expected
pytest.raises(ValueError, Period, ordinal=200701)
pytest.raises(ValueError, Period, '2007-1-1', freq='X')
def test_freq_str(self):
i1 = Period('1982', freq='Min')
assert i1.freq == offsets.Minute()
assert i1.freqstr == 'T'
def test_period_deprecated_freq(self):
cases = {"M": ["MTH", "MONTH", "MONTHLY", "Mth", "month", "monthly"],
"B": ["BUS", "BUSINESS", "BUSINESSLY", "WEEKDAY", "bus"],
"D": ["DAY", "DLY", "DAILY", "Day", "Dly", "Daily"],
"H": ["HR", "HOUR", "HRLY", "HOURLY", "hr", "Hour", "HRly"],
"T": ["minute", "MINUTE", "MINUTELY", "minutely"],
"S": ["sec", "SEC", "SECOND", "SECONDLY", "second"],
"L": ["MILLISECOND", "MILLISECONDLY", "millisecond"],
"U": ["MICROSECOND", "MICROSECONDLY", "microsecond"],
"N": ["NANOSECOND", "NANOSECONDLY", "nanosecond"]}
msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG
for exp, freqs in iteritems(cases):
for freq in freqs:
with pytest.raises(ValueError, match=msg):
Period('2016-03-01 09:00', freq=freq)
with pytest.raises(ValueError, match=msg):
Period(ordinal=1, freq=freq)
# check supported freq-aliases still works
p1 = Period('2016-03-01 09:00', freq=exp)
p2 = Period(ordinal=1, freq=exp)
assert isinstance(p1, Period)
assert isinstance(p2, Period)
def test_hash(self):
assert (hash(Period('2011-01', freq='M')) ==
hash(Period('2011-01', freq='M')))
assert (hash(Period('2011-01-01', freq='D')) !=
hash(Period('2011-01', freq='M')))
assert (hash(Period('2011-01', freq='3M')) !=
hash(Period('2011-01', freq='2M')))
assert (hash(Period('2011-01', freq='M')) !=
hash(Period('2011-02', freq='M')))
def test_repr(self):
p = Period('Jan-2000')
assert '2000-01' in repr(p)
p = Period('2000-12-15')
assert '2000-12-15' in repr(p)
def test_repr_nat(self):
p = Period('nat', freq='M')
assert repr(NaT) in repr(p)
def test_millisecond_repr(self):
p = Period('2000-01-01 12:15:02.123')
assert repr(p) == "Period('2000-01-01 12:15:02.123', 'L')"
def test_microsecond_repr(self):
p = Period('2000-01-01 12:15:02.123567')
assert repr(p) == "Period('2000-01-01 12:15:02.123567', 'U')"
def test_strftime(self):
p = Period('2000-1-1 12:34:12', freq='S')
res = p.strftime('%Y-%m-%d %H:%M:%S')
assert res == '2000-01-01 12:34:12'
assert isinstance(res, text_type) # GH3363
def test_sub_delta(self):
left, right = Period('2011', freq='A'), Period('2007', freq='A')
result = left - right
assert result == 4 * right.freq
with pytest.raises(period.IncompatibleFrequency):
left - Period('2007-01', freq='M')
def test_to_timestamp(self):
p = Period('1982', freq='A')
start_ts = p.to_timestamp(how='S')
aliases = ['s', 'StarT', 'BEGIn']
for a in aliases:
assert start_ts == p.to_timestamp('D', how=a)
# freq with mult should not affect to the result
assert start_ts == p.to_timestamp('3D', how=a)
end_ts = p.to_timestamp(how='E')
aliases = ['e', 'end', 'FINIsH']
for a in aliases:
assert end_ts == p.to_timestamp('D', how=a)
assert end_ts == p.to_timestamp('3D', how=a)
from_lst = ['A', 'Q', 'M', 'W', 'B', 'D', 'H', 'Min', 'S']
def _ex(p):
return Timestamp((p + p.freq).start_time.value - 1)
for i, fcode in enumerate(from_lst):
p = Period('1982', freq=fcode)
result = p.to_timestamp().to_period(fcode)
assert result == p
assert p.start_time == p.to_timestamp(how='S')
assert p.end_time == _ex(p)
# Frequency other than daily
p = Period('1985', freq='A')
result = p.to_timestamp('H', how='end')
expected = Timestamp(1986, 1, 1) - Timedelta(1, 'ns')
assert result == expected
result = p.to_timestamp('3H', how='end')
assert result == expected
result = p.to_timestamp('T', how='end')
expected = Timestamp(1986, 1, 1) - Timedelta(1, 'ns')
assert result == expected
result = p.to_timestamp('2T', how='end')
assert result == expected
result = p.to_timestamp(how='end')
expected = Timestamp(1986, 1, 1) - Timedelta(1, 'ns')
assert result == expected
expected = datetime(1985, 1, 1)
result = p.to_timestamp('H', how='start')
assert result == expected
result = p.to_timestamp('T', how='start')
assert result == expected
result = p.to_timestamp('S', how='start')
assert result == expected
result = p.to_timestamp('3H', how='start')
assert result == expected
result = p.to_timestamp('5S', how='start')
assert result == expected
def test_start_time(self):
freq_lst = ['A', 'Q', 'M', 'D', 'H', 'T', 'S']
xp = datetime(2012, 1, 1)
for f in freq_lst:
p = Period('2012', freq=f)
assert p.start_time == xp
assert Period('2012', freq='B').start_time == datetime(2012, 1, 2)
assert Period('2012', freq='W').start_time == datetime(2011, 12, 26)
def test_end_time(self):
p = Period('2012', freq='A')
def _ex(*args):
return Timestamp(Timestamp(datetime(*args)).value - 1)
xp = _ex(2013, 1, 1)
assert xp == p.end_time
p = Period('2012', freq='Q')
xp = _ex(2012, 4, 1)
assert xp == p.end_time
p = Period('2012', freq='M')
xp = _ex(2012, 2, 1)
assert xp == p.end_time
p = Period('2012', freq='D')
xp = _ex(2012, 1, 2)
assert xp == p.end_time
p = Period('2012', freq='H')
xp = _ex(2012, 1, 1, 1)
assert xp == p.end_time
p = Period('2012', freq='B')
xp = _ex(2012, 1, 3)
assert xp == p.end_time
p = Period('2012', freq='W')
xp = _ex(2012, 1, 2)
assert xp == p.end_time
# Test for GH 11738
p = Period('2012', freq='15D')
xp = _ex(2012, 1, 16)
assert xp == p.end_time
p = Period('2012', freq='1D1H')
xp = _ex(2012, 1, 2, 1)
assert xp == p.end_time
p = Period('2012', freq='1H1D')
xp = _ex(2012, 1, 2, 1)
assert xp == p.end_time
def test_anchor_week_end_time(self):
def _ex(*args):
return Timestamp(Timestamp(datetime(*args)).value - 1)
p = Period('2013-1-1', 'W-SAT')
xp = _ex(2013, 1, 6)
assert p.end_time == xp
def test_properties_annually(self):
# Test properties on Periods with annually frequency.
a_date = Period(freq='A', year=2007)
assert a_date.year == 2007
def test_properties_quarterly(self):
# Test properties on Periods with daily frequency.
qedec_date = Period(freq="Q-DEC", year=2007, quarter=1)
qejan_date = Period(freq="Q-JAN", year=2007, quarter=1)
qejun_date = Period(freq="Q-JUN", year=2007, quarter=1)
#
for x in range(3):
for qd in (qedec_date, qejan_date, qejun_date):
with tm.assert_produces_warning(FutureWarning):
assert (qd + x).qyear == 2007
assert (qd + x).quarter == x + 1
def test_properties_monthly(self):
# Test properties on Periods with daily frequency.
m_date = Period(freq='M', year=2007, month=1)
for x in range(11):
with tm.assert_produces_warning(FutureWarning):
m_ival_x = m_date + x
assert m_ival_x.year == 2007
if 1 <= x + 1 <= 3:
assert m_ival_x.quarter == 1
elif 4 <= x + 1 <= 6:
assert m_ival_x.quarter == 2
elif 7 <= x + 1 <= 9:
assert m_ival_x.quarter == 3
elif 10 <= x + 1 <= 12:
assert m_ival_x.quarter == 4
assert m_ival_x.month == x + 1
def test_properties_weekly(self):
# Test properties on Periods with daily frequency.
w_date = Period(freq='W', year=2007, month=1, day=7)
#
assert w_date.year == 2007
assert w_date.quarter == 1
assert w_date.month == 1
assert w_date.week == 1
with tm.assert_produces_warning(FutureWarning):
assert (w_date - 1).week == 52
assert w_date.days_in_month == 31
assert Period(freq='W', year=2012,
month=2, day=1).days_in_month == 29
def test_properties_weekly_legacy(self):
# Test properties on Periods with daily frequency.
w_date = Period(freq='W', year=2007, month=1, day=7)
assert w_date.year == 2007
assert w_date.quarter == 1
assert w_date.month == 1
assert w_date.week == 1
with tm.assert_produces_warning(FutureWarning):
assert (w_date - 1).week == 52
assert w_date.days_in_month == 31
exp = Period(freq='W', year=2012, month=2, day=1)
assert exp.days_in_month == 29
msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG
with pytest.raises(ValueError, match=msg):
Period(freq='WK', year=2007, month=1, day=7)
def test_properties_daily(self):
# Test properties on Periods with daily frequency.
b_date = Period(freq='B', year=2007, month=1, day=1)
#
assert b_date.year == 2007
assert b_date.quarter == 1
assert b_date.month == 1
assert b_date.day == 1
assert b_date.weekday == 0
assert b_date.dayofyear == 1
assert b_date.days_in_month == 31
assert Period(freq='B', year=2012,
month=2, day=1).days_in_month == 29
d_date = Period(freq='D', year=2007, month=1, day=1)
assert d_date.year == 2007
assert d_date.quarter == 1
assert d_date.month == 1
assert d_date.day == 1
assert d_date.weekday == 0
assert d_date.dayofyear == 1
assert d_date.days_in_month == 31
assert Period(freq='D', year=2012, month=2,
day=1).days_in_month == 29
def test_properties_hourly(self):
# Test properties on Periods with hourly frequency.
h_date1 = Period(freq='H', year=2007, month=1, day=1, hour=0)
h_date2 = Period(freq='2H', year=2007, month=1, day=1, hour=0)
for h_date in [h_date1, h_date2]:
assert h_date.year == 2007
assert h_date.quarter == 1
assert h_date.month == 1
assert h_date.day == 1
assert h_date.weekday == 0
assert h_date.dayofyear == 1
assert h_date.hour == 0
assert h_date.days_in_month == 31
assert Period(freq='H', year=2012, month=2, day=1,
hour=0).days_in_month == 29
def test_properties_minutely(self):
# Test properties on Periods with minutely frequency.
t_date = Period(freq='Min', year=2007, month=1, day=1, hour=0,
minute=0)
#
assert t_date.quarter == 1
assert t_date.month == 1
assert t_date.day == 1
assert t_date.weekday == 0
assert t_date.dayofyear == 1
assert t_date.hour == 0
assert t_date.minute == 0
assert t_date.days_in_month == 31
assert Period(freq='D', year=2012, month=2, day=1, hour=0,
minute=0).days_in_month == 29
def test_properties_secondly(self):
# Test properties on Periods with secondly frequency.
s_date = Period(freq='Min', year=2007, month=1, day=1, hour=0,
minute=0, second=0)
#
assert s_date.year == 2007
assert s_date.quarter == 1
assert s_date.month == 1
assert s_date.day == 1
assert s_date.weekday == 0
assert s_date.dayofyear == 1
assert s_date.hour == 0
assert s_date.minute == 0
assert s_date.second == 0
assert s_date.days_in_month == 31
assert Period(freq='Min', year=2012, month=2, day=1, hour=0,
minute=0, second=0).days_in_month == 29
def test_constructor_corner(self):
expected = Period('2007-01', freq='2M')
assert Period(year=2007, month=1, freq='2M') == expected
pytest.raises(ValueError, Period, datetime.now())
pytest.raises(ValueError, Period, datetime.now().date())
pytest.raises(ValueError, Period, 1.6, freq='D')
pytest.raises(ValueError, Period, ordinal=1.6, freq='D')
pytest.raises(ValueError, Period, ordinal=2, value=1, freq='D')
assert Period(None) is NaT
pytest.raises(ValueError, Period, month=1)
p = Period('2007-01-01', freq='D')
result = Period(p, freq='A')
exp = Period('2007', freq='A')
assert result == exp
def test_constructor_infer_freq(self):
p = Period('2007-01-01')
assert p.freq == 'D'
p = Period('2007-01-01 07')
assert p.freq == 'H'
p = Period('2007-01-01 07:10')
assert p.freq == 'T'
p = Period('2007-01-01 07:10:15')
assert p.freq == 'S'
p = Period('2007-01-01 07:10:15.123')
assert p.freq == 'L'
p = Period('2007-01-01 07:10:15.123000')
assert p.freq == 'L'
p = Period('2007-01-01 07:10:15.123400')
assert p.freq == 'U'
def test_badinput(self):
pytest.raises(ValueError, Period, '-2000', 'A')
pytest.raises(DateParseError, Period, '0', 'A')
pytest.raises(DateParseError, Period, '1/1/-2000', 'A')
def test_multiples(self):
result1 = Period('1989', freq='2A')
result2 = Period('1989', freq='A')
assert result1.ordinal == result2.ordinal
assert result1.freqstr == '2A-DEC'
assert result2.freqstr == 'A-DEC'
assert result1.freq == offsets.YearEnd(2)
assert result2.freq == offsets.YearEnd()
with tm.assert_produces_warning(FutureWarning):
assert (result1 + 1).ordinal == result1.ordinal + 2
assert (1 + result1).ordinal == result1.ordinal + 2
assert (result1 - 1).ordinal == result2.ordinal - 2
assert (-1 + result1).ordinal == result2.ordinal - 2
def test_round_trip(self):
p = Period('2000Q1')
new_p = tm.round_trip_pickle(p)
assert new_p == p
class TestPeriodField(object):
def test_get_period_field_array_raises_on_out_of_range(self):
pytest.raises(ValueError, libperiod.get_period_field_arr, -1,
np.empty(1), 0)
class TestComparisons(object):
def setup_method(self, method):
self.january1 = Period('2000-01', 'M')
self.january2 = Period('2000-01', 'M')
self.february = Period('2000-02', 'M')
self.march = Period('2000-03', 'M')
self.day = Period('2012-01-01', 'D')
def test_equal(self):
assert self.january1 == self.january2
def test_equal_Raises_Value(self):
with pytest.raises(period.IncompatibleFrequency):
self.january1 == self.day
def test_notEqual(self):
assert self.january1 != 1
assert self.january1 != self.february
def test_greater(self):
assert self.february > self.january1
def test_greater_Raises_Value(self):
with pytest.raises(period.IncompatibleFrequency):
self.january1 > self.day
def test_greater_Raises_Type(self):
with pytest.raises(TypeError):
self.january1 > 1
def test_greaterEqual(self):
assert self.january1 >= self.january2
def test_greaterEqual_Raises_Value(self):
with pytest.raises(period.IncompatibleFrequency):
self.january1 >= self.day
with pytest.raises(TypeError):
print(self.january1 >= 1)
def test_smallerEqual(self):
assert self.january1 <= self.january2
def test_smallerEqual_Raises_Value(self):
with pytest.raises(period.IncompatibleFrequency):
self.january1 <= self.day
def test_smallerEqual_Raises_Type(self):
with pytest.raises(TypeError):
self.january1 <= 1
def test_smaller(self):
assert self.january1 < self.february
def test_smaller_Raises_Value(self):
with pytest.raises(period.IncompatibleFrequency):
self.january1 < self.day
def test_smaller_Raises_Type(self):
with pytest.raises(TypeError):
self.january1 < 1
def test_sort(self):
periods = [self.march, self.january1, self.february]
correctPeriods = [self.january1, self.february, self.march]
assert sorted(periods) == correctPeriods
def test_period_nat_comp(self):
p_nat = Period('NaT', freq='D')