@@ -89,14 +89,12 @@ static npy_int64 daytime_conversion_factor_matrix[7][7] = {
89
89
{0 , 0 , 0 , 0 , 0 , 1 , 1000 },
90
90
{0 , 0 , 0 , 0 , 0 , 0 , 1 }};
91
91
92
- PANDAS_INLINE int max_value (int a , int b ) { return a > b ? a : b ; }
92
+ int max_value (int a , int b ) { return a > b ? a : b ; }
93
93
94
94
PANDAS_INLINE int min_value (int a , int b ) { return a < b ? a : b ; }
95
95
96
96
PANDAS_INLINE int get_freq_group (int freq ) { return (freq / 1000 ) * 1000 ; }
97
97
98
- PANDAS_INLINE int get_freq_group_index (int freq ) { return freq / 1000 ; }
99
-
100
98
101
99
npy_int64 get_daytime_conversion_factor (int from_index , int to_index ) {
102
100
int row = min_value (from_index , to_index );
@@ -227,16 +225,6 @@ static npy_int64 asfreq_DTtoB(npy_int64 ordinal, asfreq_info *af_info) {
227
225
return DtoB (& dinfo , roll_back , ordinal );
228
226
}
229
227
230
- // all intra day calculations are now done within one function
231
- static npy_int64 asfreq_DownsampleWithinDay (npy_int64 ordinal ,
232
- asfreq_info * af_info ) {
233
- return downsample_daytime (ordinal , af_info );
234
- }
235
-
236
- static npy_int64 asfreq_UpsampleWithinDay (npy_int64 ordinal ,
237
- asfreq_info * af_info ) {
238
- return upsample_daytime (ordinal , af_info );
239
- }
240
228
//************ FROM BUSINESS ***************
241
229
242
230
static npy_int64 asfreq_BtoDT (npy_int64 ordinal , asfreq_info * af_info ) {
@@ -288,26 +276,26 @@ static npy_int64 asfreq_WtoW(npy_int64 ordinal, asfreq_info *af_info) {
288
276
static npy_int64 asfreq_WtoB (npy_int64 ordinal , asfreq_info * af_info ) {
289
277
struct date_info dinfo ;
290
278
npy_int64 unix_date = asfreq_WtoDT (ordinal , af_info );
279
+
291
280
int roll_back = af_info -> is_end ;
292
281
dInfoCalc_SetFromAbsDate (& dinfo , unix_date );
293
-
294
282
return DtoB (& dinfo , roll_back , unix_date );
295
283
}
296
284
297
285
//************ FROM MONTHLY ***************
298
- static void MtoD_ym (npy_int64 ordinal , int * y , int * m ) {
299
- * y = floordiv (ordinal , 12 ) + 1970 ;
300
- * m = mod_compat (ordinal , 12 ) + 1 ;
286
+ static void MtoD_ym (npy_int64 ordinal , int * year , int * month ) {
287
+ * year = floordiv (ordinal , 12 ) + 1970 ;
288
+ * month = mod_compat (ordinal , 12 ) + 1 ;
301
289
}
302
290
303
291
static npy_int64 asfreq_MtoDT (npy_int64 ordinal , asfreq_info * af_info ) {
304
292
npy_int64 unix_date ;
305
- int y , m ;
293
+ int year , month ;
306
294
307
295
ordinal += af_info -> is_end ;
308
- MtoD_ym (ordinal , & y , & m );
309
- unix_date = unix_date_from_ymd (y , m , 1 );
296
+ MtoD_ym (ordinal , & year , & month );
310
297
298
+ unix_date = unix_date_from_ymd (year , month , 1 );
311
299
unix_date -= af_info -> is_end ;
312
300
return upsample_daytime (unix_date , af_info );
313
301
}
@@ -327,38 +315,37 @@ static npy_int64 asfreq_MtoW(npy_int64 ordinal, asfreq_info *af_info) {
327
315
static npy_int64 asfreq_MtoB (npy_int64 ordinal , asfreq_info * af_info ) {
328
316
struct date_info dinfo ;
329
317
npy_int64 unix_date = asfreq_MtoDT (ordinal , af_info );
330
- int roll_back = af_info -> is_end ;
331
318
319
+ int roll_back = af_info -> is_end ;
332
320
dInfoCalc_SetFromAbsDate (& dinfo , unix_date );
333
-
334
321
return DtoB (& dinfo , roll_back , unix_date );
335
322
}
336
323
337
324
//************ FROM QUARTERLY ***************
338
325
339
- static void QtoD_ym (npy_int64 ordinal , int * y , int * m , asfreq_info * af_info ) {
340
- * y = floordiv (ordinal , 4 ) + 1970 ;
341
- * m = mod_compat (ordinal , 4 ) * 3 + 1 ;
326
+ static void QtoD_ym (npy_int64 ordinal , int * year , int * month ,
327
+ asfreq_info * af_info ) {
328
+ * year = floordiv (ordinal , 4 ) + 1970 ;
329
+ * month = mod_compat (ordinal , 4 ) * 3 + 1 ;
342
330
343
331
if (af_info -> from_q_year_end != 12 ) {
344
- * m += af_info -> from_q_year_end ;
345
- if (* m > 12 ) {
346
- * m -= 12 ;
332
+ * month += af_info -> from_q_year_end ;
333
+ if (* month > 12 ) {
334
+ * month -= 12 ;
347
335
} else {
348
- * y -= 1 ;
336
+ * year -= 1 ;
349
337
}
350
338
}
351
339
}
352
340
353
341
static npy_int64 asfreq_QtoDT (npy_int64 ordinal , asfreq_info * af_info ) {
354
342
npy_int64 unix_date ;
355
- int y , m ;
343
+ int year , month ;
356
344
357
345
ordinal += af_info -> is_end ;
358
- QtoD_ym (ordinal , & y , & m , af_info );
359
-
360
- unix_date = unix_date_from_ymd (y , m , 1 );
346
+ QtoD_ym (ordinal , & year , & month , af_info );
361
347
348
+ unix_date = unix_date_from_ymd (year , month , 1 );
362
349
unix_date -= af_info -> is_end ;
363
350
return upsample_daytime (unix_date , af_info );
364
351
}
@@ -382,29 +369,39 @@ static npy_int64 asfreq_QtoW(npy_int64 ordinal, asfreq_info *af_info) {
382
369
static npy_int64 asfreq_QtoB (npy_int64 ordinal , asfreq_info * af_info ) {
383
370
struct date_info dinfo ;
384
371
npy_int64 unix_date = asfreq_QtoDT (ordinal , af_info );
385
- int roll_back = af_info -> is_end ;
386
372
373
+ int roll_back = af_info -> is_end ;
387
374
dInfoCalc_SetFromAbsDate (& dinfo , unix_date );
388
-
389
375
return DtoB (& dinfo , roll_back , unix_date );
390
376
}
391
377
392
378
//************ FROM ANNUAL ***************
393
379
394
- static npy_int64 asfreq_AtoDT (npy_int64 ordinal , asfreq_info * af_info ) {
395
- npy_int64 unix_date ;
380
+ static void AtoD_ym (npy_int64 ordinal , int * year , int * month ,
381
+ asfreq_info * af_info ) {
382
+ * year = ordinal + 1970 ;
383
+ * month = 1 ;
396
384
397
- // start from 1970
398
- npy_int64 year = ordinal + 1970 ;
399
-
400
- int month = (af_info -> from_a_year_end % 12 ) + 1 ;
401
385
if (af_info -> from_a_year_end != 12 ) {
402
- year -= 1 ;
386
+ * month += af_info -> from_a_year_end ;
387
+ if (* month > 12 ) {
388
+ // This case is never reached, but is kept for symmetry
389
+ // with QtoD_ym
390
+ * month -= 12 ;
391
+ } else {
392
+ * year -= 1 ;
393
+ }
403
394
}
395
+ }
404
396
405
- year += af_info -> is_end ;
406
- unix_date = unix_date_from_ymd (year , month , 1 );
397
+ static npy_int64 asfreq_AtoDT (npy_int64 ordinal , asfreq_info * af_info ) {
398
+ npy_int64 unix_date ;
399
+ int year , month ;
400
+
401
+ ordinal += af_info -> is_end ;
402
+ AtoD_ym (ordinal , & year , & month , af_info );
407
403
404
+ unix_date = unix_date_from_ymd (year , month , 1 );
408
405
unix_date -= af_info -> is_end ;
409
406
return upsample_daytime (unix_date , af_info );
410
407
}
@@ -428,9 +425,9 @@ static npy_int64 asfreq_AtoW(npy_int64 ordinal, asfreq_info *af_info) {
428
425
static npy_int64 asfreq_AtoB (npy_int64 ordinal , asfreq_info * af_info ) {
429
426
struct date_info dinfo ;
430
427
npy_int64 unix_date = asfreq_AtoDT (ordinal , af_info );
428
+
431
429
int roll_back = af_info -> is_end ;
432
430
dInfoCalc_SetFromAbsDate (& dinfo , unix_date );
433
-
434
431
return DtoB (& dinfo , roll_back , unix_date );
435
432
}
436
433
@@ -443,57 +440,6 @@ static npy_int64 no_op(npy_int64 ordinal, asfreq_info *af_info) {
443
440
444
441
// end of frequency specific conversion routines
445
442
446
- static int calc_a_year_end (int freq , int group ) {
447
- int result = (freq - group ) % 12 ;
448
- if (result == 0 ) {
449
- return 12 ;
450
- } else {
451
- return result ;
452
- }
453
- }
454
-
455
- static int calc_week_end (int freq , int group ) { return freq - group ; }
456
-
457
- void get_asfreq_info (int fromFreq , int toFreq , char relation ,
458
- asfreq_info * af_info ) {
459
- int fromGroup = get_freq_group (fromFreq );
460
- int toGroup = get_freq_group (toFreq );
461
-
462
- if (relation == 'E' ) {
463
- af_info -> is_end = 1 ;
464
- } else {
465
- af_info -> is_end = 0 ;
466
- }
467
-
468
- af_info -> intraday_conversion_factor = get_daytime_conversion_factor (
469
- get_freq_group_index (max_value (fromGroup , FR_DAY )),
470
- get_freq_group_index (max_value (toGroup , FR_DAY )));
471
-
472
- switch (fromGroup ) {
473
- case FR_WK :
474
- af_info -> from_week_end = calc_week_end (fromFreq , fromGroup );
475
- break ;
476
- case FR_ANN :
477
- af_info -> from_a_year_end = calc_a_year_end (fromFreq , fromGroup );
478
- break ;
479
- case FR_QTR :
480
- af_info -> from_q_year_end = calc_a_year_end (fromFreq , fromGroup );
481
- break ;
482
- }
483
-
484
- switch (toGroup ) {
485
- case FR_WK :
486
- af_info -> to_week_end = calc_week_end (toFreq , toGroup );
487
- break ;
488
- case FR_ANN :
489
- af_info -> to_a_year_end = calc_a_year_end (toFreq , toGroup );
490
- break ;
491
- case FR_QTR :
492
- af_info -> to_q_year_end = calc_a_year_end (toFreq , toGroup );
493
- break ;
494
- }
495
- }
496
-
497
443
freq_conv_func get_asfreq_func (int fromFreq , int toFreq ) {
498
444
int fromGroup = get_freq_group (fromFreq );
499
445
int toGroup = get_freq_group (toFreq );
@@ -650,9 +596,9 @@ freq_conv_func get_asfreq_func(int fromFreq, int toFreq) {
650
596
case FR_US :
651
597
case FR_NS :
652
598
if (fromGroup > toGroup ) {
653
- return & asfreq_DownsampleWithinDay ;
599
+ return & downsample_daytime ;
654
600
} else {
655
- return & asfreq_UpsampleWithinDay ;
601
+ return & upsample_daytime ;
656
602
}
657
603
default :
658
604
return & nofunc ;
@@ -662,20 +608,3 @@ freq_conv_func get_asfreq_func(int fromFreq, int toFreq) {
662
608
return & nofunc ;
663
609
}
664
610
}
665
-
666
- /* ------------------------------------------------------------------
667
- * New pandas API-helper code, to expose to cython
668
- * ------------------------------------------------------------------*/
669
-
670
- npy_int64 asfreq (npy_int64 period_ordinal , int freq1 , int freq2 ,
671
- char relation ) {
672
- npy_int64 val ;
673
- freq_conv_func func ;
674
- asfreq_info finfo ;
675
-
676
- func = get_asfreq_func (freq1 , freq2 );
677
-
678
- get_asfreq_info (freq1 , freq2 , relation , & finfo );
679
- val = (* func )(period_ordinal , & finfo );
680
- return val ;
681
- }
0 commit comments