Skip to content

Commit e0451e0

Browse files
committed
ENH: Make weekday_name field in DatetimeIndex categorical
1 parent c2da06c commit e0451e0

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

pandas/core/indexes/datetimes.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from pandas.tseries.frequencies import to_offset, get_period_alias, Resolution
4343
from pandas.core.indexes.datetimelike import (
4444
DatelikeOps, TimelikeOps, DatetimeIndexOpsMixin)
45+
from pandas.core.indexes.category import CategoricalIndex
4546
from pandas.tseries.offsets import (
4647
DateOffset, generate_range, Tick, CDay, prefix_mapping)
4748

@@ -55,8 +56,7 @@
5556
from pandas._libs import (lib, index as libindex, tslib as libts,
5657
join as libjoin, Timestamp)
5758
from pandas._libs.tslibs import (timezones, conversion, fields, parsing,
58-
ccalendar,
59-
resolution as libresolution)
59+
resolution as libresolution, ccalendar)
6060

6161
# -------- some conversion wrapper functions
6262

@@ -2511,7 +2511,9 @@ def month_name(self, locale=None):
25112511
result = fields.get_date_name_field(values, 'month_name',
25122512
locale=locale)
25132513
result = self._maybe_mask_results(result)
2514-
return Index(result, name=self.name)
2514+
return CategoricalIndex(result, ordered=True,
2515+
categories=ccalendar.MONTHS_FULL[1:],
2516+
name=self.name)
25152517

25162518
def day_name(self, locale=None):
25172519
"""
@@ -2537,7 +2539,8 @@ def day_name(self, locale=None):
25372539
result = fields.get_date_name_field(values, 'day_name',
25382540
locale=locale)
25392541
result = self._maybe_mask_results(result)
2540-
return Index(result, name=self.name)
2542+
return CategoricalIndex(result, ordered=True, name=self.name,
2543+
categories=ccalendar.DAYS_FULL)
25412544

25422545

25432546
DatetimeIndex._add_comparison_methods()

pandas/tests/indexes/datetimes/test_misc.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pandas as pd
88
import pandas.util.testing as tm
99
from pandas import (Index, DatetimeIndex, datetime, offsets,
10-
date_range, Timestamp)
10+
date_range, Timestamp, CategoricalIndex)
1111

1212

1313
class TestTimeSeries(object):
@@ -283,7 +283,9 @@ def test_datetime_name_accessors(self, time_locale):
283283
# GH 12805
284284
dti = DatetimeIndex(freq='M', start='2012', end='2013')
285285
result = dti.month_name(locale=time_locale)
286-
expected = Index([month.capitalize() for month in expected_months])
286+
expected = CategoricalIndex(
287+
[month.capitalize() for month in expected_months],
288+
ordered=True, categories=expected_months)
287289
tm.assert_index_equal(result, expected)
288290
for date, expected in zip(dti, expected_months):
289291
result = date.month_name(locale=time_locale)

pandas/tests/series/test_datetime_values.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ def test_dt_accessor_datetime_name_accessors(self, time_locale):
308308

309309
s = Series(DatetimeIndex(freq='M', start='2012', end='2013'))
310310
result = s.dt.month_name(locale=time_locale)
311-
expected = Series([month.capitalize() for month in expected_months])
311+
expected = Series([month.capitalize() for month in expected_months])\
312+
.astype('category', ordered=True, categories=expected_months)
312313
tm.assert_series_equal(result, expected)
313314
for s_date, expected in zip(s, expected_months):
314315
result = s_date.month_name(locale=time_locale)

0 commit comments

Comments
 (0)