@@ -22,6 +22,10 @@ from np_datetime cimport (pandas_datetimestruct, pandas_timedeltastruct,
22
22
days_per_month_table, is_leapyear, dayofweek)
23
23
from nattype cimport NPY_NAT
24
24
25
+ from pandas._libs.tslibs.strptime import LocaleTime
26
+
27
+ import locale
28
+ from pandas.util.testing import set_locale
25
29
26
30
def build_field_sarray (ndarray[int64_t] dtindex ):
27
31
"""
@@ -67,7 +71,8 @@ def build_field_sarray(ndarray[int64_t] dtindex):
67
71
68
72
@ cython.wraparound (False )
69
73
@ cython.boundscheck (False )
70
- def get_date_name_field (ndarray[int64_t] dtindex , object field ):
74
+ def get_date_name_field (ndarray[int64_t] dtindex , object field ,
75
+ object time_locale = None ):
71
76
"""
72
77
Given a int64-based datetime index, return array of strings of date
73
78
name based on requested field (e.g. weekday_name)
@@ -77,16 +82,15 @@ def get_date_name_field(ndarray[int64_t] dtindex, object field):
77
82
ndarray[object ] out
78
83
pandas_datetimestruct dts
79
84
int dow
80
-
81
- _dayname = np.array(
82
- [' Monday' , ' Tuesday' , ' Wednesday' , ' Thursday' ,
83
- ' Friday' , ' Saturday' , ' Sunday' ],
84
- dtype = np.object_)
85
+ object locale_time = LocaleTime()
85
86
86
87
count = len (dtindex)
87
88
out = np.empty(count, dtype = object )
88
89
89
90
if field == ' weekday_name' :
91
+ _dayname = np.array([' Monday' , ' Tuesday' , ' Wednesday' , ' Thursday' ,
92
+ ' Friday' , ' Saturday' , ' Sunday' ],
93
+ dtype = np.object_)
90
94
for i in range (count):
91
95
if dtindex[i] == NPY_NAT:
92
96
out[i] = np.nan
@@ -95,6 +99,40 @@ def get_date_name_field(ndarray[int64_t] dtindex, object field):
95
99
dt64_to_dtstruct(dtindex[i], & dts)
96
100
dow = dayofweek(dts.year, dts.month, dts.day)
97
101
out[i] = _dayname[dow]
102
+ if field == ' day_name' :
103
+ if time_locale is None :
104
+ _dayname = np.array([' monday' , ' tuesday' , ' wednesday' , ' thursday' ,
105
+ ' friday' , ' saturday' , ' sunday' ],
106
+ dtype = np.object_)
107
+ else :
108
+ with set_locale(time_locale, locale.LC_TIME):
109
+ locale_time = LocaleTime()
110
+ _dayname = np.array(locale_time.f_weekday, dtype = np.object_)
111
+ for i in range (count):
112
+ if dtindex[i] == NPY_NAT:
113
+ out[i] = np.nan
114
+ continue
115
+
116
+ dt64_to_dtstruct(dtindex[i], & dts)
117
+ dow = dayofweek(dts.year, dts.month, dts.day)
118
+ out[i] = _dayname[dow].capitalize()
119
+ return out
120
+ elif field == ' month_name' :
121
+ if time_locale is None :
122
+ _monthname = np.array([' ' , ' monday' , ' tuesday' , ' wednesday' ,
123
+ ' thursday' , ' friday' , ' saturday' , ' sunday' ],
124
+ dtype = np.object_)
125
+ else :
126
+ with set_locale(time_locale, locale.LC_TIME):
127
+ locale_time = LocaleTime()
128
+ _monthname = np.array(locale_time.f_month, dtype = np.object_)
129
+ for i in range (count):
130
+ if dtindex[i] == NPY_NAT:
131
+ out[i] = np.nan
132
+ continue
133
+
134
+ dt64_to_dtstruct(dtindex[i], & dts)
135
+ out[i] = _monthname[dts.month].capitalize()
98
136
return out
99
137
100
138
raise ValueError (" Field %s not supported" % field)
0 commit comments