|
1 | 1 | # coding=utf-8
|
2 | 2 | # pylint: disable-msg=E1101,W0612
|
3 | 3 |
|
| 4 | +import locale |
| 5 | +import calendar |
4 | 6 | import pytest
|
5 | 7 |
|
6 | 8 | from datetime import datetime, date
|
@@ -32,7 +34,7 @@ def test_dt_namespace_accessor(self):
|
32 | 34 | ok_for_dt = DatetimeIndex._datetimelike_ops
|
33 | 35 | ok_for_dt_methods = ['to_period', 'to_pydatetime', 'tz_localize',
|
34 | 36 | 'tz_convert', 'normalize', 'strftime', 'round',
|
35 |
| - 'floor', 'ceil', 'weekday_name'] |
| 37 | + 'floor', 'ceil', 'day_name', 'month_name'] |
36 | 38 | ok_for_td = TimedeltaIndex._datetimelike_ops
|
37 | 39 | ok_for_td_methods = ['components', 'to_pytimedelta', 'total_seconds',
|
38 | 40 | 'round', 'floor', 'ceil']
|
@@ -274,6 +276,46 @@ def test_dt_accessor_no_new_attributes(self):
|
274 | 276 | "You cannot add any new attribute"):
|
275 | 277 | s.dt.xlabel = "a"
|
276 | 278 |
|
| 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 | + |
277 | 319 | def test_strftime(self):
|
278 | 320 | # GH 10086
|
279 | 321 | s = Series(date_range('20130101', periods=5))
|
|
0 commit comments