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