np_datetime.pxd 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. cimport numpy as cnp
  2. from cpython.datetime cimport (
  3. date,
  4. datetime,
  5. )
  6. from numpy cimport (
  7. int32_t,
  8. int64_t,
  9. )
  10. # TODO(cython3): most of these can be cimported directly from numpy
  11. cdef extern from "numpy/ndarrayobject.h":
  12. ctypedef int64_t npy_timedelta
  13. ctypedef int64_t npy_datetime
  14. cdef extern from "numpy/ndarraytypes.h":
  15. ctypedef struct PyArray_DatetimeMetaData:
  16. NPY_DATETIMEUNIT base
  17. int64_t num
  18. cdef extern from "numpy/arrayscalars.h":
  19. ctypedef struct PyDatetimeScalarObject:
  20. # PyObject_HEAD
  21. npy_datetime obval
  22. PyArray_DatetimeMetaData obmeta
  23. ctypedef struct PyTimedeltaScalarObject:
  24. # PyObject_HEAD
  25. npy_timedelta obval
  26. PyArray_DatetimeMetaData obmeta
  27. cdef extern from "numpy/ndarraytypes.h":
  28. ctypedef struct npy_datetimestruct:
  29. int64_t year
  30. int32_t month, day, hour, min, sec, us, ps, as
  31. ctypedef enum NPY_DATETIMEUNIT:
  32. NPY_FR_Y
  33. NPY_FR_M
  34. NPY_FR_W
  35. NPY_FR_D
  36. NPY_FR_B
  37. NPY_FR_h
  38. NPY_FR_m
  39. NPY_FR_s
  40. NPY_FR_ms
  41. NPY_FR_us
  42. NPY_FR_ns
  43. NPY_FR_ps
  44. NPY_FR_fs
  45. NPY_FR_as
  46. NPY_FR_GENERIC
  47. int64_t NPY_DATETIME_NAT # elswhere we call this NPY_NAT
  48. cdef extern from "src/datetime/np_datetime.h":
  49. ctypedef struct pandas_timedeltastruct:
  50. int64_t days
  51. int32_t hrs, min, sec, ms, us, ns, seconds, microseconds, nanoseconds
  52. void pandas_datetime_to_datetimestruct(npy_datetime val,
  53. NPY_DATETIMEUNIT fr,
  54. npy_datetimestruct *result) nogil
  55. npy_datetime npy_datetimestruct_to_datetime(NPY_DATETIMEUNIT fr,
  56. npy_datetimestruct *d) nogil
  57. void pandas_timedelta_to_timedeltastruct(npy_timedelta val,
  58. NPY_DATETIMEUNIT fr,
  59. pandas_timedeltastruct *result
  60. ) nogil
  61. cdef bint cmp_scalar(int64_t lhs, int64_t rhs, int op) except -1
  62. cdef check_dts_bounds(npy_datetimestruct *dts, NPY_DATETIMEUNIT unit=?)
  63. cdef int64_t pydatetime_to_dt64(
  64. datetime val, npy_datetimestruct *dts, NPY_DATETIMEUNIT reso=?
  65. )
  66. cdef void pydatetime_to_dtstruct(datetime dt, npy_datetimestruct *dts)
  67. cdef int64_t pydate_to_dt64(
  68. date val, npy_datetimestruct *dts, NPY_DATETIMEUNIT reso=?
  69. )
  70. cdef void pydate_to_dtstruct(date val, npy_datetimestruct *dts)
  71. cdef npy_datetime get_datetime64_value(object obj) nogil
  72. cdef npy_timedelta get_timedelta64_value(object obj) nogil
  73. cdef NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil
  74. cdef int string_to_dts(
  75. str val,
  76. npy_datetimestruct* dts,
  77. NPY_DATETIMEUNIT* out_bestunit,
  78. int* out_local,
  79. int* out_tzoffset,
  80. bint want_exc,
  81. format: str | None = *,
  82. bint exact = *
  83. ) except? -1
  84. cdef NPY_DATETIMEUNIT get_unit_from_dtype(cnp.dtype dtype)
  85. cpdef cnp.ndarray astype_overflowsafe(
  86. cnp.ndarray values, # ndarray[datetime64[anyunit]]
  87. cnp.dtype dtype, # ndarray[datetime64[anyunit]]
  88. bint copy=*,
  89. bint round_ok=*,
  90. bint is_coerce=*,
  91. )
  92. cdef int64_t get_conversion_factor(
  93. NPY_DATETIMEUNIT from_unit,
  94. NPY_DATETIMEUNIT to_unit,
  95. ) except? -1
  96. cdef bint cmp_dtstructs(npy_datetimestruct* left, npy_datetimestruct* right, int op)
  97. cdef get_implementation_bounds(
  98. NPY_DATETIMEUNIT reso, npy_datetimestruct *lower, npy_datetimestruct *upper
  99. )
  100. cdef int64_t convert_reso(
  101. int64_t value,
  102. NPY_DATETIMEUNIT from_reso,
  103. NPY_DATETIMEUNIT to_reso,
  104. bint round_ok,
  105. ) except? -1
  106. cdef extern from "src/datetime/np_datetime_strings.h":
  107. ctypedef enum FormatRequirement:
  108. PARTIAL_MATCH
  109. EXACT_MATCH
  110. INFER_FORMAT