forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathperiod.pyx
2554 lines (2041 loc) · 78.1 KB
/
period.pyx
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
# -*- coding: utf-8 -*-
from datetime import datetime
from cpython cimport (
PyObject_RichCompareBool,
Py_EQ, Py_NE)
from numpy cimport int64_t, import_array, ndarray
import numpy as np
import_array()
from libc.stdlib cimport free, malloc
from libc.time cimport strftime, tm
from libc.string cimport strlen, memset
import cython
from cpython.datetime cimport (PyDateTime_Check, PyDelta_Check, PyDate_Check,
PyDateTime_IMPORT)
# import datetime C API
PyDateTime_IMPORT
from pandas._libs.tslibs.np_datetime cimport (
npy_datetimestruct, dtstruct_to_dt64, dt64_to_dtstruct,
pandas_datetime_to_datetimestruct, NPY_DATETIMEUNIT, NPY_FR_D)
cdef extern from "src/datetime/np_datetime.h":
int64_t npy_datetimestruct_to_datetime(NPY_DATETIMEUNIT fr,
npy_datetimestruct *d) nogil
cimport pandas._libs.tslibs.util as util
from pandas._libs.tslibs.util cimport is_period_object, is_string_object
from pandas._libs.tslibs.timestamps import Timestamp
from pandas._libs.tslibs.timezones cimport is_utc, is_tzlocal, get_dst_info
from pandas._libs.tslibs.timedeltas import Timedelta
from pandas._libs.tslibs.timedeltas cimport delta_to_nanoseconds
cimport pandas._libs.tslibs.ccalendar as ccalendar
from pandas._libs.tslibs.ccalendar cimport (
dayofweek, get_day_of_year, is_leapyear)
from pandas._libs.tslibs.ccalendar import MONTH_NUMBERS
from pandas._libs.tslibs.conversion cimport tz_convert_utc_to_tzlocal
from pandas._libs.tslibs.frequencies cimport (
get_freq_code, get_base_alias, get_to_timestamp_base, get_freq_str,
get_rule_month)
from pandas._libs.tslibs.parsing import parse_time_string
from pandas._libs.tslibs.resolution import Resolution
from pandas._libs.tslibs.nattype import nat_strings
from pandas._libs.tslibs.nattype cimport (
_nat_scalar_rules, NPY_NAT, is_null_datetimelike, c_NaT as NaT)
from pandas._libs.tslibs.offsets cimport to_offset
from pandas._libs.tslibs.offsets import _Tick
cdef:
bint PY2 = str == bytes
enum:
INT32_MIN = -2147483648
ctypedef struct asfreq_info:
int64_t intraday_conversion_factor
int is_end
int to_end
int from_end
ctypedef int64_t (*freq_conv_func)(int64_t, asfreq_info*) nogil
cdef extern from *:
"""
/*** FREQUENCY CONSTANTS ***/
// See frequencies.pyx for more detailed variants
#define FR_ANN 1000 /* Annual */
#define FR_QTR 2000 /* Quarterly - December year end (default Q) */
#define FR_MTH 3000 /* Monthly */
#define FR_WK 4000 /* Weekly */
#define FR_BUS 5000 /* Business days */
#define FR_DAY 6000 /* Daily */
#define FR_HR 7000 /* Hourly */
#define FR_MIN 8000 /* Minutely */
#define FR_SEC 9000 /* Secondly */
#define FR_MS 10000 /* Millisecondly */
#define FR_US 11000 /* Microsecondly */
#define FR_NS 12000 /* Nanosecondly */
#define FR_UND -10000 /* Undefined */
// must use npy typedef b/c int64_t is aliased in cython-generated c
static npy_int64 daytime_conversion_factor_matrix[7][7] = {
{1, 24, 1440, 86400, 86400000, 86400000000, 86400000000000},
{0, 1, 60, 3600, 3600000, 3600000000, 3600000000000},
{0, 0, 1, 60, 60000, 60000000, 60000000000},
{0, 0, 0, 1, 1000, 1000000, 1000000000},
{0, 0, 0, 0, 1, 1000, 1000000},
{0, 0, 0, 0, 0, 1, 1000},
{0, 0, 0, 0, 0, 0, 1}};
"""
int64_t daytime_conversion_factor_matrix[7][7]
# TODO: Can we get these frequencies from frequencies.FreqGroup?
int FR_ANN
int FR_QTR
int FR_MTH
int FR_WK
int FR_DAY
int FR_HR
int FR_MIN
int FR_SEC
int FR_MS
int FR_US
int FR_NS
int FR_BUS
int FR_UND
cdef int max_value(int left, int right) nogil:
if left > right:
return left
return right
cdef int min_value(int left, int right) nogil:
if left < right:
return left
return right
cdef int64_t get_daytime_conversion_factor(int from_index, int to_index) nogil:
cdef:
int row = min_value(from_index, to_index)
int col = max_value(from_index, to_index)
# row or col < 6 means frequency strictly lower than Daily, which
# do not use daytime_conversion_factors
if row < 6:
return 0
elif col < 6:
return 0
return daytime_conversion_factor_matrix[row - 6][col - 6]
cdef int64_t nofunc(int64_t ordinal, asfreq_info *af_info) nogil:
return INT32_MIN
cdef int64_t no_op(int64_t ordinal, asfreq_info *af_info) nogil:
return ordinal
cdef freq_conv_func get_asfreq_func(int from_freq, int to_freq) nogil:
cdef:
int from_group = get_freq_group(from_freq)
int to_group = get_freq_group(to_freq)
if from_group == FR_UND:
from_group = FR_DAY
if from_group == FR_BUS:
if to_group == FR_ANN:
return <freq_conv_func>asfreq_BtoA
elif to_group == FR_QTR:
return <freq_conv_func>asfreq_BtoQ
elif to_group == FR_MTH:
return <freq_conv_func>asfreq_BtoM
elif to_group == FR_WK:
return <freq_conv_func>asfreq_BtoW
elif to_group == FR_BUS:
return <freq_conv_func>no_op
elif to_group in [FR_DAY, FR_HR, FR_MIN, FR_SEC, FR_MS, FR_US, FR_NS]:
return <freq_conv_func>asfreq_BtoDT
else:
return <freq_conv_func>nofunc
elif to_group == FR_BUS:
if from_group == FR_ANN:
return <freq_conv_func>asfreq_AtoB
elif from_group == FR_QTR:
return <freq_conv_func>asfreq_QtoB
elif from_group == FR_MTH:
return <freq_conv_func>asfreq_MtoB
elif from_group == FR_WK:
return <freq_conv_func>asfreq_WtoB
elif from_group in [FR_DAY, FR_HR, FR_MIN, FR_SEC,
FR_MS, FR_US, FR_NS]:
return <freq_conv_func>asfreq_DTtoB
else:
return <freq_conv_func>nofunc
elif from_group == FR_ANN:
if to_group == FR_ANN:
return <freq_conv_func>asfreq_AtoA
elif to_group == FR_QTR:
return <freq_conv_func>asfreq_AtoQ
elif to_group == FR_MTH:
return <freq_conv_func>asfreq_AtoM
elif to_group == FR_WK:
return <freq_conv_func>asfreq_AtoW
elif to_group in [FR_DAY, FR_HR, FR_MIN, FR_SEC, FR_MS, FR_US, FR_NS]:
return <freq_conv_func>asfreq_AtoDT
else:
return <freq_conv_func>nofunc
elif from_group == FR_QTR:
if to_group == FR_ANN:
return <freq_conv_func>asfreq_QtoA
elif to_group == FR_QTR:
return <freq_conv_func>asfreq_QtoQ
elif to_group == FR_MTH:
return <freq_conv_func>asfreq_QtoM
elif to_group == FR_WK:
return <freq_conv_func>asfreq_QtoW
elif to_group in [FR_DAY, FR_HR, FR_MIN, FR_SEC, FR_MS, FR_US, FR_NS]:
return <freq_conv_func>asfreq_QtoDT
else:
return <freq_conv_func>nofunc
elif from_group == FR_MTH:
if to_group == FR_ANN:
return <freq_conv_func>asfreq_MtoA
elif to_group == FR_QTR:
return <freq_conv_func>asfreq_MtoQ
elif to_group == FR_MTH:
return <freq_conv_func>no_op
elif to_group == FR_WK:
return <freq_conv_func>asfreq_MtoW
elif to_group in [FR_DAY, FR_HR, FR_MIN, FR_SEC, FR_MS, FR_US, FR_NS]:
return <freq_conv_func>asfreq_MtoDT
else:
return <freq_conv_func>nofunc
elif from_group == FR_WK:
if to_group == FR_ANN:
return <freq_conv_func>asfreq_WtoA
elif to_group == FR_QTR:
return <freq_conv_func>asfreq_WtoQ
elif to_group == FR_MTH:
return <freq_conv_func>asfreq_WtoM
elif to_group == FR_WK:
return <freq_conv_func>asfreq_WtoW
elif to_group in [FR_DAY, FR_HR, FR_MIN, FR_SEC, FR_MS, FR_US, FR_NS]:
return <freq_conv_func>asfreq_WtoDT
else:
return <freq_conv_func>nofunc
elif from_group in [FR_DAY, FR_HR, FR_MIN, FR_SEC, FR_MS, FR_US, FR_NS]:
if to_group == FR_ANN:
return <freq_conv_func>asfreq_DTtoA
elif to_group == FR_QTR:
return <freq_conv_func>asfreq_DTtoQ
elif to_group == FR_MTH:
return <freq_conv_func>asfreq_DTtoM
elif to_group == FR_WK:
return <freq_conv_func>asfreq_DTtoW
elif to_group in [FR_DAY, FR_HR, FR_MIN, FR_SEC, FR_MS, FR_US, FR_NS]:
if from_group > to_group:
return <freq_conv_func>downsample_daytime
else:
return <freq_conv_func>upsample_daytime
else:
return <freq_conv_func>nofunc
else:
return <freq_conv_func>nofunc
# --------------------------------------------------------------------
# Frequency Conversion Helpers
cdef int64_t DtoB_weekday(int64_t unix_date) nogil:
return ((unix_date + 4) // 7) * 5 + ((unix_date + 4) % 7) - 4
cdef int64_t DtoB(npy_datetimestruct *dts, int roll_back,
int64_t unix_date) nogil:
cdef:
int day_of_week = dayofweek(dts.year, dts.month, dts.day)
if roll_back == 1:
if day_of_week > 4:
# change to friday before weekend
unix_date -= (day_of_week - 4)
else:
if day_of_week > 4:
# change to Monday after weekend
unix_date += (7 - day_of_week)
return DtoB_weekday(unix_date)
cdef inline int64_t upsample_daytime(int64_t ordinal,
asfreq_info *af_info) nogil:
if (af_info.is_end):
return (ordinal + 1) * af_info.intraday_conversion_factor - 1
else:
return ordinal * af_info.intraday_conversion_factor
cdef inline int64_t downsample_daytime(int64_t ordinal,
asfreq_info *af_info) nogil:
return ordinal // (af_info.intraday_conversion_factor)
cdef inline int64_t transform_via_day(int64_t ordinal,
asfreq_info *af_info,
freq_conv_func first_func,
freq_conv_func second_func) nogil:
cdef:
int64_t result
result = first_func(ordinal, af_info)
result = second_func(result, af_info)
return result
# --------------------------------------------------------------------
# Conversion _to_ Daily Freq
cdef void AtoD_ym(int64_t ordinal, int64_t *year,
int *month, asfreq_info *af_info) nogil:
year[0] = ordinal + 1970
month[0] = 1
if af_info.from_end != 12:
month[0] += af_info.from_end
if month[0] > 12:
# This case is never reached, but is kept for symmetry
# with QtoD_ym
month[0] -= 12
else:
year[0] -= 1
cdef int64_t asfreq_AtoDT(int64_t ordinal, asfreq_info *af_info) nogil:
cdef:
int64_t unix_date, year
int month
ordinal += af_info.is_end
AtoD_ym(ordinal, &year, &month, af_info)
unix_date = unix_date_from_ymd(year, month, 1)
unix_date -= af_info.is_end
return upsample_daytime(unix_date, af_info)
cdef void QtoD_ym(int64_t ordinal, int *year,
int *month, asfreq_info *af_info) nogil:
year[0] = ordinal // 4 + 1970
month[0] = (ordinal % 4) * 3 + 1
if af_info.from_end != 12:
month[0] += af_info.from_end
if month[0] > 12:
month[0] -= 12
else:
year[0] -= 1
cdef int64_t asfreq_QtoDT(int64_t ordinal, asfreq_info *af_info) nogil:
cdef:
int64_t unix_date
int year, month
ordinal += af_info.is_end
QtoD_ym(ordinal, &year, &month, af_info)
unix_date = unix_date_from_ymd(year, month, 1)
unix_date -= af_info.is_end
return upsample_daytime(unix_date, af_info)
cdef void MtoD_ym(int64_t ordinal, int *year, int *month) nogil:
year[0] = ordinal // 12 + 1970
month[0] = ordinal % 12 + 1
cdef int64_t asfreq_MtoDT(int64_t ordinal, asfreq_info *af_info) nogil:
cdef:
int64_t unix_date
int year, month
ordinal += af_info.is_end
MtoD_ym(ordinal, &year, &month)
unix_date = unix_date_from_ymd(year, month, 1)
unix_date -= af_info.is_end
return upsample_daytime(unix_date, af_info)
cdef int64_t asfreq_WtoDT(int64_t ordinal, asfreq_info *af_info) nogil:
ordinal = (ordinal * 7 + af_info.from_end - 4 +
(7 - 1) * (af_info.is_end - 1))
return upsample_daytime(ordinal, af_info)
# --------------------------------------------------------------------
# Conversion _to_ BusinessDay Freq
cdef int64_t asfreq_AtoB(int64_t ordinal, asfreq_info *af_info) nogil:
cdef:
int roll_back
npy_datetimestruct dts
int64_t unix_date = asfreq_AtoDT(ordinal, af_info)
pandas_datetime_to_datetimestruct(unix_date, NPY_FR_D, &dts)
roll_back = af_info.is_end
return DtoB(&dts, roll_back, unix_date)
cdef int64_t asfreq_QtoB(int64_t ordinal, asfreq_info *af_info) nogil:
cdef:
int roll_back
npy_datetimestruct dts
int64_t unix_date = asfreq_QtoDT(ordinal, af_info)
pandas_datetime_to_datetimestruct(unix_date, NPY_FR_D, &dts)
roll_back = af_info.is_end
return DtoB(&dts, roll_back, unix_date)
cdef int64_t asfreq_MtoB(int64_t ordinal, asfreq_info *af_info) nogil:
cdef:
int roll_back
npy_datetimestruct dts
int64_t unix_date = asfreq_MtoDT(ordinal, af_info)
pandas_datetime_to_datetimestruct(unix_date, NPY_FR_D, &dts)
roll_back = af_info.is_end
return DtoB(&dts, roll_back, unix_date)
cdef int64_t asfreq_WtoB(int64_t ordinal, asfreq_info *af_info) nogil:
cdef:
int roll_back
npy_datetimestruct dts
int64_t unix_date = asfreq_WtoDT(ordinal, af_info)
pandas_datetime_to_datetimestruct(unix_date, NPY_FR_D, &dts)
roll_back = af_info.is_end
return DtoB(&dts, roll_back, unix_date)
cdef int64_t asfreq_DTtoB(int64_t ordinal, asfreq_info *af_info) nogil:
cdef:
int roll_back
npy_datetimestruct dts
int64_t unix_date = downsample_daytime(ordinal, af_info)
pandas_datetime_to_datetimestruct(unix_date, NPY_FR_D, &dts)
# This usage defines roll_back the opposite way from the others
roll_back = 1 - af_info.is_end
return DtoB(&dts, roll_back, unix_date)
# ----------------------------------------------------------------------
# Conversion _from_ Daily Freq
cdef int64_t asfreq_DTtoA(int64_t ordinal, asfreq_info *af_info) nogil:
cdef:
npy_datetimestruct dts
ordinal = downsample_daytime(ordinal, af_info)
pandas_datetime_to_datetimestruct(ordinal, NPY_FR_D, &dts)
if dts.month > af_info.to_end:
return <int64_t>(dts.year + 1 - 1970)
else:
return <int64_t>(dts.year - 1970)
cdef int DtoQ_yq(int64_t ordinal, asfreq_info *af_info, int *year) nogil:
cdef:
npy_datetimestruct dts
int quarter
pandas_datetime_to_datetimestruct(ordinal, NPY_FR_D, &dts)
# TODO: Another version of this function used
# date_info_from_days_and_time(&dts, unix_date, 0)
# instead of pandas_datetime_to_datetimestruct; is one more performant?
if af_info.to_end != 12:
dts.month -= af_info.to_end
if dts.month <= 0:
dts.month += 12
else:
dts.year += 1
year[0] = dts.year
quarter = month_to_quarter(dts.month)
return quarter
cdef int64_t asfreq_DTtoQ(int64_t ordinal, asfreq_info *af_info) nogil:
cdef:
int year, quarter
ordinal = downsample_daytime(ordinal, af_info)
quarter = DtoQ_yq(ordinal, af_info, &year)
return <int64_t>((year - 1970) * 4 + quarter - 1)
cdef int64_t asfreq_DTtoM(int64_t ordinal, asfreq_info *af_info) nogil:
cdef:
npy_datetimestruct dts
ordinal = downsample_daytime(ordinal, af_info)
pandas_datetime_to_datetimestruct(ordinal, NPY_FR_D, &dts)
return <int64_t>((dts.year - 1970) * 12 + dts.month - 1)
cdef int64_t asfreq_DTtoW(int64_t ordinal, asfreq_info *af_info) nogil:
ordinal = downsample_daytime(ordinal, af_info)
return (ordinal + 3 - af_info.to_end) // 7 + 1
# --------------------------------------------------------------------
# Conversion _from_ BusinessDay Freq
cdef int64_t asfreq_BtoDT(int64_t ordinal, asfreq_info *af_info) nogil:
ordinal = ((ordinal + 3) // 5) * 7 + (ordinal + 3) % 5 -3
return upsample_daytime(ordinal, af_info)
cdef int64_t asfreq_BtoA(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_BtoDT,
<freq_conv_func>asfreq_DTtoA)
cdef int64_t asfreq_BtoQ(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_BtoDT,
<freq_conv_func>asfreq_DTtoQ)
cdef int64_t asfreq_BtoM(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_BtoDT,
<freq_conv_func>asfreq_DTtoM)
cdef int64_t asfreq_BtoW(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_BtoDT,
<freq_conv_func>asfreq_DTtoW)
# ----------------------------------------------------------------------
# Conversion _from_ Annual Freq
cdef int64_t asfreq_AtoA(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_AtoDT,
<freq_conv_func>asfreq_DTtoA)
cdef int64_t asfreq_AtoQ(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_AtoDT,
<freq_conv_func>asfreq_DTtoQ)
cdef int64_t asfreq_AtoM(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_AtoDT,
<freq_conv_func>asfreq_DTtoM)
cdef int64_t asfreq_AtoW(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_AtoDT,
<freq_conv_func>asfreq_DTtoW)
# ----------------------------------------------------------------------
# Conversion _from_ Quarterly Freq
cdef int64_t asfreq_QtoQ(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_QtoDT,
<freq_conv_func>asfreq_DTtoQ)
cdef int64_t asfreq_QtoA(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_QtoDT,
<freq_conv_func>asfreq_DTtoA)
cdef int64_t asfreq_QtoM(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_QtoDT,
<freq_conv_func>asfreq_DTtoM)
cdef int64_t asfreq_QtoW(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_QtoDT,
<freq_conv_func>asfreq_DTtoW)
# ----------------------------------------------------------------------
# Conversion _from_ Monthly Freq
cdef int64_t asfreq_MtoA(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_MtoDT,
<freq_conv_func>asfreq_DTtoA)
cdef int64_t asfreq_MtoQ(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_MtoDT,
<freq_conv_func>asfreq_DTtoQ)
cdef int64_t asfreq_MtoW(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_MtoDT,
<freq_conv_func>asfreq_DTtoW)
# ----------------------------------------------------------------------
# Conversion _from_ Weekly Freq
cdef int64_t asfreq_WtoA(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_WtoDT,
<freq_conv_func>asfreq_DTtoA)
cdef int64_t asfreq_WtoQ(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_WtoDT,
<freq_conv_func>asfreq_DTtoQ)
cdef int64_t asfreq_WtoM(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_WtoDT,
<freq_conv_func>asfreq_DTtoM)
cdef int64_t asfreq_WtoW(int64_t ordinal, asfreq_info *af_info) nogil:
return transform_via_day(ordinal, af_info,
<freq_conv_func>asfreq_WtoDT,
<freq_conv_func>asfreq_DTtoW)
# ----------------------------------------------------------------------
@cython.cdivision
cdef char* c_strftime(npy_datetimestruct *dts, char *fmt):
"""
Generate a nice string representation of the period
object, originally from DateObject_strftime
Parameters
----------
dts : npy_datetimestruct*
fmt : char*
Returns
-------
result : char*
"""
cdef:
tm c_date
char *result
int result_len = strlen(fmt) + 50
c_date.tm_sec = dts.sec
c_date.tm_min = dts.min
c_date.tm_hour = dts.hour
c_date.tm_mday = dts.day
c_date.tm_mon = dts.month - 1
c_date.tm_year = dts.year - 1900
c_date.tm_wday = (dayofweek(dts.year, dts.month, dts.day) + 1) % 7
c_date.tm_yday = get_day_of_year(dts.year, dts.month, dts.day) - 1
c_date.tm_isdst = -1
result = <char*>malloc(result_len * sizeof(char))
strftime(result, result_len, fmt, &c_date)
return result
# ----------------------------------------------------------------------
# Conversion between date_info and npy_datetimestruct
cdef inline int get_freq_group(int freq) nogil:
return (freq // 1000) * 1000
cdef inline int get_freq_group_index(int freq) nogil:
return freq // 1000
# Find the unix_date (days elapsed since datetime(1970, 1, 1)
# for the given year/month/day.
# Assumes GREGORIAN_CALENDAR */
cdef int64_t unix_date_from_ymd(int year, int month, int day) nogil:
# Calculate the absolute date
cdef:
npy_datetimestruct dts
int64_t unix_date
memset(&dts, 0, sizeof(npy_datetimestruct))
dts.year = year
dts.month = month
dts.day = day
unix_date = npy_datetimestruct_to_datetime(NPY_FR_D, &dts)
return unix_date
# specifically _dont_ use cdvision or else ordinals near -1 are assigned to
# incorrect dates GH#19643
@cython.cdivision(False)
cdef int64_t get_period_ordinal(npy_datetimestruct *dts, int freq) nogil:
"""
Generate an ordinal in period space
Parameters
----------
dts: npy_datetimestruct*
freq : int
Returns
-------
period_ordinal : int64_t
"""
cdef:
int64_t unix_date, seconds, delta
int64_t weeks
int64_t day_adj
int freq_group, fmonth, mdiff
freq_group = get_freq_group(freq)
if freq_group == FR_ANN:
fmonth = freq - FR_ANN
if fmonth == 0:
fmonth = 12
mdiff = dts.month - fmonth
if mdiff <= 0:
return dts.year - 1970
else:
return dts.year - 1970 + 1
elif freq_group == FR_QTR:
fmonth = freq - FR_QTR
if fmonth == 0:
fmonth = 12
mdiff = dts.month - fmonth
# TODO: Aren't the next two conditions equivalent to
# unconditional incrementing?
if mdiff < 0:
mdiff += 12
if dts.month >= fmonth:
mdiff += 12
return (dts.year - 1970) * 4 + (mdiff - 1) // 3
elif freq == FR_MTH:
return (dts.year - 1970) * 12 + dts.month - 1
unix_date = npy_datetimestruct_to_datetime(NPY_FR_D, dts)
if freq >= FR_SEC:
seconds = unix_date * 86400 + dts.hour * 3600 + dts.min * 60 + dts.sec
if freq == FR_MS:
return seconds * 1000 + dts.us // 1000
elif freq == FR_US:
return seconds * 1000000 + dts.us
elif freq == FR_NS:
return (seconds * 1000000000 +
dts.us * 1000 + dts.ps // 1000)
else:
return seconds
elif freq == FR_MIN:
return unix_date * 1440 + dts.hour * 60 + dts.min
elif freq == FR_HR:
return unix_date * 24 + dts.hour
elif freq == FR_DAY:
return unix_date
elif freq == FR_UND:
return unix_date
elif freq == FR_BUS:
# calculate the current week (counting from 1970-01-01) treating
# sunday as last day of a week
weeks = (unix_date + 3) // 7
# calculate the current weekday (in range 1 .. 7)
delta = (unix_date + 3) % 7 + 1
# return the number of business days in full weeks plus the business
# days in the last - possible partial - week
if delta <= 5:
return (5 * weeks) + delta - 4
else:
return (5 * weeks) + (5 + 1) - 4
elif freq_group == FR_WK:
day_adj = freq - FR_WK
return (unix_date + 3 - day_adj) // 7 + 1
# raise ValueError
cdef void get_date_info(int64_t ordinal, int freq,
npy_datetimestruct *dts) nogil:
cdef:
int64_t unix_date
double abstime
unix_date = get_unix_date(ordinal, freq)
abstime = get_abs_time(freq, unix_date, ordinal)
while abstime < 0:
abstime += 86400
unix_date -= 1
while abstime >= 86400:
abstime -= 86400
unix_date += 1
date_info_from_days_and_time(dts, unix_date, abstime)
cdef int64_t get_unix_date(int64_t period_ordinal, int freq) nogil:
"""
Returns the proleptic Gregorian ordinal of the date, as an integer.
This corresponds to the number of days since Jan., 1st, 1970 AD.
When the instance has a frequency less than daily, the proleptic date
is calculated for the last day of the period.
Parameters
----------
period_ordinal : int64_t
freq : int
Returns
-------
unix_date : int64_t number of days since datetime(1970, 1, 1)
"""
cdef:
asfreq_info af_info
freq_conv_func toDaily = NULL
if freq == FR_DAY:
return period_ordinal
toDaily = get_asfreq_func(freq, FR_DAY)
get_asfreq_info(freq, FR_DAY, True, &af_info)
return toDaily(period_ordinal, &af_info)
@cython.cdivision
cdef void date_info_from_days_and_time(npy_datetimestruct *dts,
int64_t unix_date,
double abstime) nogil:
"""
Set the instance's value using the given date and time.
Parameters
----------
dts : npy_datetimestruct*
unix_date : int64_t
days elapsed since datetime(1970, 1, 1)
abstime : double
seconds elapsed since beginning of day described by unix_date
Notes
-----
Updates dts inplace
"""
cdef:
int inttime
int hour, minute
double second, subsecond_fraction
# Bounds check
# The calling function is responsible for ensuring that
# abstime >= 0.0 and abstime <= 86400
# Calculate the date
pandas_datetime_to_datetimestruct(unix_date, NPY_FR_D, dts)
# Calculate the time
inttime = <int>abstime
hour = inttime / 3600
minute = (inttime % 3600) / 60
second = abstime - <double>(hour * 3600 + minute * 60)
dts.hour = hour
dts.min = minute
dts.sec = <int>second
subsecond_fraction = second - dts.sec
dts.us = int((subsecond_fraction) * 1e6)
dts.ps = int(((subsecond_fraction) * 1e6 - dts.us) * 1e6)
@cython.cdivision
cdef double get_abs_time(int freq, int64_t unix_date, int64_t ordinal) nogil:
cdef:
int freq_index, day_index, base_index
int64_t per_day, start_ord
double unit, result
if freq <= FR_DAY:
return 0
freq_index = freq // 1000
day_index = FR_DAY // 1000
base_index = FR_SEC // 1000
per_day = get_daytime_conversion_factor(day_index, freq_index)
unit = get_daytime_conversion_factor(freq_index, base_index)
if base_index < freq_index:
unit = 1 / unit
start_ord = unix_date * per_day
result = <double>(unit * (ordinal - start_ord))
return result
cdef int get_yq(int64_t ordinal, int freq, int *quarter, int *year):
"""
Find the year and quarter of a Period with the given ordinal and frequency
Parameters
----------
ordinal : int64_t
freq : int
quarter : *int
year : *int
Returns
-------
qtr_freq : int
describes the implied quarterly frequency associated with `freq`
Notes
-----
Sets quarter and year inplace
"""
cdef:
asfreq_info af_info
int qtr_freq
int64_t unix_date
unix_date = get_unix_date(ordinal, freq)
if get_freq_group(freq) == FR_QTR:
qtr_freq = freq
else:
qtr_freq = FR_QTR
assert (qtr_freq % 1000) <= 12
get_asfreq_info(FR_DAY, qtr_freq, True, &af_info)
quarter[0] = DtoQ_yq(unix_date, &af_info, year)
return qtr_freq
cdef inline int month_to_quarter(int month) nogil:
return (month - 1) // 3 + 1
# ----------------------------------------------------------------------
# Period logic
@cython.wraparound(False)
@cython.boundscheck(False)
def dt64arr_to_periodarr(int64_t[:] dtarr, int freq, tz=None):
"""
Convert array of datetime64 values (passed in as 'i8' dtype) to a set of
periods corresponding to desired frequency, per period convention.
"""
cdef:
int64_t[:] out
Py_ssize_t i, l
npy_datetimestruct dts
l = len(dtarr)
out = np.empty(l, dtype='i8')
if tz is None: