@@ -54,6 +54,7 @@ from pandas._libs.tslibs.ccalendar cimport (
54
54
get_days_in_month,
55
55
)
56
56
from pandas._libs.tslibs.ccalendar cimport c_MONTH_NUMBERS
57
+ from pandas._libs.tslibs.conversion import ensure_datetime64ns
57
58
58
59
from pandas._libs.tslibs.dtypes cimport (
59
60
PeriodDtypeBase,
@@ -943,14 +944,34 @@ def periodarr_to_dt64arr(const int64_t[:] periodarr, int freq):
943
944
int64_t[:] out
944
945
Py_ssize_t i, l
945
946
946
- l = len (periodarr)
947
+ if freq < 6000 : # i.e. FR_DAY, hard-code to avoid need to cast
948
+ l = len (periodarr)
949
+ out = np.empty(l, dtype = " i8" )
947
950
948
- out = np.empty(l, dtype = ' i8' )
951
+ # We get here with freqs that do not correspond to a datetime64 unit
952
+ for i in range (l):
953
+ out[i] = period_ordinal_to_dt64(periodarr[i], freq)
949
954
950
- for i in range (l):
951
- out[i] = period_ordinal_to_dt64(periodarr[i], freq)
955
+ return out.base # .base to access underlying np.ndarray
952
956
953
- return out.base # .base to access underlying np.ndarray
957
+ else :
958
+ # Short-circuit for performance
959
+ if freq == FR_NS:
960
+ return periodarr.base
961
+
962
+ if freq == FR_US:
963
+ dta = periodarr.base.view(" M8[us]" )
964
+ elif freq == FR_MS:
965
+ dta = periodarr.base.view(" M8[ms]" )
966
+ elif freq == FR_SEC:
967
+ dta = periodarr.base.view(" M8[s]" )
968
+ elif freq == FR_MIN:
969
+ dta = periodarr.base.view(" M8[m]" )
970
+ elif freq == FR_HR:
971
+ dta = periodarr.base.view(" M8[h]" )
972
+ elif freq == FR_DAY:
973
+ dta = periodarr.base.view(" M8[D]" )
974
+ return ensure_datetime64ns(dta)
954
975
955
976
956
977
cpdef int64_t period_asfreq(int64_t ordinal, int freq1, int freq2, bint end):
0 commit comments