Skip to content

Commit 6f4a962

Browse files
committed
address review
1 parent a40aa16 commit 6f4a962

File tree

6 files changed

+81
-30
lines changed

6 files changed

+81
-30
lines changed

pandas/_libs/tslibs/ccalendar.pyx

+5-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ cimport numpy as cnp
1212
from numpy cimport int64_t, int32_t
1313
cnp.import_array()
1414

15-
import locale
15+
from locale import LC_TIME
1616
from strptime import LocaleTime
1717

1818
# ----------------------------------------------------------------------
@@ -210,7 +210,7 @@ cpdef int32_t get_day_of_year(int year, int month, int day) nogil:
210210
return day_of_year
211211

212212

213-
cpdef get_locale_names(object name_type, object time_locale=None):
213+
cpdef get_locale_names(object name_type, object locale=None):
214214
"""Returns an array of localized day or month names
215215
216216
Parameters
@@ -221,14 +221,10 @@ cpdef get_locale_names(object name_type, object time_locale=None):
221221
222222
Returns
223223
-------
224-
names : list
224+
list of locale names
225225
226226
"""
227-
cdef:
228-
list locale_names
229-
230227
from pandas.util.testing import set_locale
231228

232-
with set_locale(time_locale, locale.LC_TIME):
233-
locale_names = getattr(LocaleTime(), name_type)
234-
return locale_names
229+
with set_locale(locale, LC_TIME):
230+
return getattr(LocaleTime(), name_type)

pandas/_libs/tslibs/fields.pyx

+10-10
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,25 @@ def build_field_sarray(ndarray[int64_t] dtindex):
8686
@cython.wraparound(False)
8787
@cython.boundscheck(False)
8888
def get_date_name_field(ndarray[int64_t] dtindex, object field,
89-
object time_locale=None):
89+
object locale=None):
9090
"""
9191
Given a int64-based datetime index, return array of strings of date
9292
name based on requested field (e.g. weekday_name)
9393
"""
9494
cdef:
9595
Py_ssize_t i, count = 0
96-
ndarray[object] out
96+
ndarray[object] out, name
9797
pandas_datetimestruct dts
9898
int dow
9999

100100
count = len(dtindex)
101101
out = np.empty(count, dtype=object)
102102

103103
if field == 'day_name' or field == 'weekday_name':
104-
if time_locale is None:
105-
_dayname = np.array(DAYS_FULL, dtype=np.object_)
104+
if locale is None:
105+
names = np.array(DAYS_FULL, dtype=np.object_)
106106
else:
107-
_dayname = np.array(get_locale_names('f_weekday', time_locale),
107+
names = np.array(get_locale_names('f_weekday', locale),
108108
dtype=np.object_)
109109
for i in range(count):
110110
if dtindex[i] == NPY_NAT:
@@ -113,21 +113,21 @@ def get_date_name_field(ndarray[int64_t] dtindex, object field,
113113

114114
dt64_to_dtstruct(dtindex[i], &dts)
115115
dow = dayofweek(dts.year, dts.month, dts.day)
116-
out[i] = _dayname[dow].capitalize()
116+
out[i] = names[dow].capitalize()
117117
return out
118118
elif field == 'month_name':
119-
if time_locale is None:
120-
_monthname = np.array(MONTHS_FULL, dtype=np.object_)
119+
if locale is None:
120+
names = np.array(MONTHS_FULL, dtype=np.object_)
121121
else:
122-
_monthname = np.array(get_locale_names('f_month', time_locale),
122+
names = np.array(get_locale_names('f_month', locale),
123123
dtype=np.object_)
124124
for i in range(count):
125125
if dtindex[i] == NPY_NAT:
126126
out[i] = np.nan
127127
continue
128128

129129
dt64_to_dtstruct(dtindex[i], &dts)
130-
out[i] = _monthname[dts.month].capitalize()
130+
out[i] = names[dts.month].capitalize()
131131
return out
132132

133133
raise ValueError("Field %s not supported" % field)

pandas/_libs/tslibs/nattype.pyx

+6-2
Original file line numberDiff line numberDiff line change
@@ -320,25 +320,29 @@ class NaTType(_NaT):
320320
321321
Parameters
322322
----------
323-
time_locale : string, default None (English locale)
323+
locale : string, default None (English locale)
324324
locale determining the language in which to return the month name
325325
326326
Returns
327327
-------
328328
month_name : string
329+
330+
.. versionadded:: 0.23.0
329331
""")
330332
day_name = _make_nan_func('day_name', # noqa:E128
331333
"""
332334
Return the day name of the Timestamp with specified locale.
333335
334336
Parameters
335337
----------
336-
time_locale : string, default None (English locale)
338+
locale : string, default None (English locale)
337339
locale determining the language in which to return the day name
338340
339341
Returns
340342
-------
341343
day_name : string
344+
345+
.. versionadded:: 0.23.0
342346
""")
343347
# _nat_methods
344348
date = _make_nat_func('date', datetime.date.__doc__)

pandas/core/indexes/datetimes.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def _add_comparison_methods(cls):
322322
_datetimelike_methods = ['to_period', 'tz_localize',
323323
'tz_convert',
324324
'normalize', 'strftime', 'round', 'floor',
325-
'ceil']
325+
'ceil', 'month_name', 'day_name']
326326

327327
_is_numeric_dtype = False
328328
_infer_as_myclass = True
@@ -2111,51 +2111,55 @@ def to_julian_date(self):
21112111
self.nanosecond / 3600.0 / 1e+9
21122112
) / 24.0)
21132113

2114-
def month_name(self, time_locale=None):
2114+
def month_name(self, locale=None):
21152115
"""
21162116
Return the month names of the DateTimeIndex with specified locale.
21172117
21182118
Parameters
21192119
----------
2120-
time_locale : string, default None (English locale)
2120+
locale : string, default None (English locale)
21212121
locale determining the language in which to return the month name
21222122
21232123
Returns
21242124
-------
21252125
month_names : Index
21262126
Index of month names
2127+
2128+
.. versionadded:: 0.23.0
21272129
"""
21282130
values = self.asi8
21292131
if self.tz is not None:
21302132
if self.tz is not utc:
21312133
values = self._local_timestamps()
21322134

21332135
result = fields.get_date_name_field(values, 'month_name',
2134-
time_locale=time_locale)
2136+
locale=locale)
21352137
result = self._maybe_mask_results(result)
21362138
return Index(result, name=self.name)
21372139

2138-
def day_name(self, time_locale=None):
2140+
def day_name(self, locale=None):
21392141
"""
21402142
Return the day names of the DateTimeIndex with specified locale.
21412143
21422144
Parameters
21432145
----------
2144-
time_locale : string, default None (English locale)
2146+
locale : string, default None (English locale)
21452147
locale determining the language in which to return the day name
21462148
21472149
Returns
21482150
-------
21492151
month_names : Index
21502152
Index of day names
2153+
2154+
.. versionadded:: 0.23.0
21512155
"""
21522156
values = self.asi8
21532157
if self.tz is not None:
21542158
if self.tz is not utc:
21552159
values = self._local_timestamps()
21562160

21572161
result = fields.get_date_name_field(values, 'day_name',
2158-
time_locale=time_locale)
2162+
locale=locale)
21592163
result = self._maybe_mask_results(result)
21602164
return Index(result, name=self.name)
21612165

pandas/tests/scalar/timestamp/test_timestamp.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from pandas.errors import OutOfBoundsDatetime
2323
from pandas.compat import long, PY3
2424
from pandas.compat.numpy import np_datetime64_compat
25-
from pandas import Timestamp, Period, Timedelta
25+
from pandas import Timestamp, Period, Timedelta, NaT
2626

2727

2828
class TestTimestampProperties(object):
@@ -119,6 +119,11 @@ def test_names(self, data, time_locale):
119119
assert data.day_name(time_locale) == expected_day
120120
assert data.month_name(time_locale) == expected_month
121121

122+
# Test NaT
123+
nan_ts = Timestamp(NaT)
124+
assert np.isnan(nan_ts.day_name(time_locale))
125+
assert np.isnan(nan_ts.month_name(time_locale))
126+
122127
@pytest.mark.parametrize('tz', [None, 'UTC', 'US/Eastern', 'Asia/Tokyo'])
123128
def test_is_leap_year(self, tz):
124129
# GH 13727

pandas/tests/series/test_datetime_values.py

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# coding=utf-8
22
# pylint: disable-msg=E1101,W0612
33

4+
import locale
5+
import calendar
46
import pytest
57

68
from datetime import datetime, date
@@ -32,7 +34,7 @@ def test_dt_namespace_accessor(self):
3234
ok_for_dt = DatetimeIndex._datetimelike_ops
3335
ok_for_dt_methods = ['to_period', 'to_pydatetime', 'tz_localize',
3436
'tz_convert', 'normalize', 'strftime', 'round',
35-
'floor', 'ceil', 'weekday_name']
37+
'floor', 'ceil', 'day_name', 'month_name']
3638
ok_for_td = TimedeltaIndex._datetimelike_ops
3739
ok_for_td_methods = ['components', 'to_pytimedelta', 'total_seconds',
3840
'round', 'floor', 'ceil']
@@ -274,6 +276,46 @@ def test_dt_accessor_no_new_attributes(self):
274276
"You cannot add any new attribute"):
275277
s.dt.xlabel = "a"
276278

279+
@pytest.mark.skipif(not tm.get_locales(), reason='No available locales')
280+
@pytest.mark.parametrize('time_locale', tm.get_locales() + [None])
281+
def test_dt_accessor_datetime_name_accessors(self, time_locale):
282+
# Test Monday -> Sunday and January -> December, in that sequence
283+
if time_locale is None:
284+
# If the time_locale is None, day-name and month_name should
285+
# return the english attributes
286+
expected_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
287+
'Friday', 'Saturday', 'Sunday']
288+
expected_months = ['January', 'February', 'March', 'April', 'May',
289+
'June', 'July', 'August', 'September',
290+
'October', 'November', 'December']
291+
else:
292+
with tm.set_locale(time_locale, locale.LC_TIME):
293+
expected_days = calendar.day_name[:]
294+
expected_months = calendar.month_name[1:]
295+
296+
s = Series(DatetimeIndex(freq='D', start=datetime(1998, 1, 1),
297+
periods=365))
298+
english_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
299+
'Friday', 'Saturday', 'Sunday']
300+
for day, name, eng_name in zip(range(4, 11),
301+
expected_days,
302+
english_days):
303+
name = name.capitalize()
304+
assert s.dt.weekday_name[day] == eng_name
305+
assert s.dt.day_name(locale=time_locale)[day] == name
306+
s = s.append(Series([pd.NaT]))
307+
assert np.isnan(s.dt.day_name(locale=time_locale).iloc[-1])
308+
309+
s = Series(DatetimeIndex(freq='M', start='2012', end='2013'))
310+
result = s.dt.month_name(locale=time_locale)
311+
expected = Series([month.capitalize() for month in expected_months])
312+
tm.assert_series_equal(result, expected)
313+
for s_date, expected in zip(s, expected_months):
314+
result = s_date.month_name(locale=time_locale)
315+
assert result == expected.capitalize()
316+
s = s.append(Series([pd.NaT]))
317+
assert np.isnan(s.dt.month_name(locale=time_locale).iloc[-1])
318+
277319
def test_strftime(self):
278320
# GH 10086
279321
s = Series(date_range('20130101', periods=5))

0 commit comments

Comments
 (0)