@@ -17,7 +17,8 @@ from numpy cimport ndarray, int64_t, int32_t, int8_t
17
17
np.import_array()
18
18
19
19
20
- from np_datetime cimport pandas_datetimestruct, dt64_to_dtstruct
20
+ from np_datetime cimport (pandas_datetimestruct, pandas_timedeltastruct,
21
+ dt64_to_dtstruct, td64_to_tdstruct)
21
22
22
23
from datetime cimport (
23
24
days_per_month_table,
@@ -545,6 +546,123 @@ def get_date_field(ndarray[int64_t] dtindex, object field):
545
546
raise ValueError (" Field %s not supported" % field)
546
547
547
548
549
+ @ cython.wraparound (False )
550
+ @ cython.boundscheck (False )
551
+ def get_timedelta_field (ndarray[int64_t] tdindex , object field ):
552
+ """
553
+ Given a int64-based timedelta index, extract the days, hrs, sec.,
554
+ field and return an array of these values.
555
+ """
556
+ cdef:
557
+ Py_ssize_t i, count = 0
558
+ ndarray[int32_t] out
559
+ pandas_timedeltastruct tds
560
+
561
+ count = len (tdindex)
562
+ out = np.empty(count, dtype = ' i4' )
563
+
564
+ if field == ' days' :
565
+ with nogil:
566
+ for i in range (count):
567
+ if tdindex[i] == NPY_NAT:
568
+ out[i] = - 1
569
+ continue
570
+
571
+ td64_to_tdstruct(tdindex[i], & tds)
572
+ out[i] = tds.days
573
+ return out
574
+
575
+ elif field == ' h' :
576
+ with nogil:
577
+ for i in range (count):
578
+ if tdindex[i] == NPY_NAT:
579
+ out[i] = - 1
580
+ continue
581
+
582
+ td64_to_tdstruct(tdindex[i], & tds)
583
+ out[i] = tds.hrs
584
+ return out
585
+
586
+ elif field == ' s' :
587
+ with nogil:
588
+ for i in range (count):
589
+ if tdindex[i] == NPY_NAT:
590
+ out[i] = - 1
591
+ continue
592
+
593
+ td64_to_tdstruct(tdindex[i], & tds)
594
+ out[i] = tds.sec
595
+ return out
596
+
597
+ elif field == ' seconds' :
598
+ with nogil:
599
+ for i in range (count):
600
+ if tdindex[i] == NPY_NAT:
601
+ out[i] = - 1
602
+ continue
603
+
604
+ td64_to_tdstruct(tdindex[i], & tds)
605
+ out[i] = tds.seconds
606
+ return out
607
+
608
+ elif field == ' ms' :
609
+ with nogil:
610
+ for i in range (count):
611
+ if tdindex[i] == NPY_NAT:
612
+ out[i] = - 1
613
+ continue
614
+
615
+ td64_to_tdstruct(tdindex[i], & tds)
616
+ out[i] = tds.ms
617
+ return out
618
+
619
+ elif field == ' microseconds' :
620
+ with nogil:
621
+ for i in range (count):
622
+ if tdindex[i] == NPY_NAT:
623
+ out[i] = - 1
624
+ continue
625
+
626
+ td64_to_tdstruct(tdindex[i], & tds)
627
+ out[i] = tds.microseconds
628
+ return out
629
+
630
+ elif field == ' us' :
631
+ with nogil:
632
+ for i in range (count):
633
+ if tdindex[i] == NPY_NAT:
634
+ out[i] = - 1
635
+ continue
636
+
637
+ td64_to_tdstruct(tdindex[i], & tds)
638
+ out[i] = tds.us
639
+ return out
640
+
641
+ elif field == ' ns' :
642
+ with nogil:
643
+ for i in range (count):
644
+ if tdindex[i] == NPY_NAT:
645
+ out[i] = - 1
646
+ continue
647
+
648
+ td64_to_tdstruct(tdindex[i], & tds)
649
+ out[i] = tds.ns
650
+ return out
651
+
652
+ elif field == ' nanoseconds' :
653
+ with nogil:
654
+ for i in range (count):
655
+ if tdindex[i] == NPY_NAT:
656
+ out[i] = - 1
657
+ continue
658
+
659
+ td64_to_tdstruct(tdindex[i], & tds)
660
+ out[i] = tds.nanoseconds
661
+ return out
662
+
663
+ raise ValueError (" Field %s not supported" % field)
664
+
665
+
548
666
cdef inline int days_in_month(pandas_datetimestruct dts) nogil:
549
667
return days_per_month_table[is_leapyear(dts.year)][dts.month - 1 ]
550
668
0 commit comments