forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperiod.pyx
3070 lines (2444 loc) · 93.5 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
import re
cimport numpy as cnp
from cpython.object cimport (
Py_EQ,
Py_NE,
PyObject,
PyObject_RichCompare,
PyObject_RichCompareBool,
)
from numpy cimport (
int32_t,
int64_t,
ndarray,
)
import numpy as np
cnp.import_array()
cimport cython
from cpython.datetime cimport (
PyDate_Check,
PyDateTime_Check,
datetime,
import_datetime,
)
from libc.stdlib cimport (
free,
malloc,
)
from libc.string cimport (
memset,
strlen,
)
from libc.time cimport (
strftime,
tm,
)
from pandas._libs.tslibs.dtypes cimport c_OFFSET_TO_PERIOD_FREQSTR
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
# import datetime C API
import_datetime()
cimport pandas._libs.tslibs.util as util
from pandas._libs.missing cimport C_NA
from pandas._libs.tslibs.np_datetime cimport (
NPY_DATETIMEUNIT,
NPY_FR_D,
astype_overflowsafe,
dts_to_iso_string,
import_pandas_datetime,
npy_datetimestruct,
npy_datetimestruct_to_datetime,
pandas_datetime_to_datetimestruct,
)
import_pandas_datetime()
from pandas._libs.tslibs.timestamps import Timestamp
from pandas._libs.tslibs.ccalendar cimport (
dayofweek,
get_day_of_year,
get_days_in_month,
get_week_of_year,
is_leapyear,
)
from pandas._libs.tslibs.timedeltas cimport (
delta_to_nanoseconds,
is_any_td_scalar,
)
from pandas._libs.tslibs.conversion import DT64NS_DTYPE
from pandas._libs.tslibs.dtypes cimport (
FR_ANN,
FR_BUS,
FR_DAY,
FR_HR,
FR_MIN,
FR_MS,
FR_MTH,
FR_NS,
FR_QTR,
FR_SEC,
FR_UND,
FR_US,
FR_WK,
PeriodDtypeBase,
attrname_to_abbrevs,
freq_group_code_to_npy_unit,
)
from pandas._libs.tslibs.parsing cimport quarter_to_myear
from pandas._libs.tslibs.parsing import parse_datetime_string_with_reso
from pandas._libs.tslibs.nattype cimport (
NPY_NAT,
c_NaT as NaT,
c_nat_strings as nat_strings,
checknull_with_nat,
)
from pandas._libs.tslibs.offsets cimport (
BaseOffset,
is_offset_object,
to_offset,
)
from pandas._libs.tslibs.offsets import (
INVALID_FREQ_ERR_MSG,
BDay,
)
from pandas.util._decorators import set_module
cdef:
enum:
INT32_MIN = -2_147_483_648LL
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*) noexcept nogil
cdef extern from *:
"""
// must use npy typedef b/c int64_t is aliased in cython-generated c
// unclear why we need LL for that row.
// see https://github.com/pandas-dev/pandas/pull/34416/
static npy_int64 daytime_conversion_factor_matrix[7][7] = {
{1, 24, 1440, 86400, 86400000, 86400000000, 86400000000000},
{0LL, 1LL, 60LL, 3600LL, 3600000LL, 3600000000LL, 3600000000000LL},
{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]
cdef int max_value(int left, int right) noexcept nogil:
if left > right:
return left
return right
cdef int min_value(int left, int right) noexcept nogil:
if left < right:
return left
return right
cdef int64_t get_daytime_conversion_factor(int from_index, int to_index) noexcept 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) noexcept nogil:
return INT32_MIN
cdef int64_t no_op(int64_t ordinal, asfreq_info *af_info) noexcept nogil:
return ordinal
cdef freq_conv_func get_asfreq_func(int from_freq, int to_freq) noexcept 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) noexcept 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) noexcept nogil:
# calculate the current week (counting from 1970-01-01) treating
# sunday as last day of a week
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 int64_t upsample_daytime(int64_t ordinal, asfreq_info *af_info) noexcept nogil:
if af_info.is_end:
return (ordinal + 1) * af_info.intraday_conversion_factor - 1
else:
return ordinal * af_info.intraday_conversion_factor
cdef int64_t downsample_daytime(int64_t ordinal, asfreq_info *af_info) noexcept nogil:
return ordinal // af_info.intraday_conversion_factor
cdef int64_t transform_via_day(int64_t ordinal,
asfreq_info *af_info,
freq_conv_func first_func,
freq_conv_func second_func) noexcept nogil:
cdef:
int64_t result
result = first_func(ordinal, af_info)
result = second_func(result, af_info)
return result
# --------------------------------------------------------------------
# Conversion _to_ Daily Freq
cdef int64_t asfreq_AtoDT(int64_t ordinal, asfreq_info *af_info) noexcept nogil:
cdef:
int64_t unix_date
npy_datetimestruct dts
ordinal += af_info.is_end
dts.year = ordinal + 1970
dts.month = 1
adjust_dts_for_month(&dts, af_info.from_end)
unix_date = unix_date_from_ymd(dts.year, dts.month, 1)
unix_date -= af_info.is_end
return upsample_daytime(unix_date, af_info)
cdef int64_t asfreq_QtoDT(int64_t ordinal, asfreq_info *af_info) noexcept nogil:
cdef:
int64_t unix_date
npy_datetimestruct dts
ordinal += af_info.is_end
dts.year = ordinal // 4 + 1970
dts.month = (ordinal % 4) * 3 + 1
adjust_dts_for_month(&dts, af_info.from_end)
unix_date = unix_date_from_ymd(dts.year, dts.month, 1)
unix_date -= af_info.is_end
return upsample_daytime(unix_date, af_info)
cdef int64_t asfreq_MtoDT(int64_t ordinal, asfreq_info *af_info) noexcept nogil:
cdef:
int64_t unix_date
int year, month
ordinal += af_info.is_end
year = ordinal // 12 + 1970
month = ordinal % 12 + 1
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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept nogil:
cdef:
npy_datetimestruct dts
ordinal = downsample_daytime(ordinal, af_info)
pandas_datetime_to_datetimestruct(ordinal, NPY_FR_D, &dts)
return dts_to_year_ordinal(&dts, af_info.to_end)
cdef int DtoQ_yq(int64_t ordinal, asfreq_info *af_info,
npy_datetimestruct* dts) noexcept nogil:
cdef:
int quarter
pandas_datetime_to_datetimestruct(ordinal, NPY_FR_D, dts)
adjust_dts_for_qtr(dts, af_info.to_end)
quarter = month_to_quarter(dts.month)
return quarter
cdef int64_t asfreq_DTtoQ(int64_t ordinal, asfreq_info *af_info) noexcept nogil:
cdef:
int quarter
npy_datetimestruct dts
ordinal = downsample_daytime(ordinal, af_info)
quarter = DtoQ_yq(ordinal, af_info, &dts)
return <int64_t>((dts.year - 1970) * 4 + quarter - 1)
cdef int64_t asfreq_DTtoM(int64_t ordinal, asfreq_info *af_info) noexcept nogil:
cdef:
npy_datetimestruct dts
ordinal = downsample_daytime(ordinal, af_info)
pandas_datetime_to_datetimestruct(ordinal, NPY_FR_D, &dts)
return dts_to_month_ordinal(&dts)
cdef int64_t asfreq_DTtoW(int64_t ordinal, asfreq_info *af_info) noexcept nogil:
ordinal = downsample_daytime(ordinal, af_info)
return unix_date_to_week(ordinal, af_info.to_end)
cdef int64_t unix_date_to_week(int64_t unix_date, int to_end) noexcept nogil:
return (unix_date + 3 - to_end) // 7 + 1
# --------------------------------------------------------------------
# Conversion _from_ BusinessDay Freq
cdef int64_t asfreq_BtoDT(int64_t ordinal, asfreq_info *af_info) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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) noexcept 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))
if result is NULL:
raise MemoryError()
strftime(result, result_len, fmt, &c_date)
return result
# ----------------------------------------------------------------------
# Conversion between date_info and npy_datetimestruct
cdef int get_freq_group(int freq) noexcept nogil:
# See also FreqGroup.get_freq_group
return (freq // 1000) * 1000
cdef int get_freq_group_index(int freq) noexcept nogil:
return freq // 1000
cdef void adjust_dts_for_month(npy_datetimestruct* dts, int from_end) noexcept nogil:
if from_end != 12:
dts.month += from_end
if dts.month > 12:
dts.month -= 12
else:
dts.year -= 1
cdef void adjust_dts_for_qtr(npy_datetimestruct* dts, int to_end) noexcept nogil:
if to_end != 12:
dts.month -= to_end
if dts.month <= 0:
dts.month += 12
else:
dts.year += 1
# 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) noexcept 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
cdef int64_t dts_to_month_ordinal(npy_datetimestruct* dts) noexcept nogil:
# AKA: use npy_datetimestruct_to_datetime(NPY_FR_M, &dts)
return <int64_t>((dts.year - 1970) * 12 + dts.month - 1)
cdef int64_t dts_to_year_ordinal(npy_datetimestruct *dts, int to_end) noexcept nogil:
cdef:
int64_t result
result = npy_datetimestruct_to_datetime(NPY_DATETIMEUNIT.NPY_FR_Y, dts)
if dts.month > to_end:
return result + 1
else:
return result
cdef int64_t dts_to_qtr_ordinal(npy_datetimestruct* dts, int to_end) noexcept nogil:
cdef:
int quarter
adjust_dts_for_qtr(dts, to_end)
quarter = month_to_quarter(dts.month)
return <int64_t>((dts.year - 1970) * 4 + quarter - 1)
cdef int get_anchor_month(int freq, int freq_group) noexcept nogil:
cdef:
int fmonth
fmonth = freq - freq_group
if fmonth == 0:
fmonth = 12
return fmonth
# 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) noexcept nogil:
"""
Generate an ordinal in period space
Parameters
----------
dts : npy_datetimestruct*
freq : int
Returns
-------
period_ordinal : int64_t
"""
cdef:
int64_t unix_date
int freq_group, fmonth
NPY_DATETIMEUNIT unit
freq_group = get_freq_group(freq)
if freq_group == FR_ANN:
fmonth = get_anchor_month(freq, freq_group)
return dts_to_year_ordinal(dts, fmonth)
elif freq_group == FR_QTR:
fmonth = get_anchor_month(freq, freq_group)
return dts_to_qtr_ordinal(dts, fmonth)
elif freq_group == FR_WK:
unix_date = npy_datetimestruct_to_datetime(NPY_FR_D, dts)
return unix_date_to_week(unix_date, freq - FR_WK)
elif freq == FR_BUS:
unix_date = npy_datetimestruct_to_datetime(NPY_FR_D, dts)
return DtoB(dts, 0, unix_date)
unit = freq_group_code_to_npy_unit(freq)
return npy_datetimestruct_to_datetime(unit, dts)
cdef void get_date_info(int64_t ordinal,
int freq, npy_datetimestruct *dts) noexcept nogil:
cdef:
int64_t unix_date, nanos
npy_datetimestruct dts2
unix_date = get_unix_date(ordinal, freq)
nanos = get_time_nanos(freq, unix_date, ordinal)
pandas_datetime_to_datetimestruct(unix_date, NPY_FR_D, dts)
pandas_datetime_to_datetimestruct(nanos, NPY_DATETIMEUNIT.NPY_FR_ns, &dts2)
dts.hour = dts2.hour
dts.min = dts2.min
dts.sec = dts2.sec
dts.us = dts2.us
dts.ps = dts2.ps
cdef int64_t get_unix_date(int64_t period_ordinal, int freq) noexcept 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 int64_t get_time_nanos(int freq, int64_t unix_date,
int64_t ordinal) noexcept nogil:
"""
Find the number of nanoseconds after midnight on the given unix_date
that the ordinal represents in the given frequency.
Parameters
----------
freq : int
unix_date : int64_t
ordinal : int64_t
Returns
-------
int64_t
"""
cdef:
int64_t sub, factor
int64_t nanos_in_day = 24 * 3600 * 10**9
freq = get_freq_group(freq)
if freq <= FR_DAY:
return 0
elif freq == FR_NS:
factor = 1
elif freq == FR_US:
factor = 10**3
elif freq == FR_MS:
factor = 10**6
elif freq == FR_SEC:
factor = 10 **9
elif freq == FR_MIN:
factor = 10**9 * 60
else:
# We must have freq == FR_HR
factor = 10**9 * 3600
sub = ordinal - unix_date * (nanos_in_day / factor)
return sub * factor
cdef int get_yq(int64_t ordinal, int freq, npy_datetimestruct* dts):
"""
Find the year and quarter of a Period with the given ordinal and frequency
Parameters
----------
ordinal : int64_t
freq : int
dts : *npy_datetimestruct
Returns
-------
quarter : int
describes the implied quarterly frequency associated with `freq`
Notes
-----
Sets dts.year in-place.
"""
cdef:
asfreq_info af_info
int qtr_freq
int64_t unix_date
int quarter
unix_date = get_unix_date(ordinal, freq)
if get_freq_group(freq) == FR_QTR:
qtr_freq = freq
else:
qtr_freq = FR_QTR
get_asfreq_info(FR_DAY, qtr_freq, True, &af_info)
quarter = DtoQ_yq(unix_date, &af_info, dts)
return quarter
cdef int month_to_quarter(int month) noexcept nogil:
return (month - 1) // 3 + 1
# ----------------------------------------------------------------------
# Period logic
@cython.wraparound(False)
@cython.boundscheck(False)
def periodarr_to_dt64arr(const int64_t[:] periodarr, int freq):
"""
Convert array to datetime64 values from a set of ordinals corresponding to
periods per period convention.
"""
cdef:
int64_t[::1] out
Py_ssize_t i, N
if freq < 6000: # i.e. FR_DAY, hard-code to avoid need to cast
N = len(periodarr)
out = np.empty(N, dtype="i8")
# We get here with freqs that do not correspond to a datetime64 unit
for i in range(N):
out[i] = period_ordinal_to_dt64(periodarr[i], freq)
return out.base # .base to access underlying np.ndarray
else:
# Short-circuit for performance
if freq == FR_NS:
# TODO: copy?
return periodarr.base
if freq == FR_US:
dta = periodarr.base.view("M8[us]")
elif freq == FR_MS:
dta = periodarr.base.view("M8[ms]")
elif freq == FR_SEC:
dta = periodarr.base.view("M8[s]")
elif freq == FR_MIN:
dta = periodarr.base.view("M8[m]")
elif freq == FR_HR:
dta = periodarr.base.view("M8[h]")
elif freq == FR_DAY:
dta = periodarr.base.view("M8[D]")
return astype_overflowsafe(dta, dtype=DT64NS_DTYPE)
cdef void get_asfreq_info(int from_freq, int to_freq,
bint is_end, asfreq_info *af_info) noexcept nogil:
"""