Skip to content

Commit

Permalink
address review missed files
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke committed Feb 27, 2018
1 parent 6f4a962 commit a1cd27b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
39 changes: 21 additions & 18 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ from util cimport (is_datetime64_object, is_timedelta64_object,
INT64_MAX)

cimport ccalendar
from ccalendar import get_locale_names, MONTHS_FULL, DAYS_FULL
from conversion import tz_localize_to_utc, date_normalize
from conversion cimport (tz_convert_single, _TSObject,
convert_to_tsobject, convert_datetime_to_tsobject)
from fields import get_date_field, get_start_end_field
from fields import get_start_end_field, get_date_name_field
from nattype import NaT
from nattype cimport NPY_NAT
from np_datetime import OutOfBoundsDatetime
Expand Down Expand Up @@ -352,6 +351,16 @@ cdef class _Timestamp(datetime):
field, freqstr, month_kw)
return out[0]

cpdef _get_date_name_field(self, object field, object locale):
cdef:
int64_t val
ndarray out

val = self._maybe_convert_value_to_local()
out = get_date_name_field(np.array([val], dtype=np.int64),
field, locale=locale)
return out[0]

@property
def _repr_base(self):
return '{date} {time}'.format(date=self._date_repr,
Expand Down Expand Up @@ -702,45 +711,39 @@ class Timestamp(_Timestamp):
def dayofweek(self):
return self.weekday()

def day_name(self, time_locale=None):
def day_name(self, locale=None):
"""
Return the day name of the Timestamp with specified locale.
Parameters
----------
time_locale : string, default None (English locale)
locale : string, default None (English locale)
locale determining the language in which to return the day name
Returns
-------
day_name : string
.. versionadded:: 0.23.0
"""
if time_locale is None:
names = DAYS_FULL
else:
names = get_locale_names('f_weekday', time_locale)
days = dict(enumerate(names))
return days[self.weekday()].capitalize()
return self._get_date_name_field('day_name', locale)

def month_name(self, time_locale=None):
def month_name(self, locale=None):
"""
Return the month name of the Timestamp with specified locale.
Parameters
----------
time_locale : string, default None (English locale)
locale : string, default None (English locale)
locale determining the language in which to return the month name
Returns
-------
month_name : string
.. versionadded:: 0.23.0
"""
if time_locale is None:
names = MONTHS_FULL
else:
names = get_locale_names('f_month', time_locale)
months = dict(enumerate(names))
return months[self.month].capitalize()
return self._get_date_name_field('month_name', locale)

@property
def weekday_name(self):
Expand Down
19 changes: 13 additions & 6 deletions pandas/tests/indexes/datetimes/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,10 @@ def test_datetimeindex_accessors(self):
@pytest.mark.skipif(not tm.get_locales(), reason='No available locales')
@pytest.mark.parametrize('time_locale', tm.get_locales() + [None])
def test_datetime_name_accessors(self, time_locale):
# Test Monday -> Sunday and January -> December, in that sequence
if time_locale is None:
# If the time_locale is None, day-name and month_name should
# return the english attributes
expected_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday', 'Sunday']
expected_months = ['January', 'February', 'March', 'April', 'May',
Expand All @@ -264,23 +267,27 @@ def test_datetime_name_accessors(self, time_locale):
for day, name, eng_name in zip(range(4, 11),
expected_days,
english_days):
# Test Monday -> Sunday
name = name.capitalize()
assert dti.weekday_name[day] == eng_name
assert dti.day_name(time_locale=time_locale)[day] == name
assert dti.day_name(locale=time_locale)[day] == name
ts = Timestamp(datetime(2016, 4, day))
assert ts.weekday_name == eng_name
assert ts.day_name(time_locale=time_locale) == name
assert ts.day_name(locale=time_locale) == name
dti = dti.append(DatetimeIndex([pd.NaT]))
assert np.isnan(dti.day_name(locale=time_locale)[-1])
ts = Timestamp(pd.NaT)
assert np.isnan(ts.day_name(locale=time_locale))

# GH 12805
dti = DatetimeIndex(freq='M', start='2012', end='2013')
# Test January -> December
result = dti.month_name(time_locale=time_locale)
result = dti.month_name(locale=time_locale)
expected = Index([month.capitalize() for month in expected_months])
tm.assert_index_equal(result, expected)
for date, expected in zip(dti, expected_months):
result = date.month_name(time_locale=time_locale)
result = date.month_name(locale=time_locale)
assert result == expected.capitalize()
dti = dti.append(DatetimeIndex([pd.NaT]))
assert np.isnan(dti.month_name(locale=time_locale)[-1])

def test_nanosecond_field(self):
dti = DatetimeIndex(np.arange(10))
Expand Down

0 comments on commit a1cd27b

Please sign in to comment.