Skip to content

WIP: Make weekday_name field in DatetimeIndex categorical #21177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from pandas.tseries.frequencies import to_offset, get_period_alias, Resolution
from pandas.core.indexes.datetimelike import (
DatelikeOps, TimelikeOps, DatetimeIndexOpsMixin)
from pandas.core.indexes.category import CategoricalIndex
from pandas.tseries.offsets import (
DateOffset, generate_range, Tick, CDay, prefix_mapping)

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

# -------- some conversion wrapper functions

Expand Down Expand Up @@ -2511,7 +2511,9 @@ def month_name(self, locale=None):
result = fields.get_date_name_field(values, 'month_name',
locale=locale)
result = self._maybe_mask_results(result)
return Index(result, name=self.name)
return CategoricalIndex(result, ordered=True,
categories=ccalendar.MONTHS_FULL[1:],
name=self.name)

def day_name(self, locale=None):
"""
Expand All @@ -2537,7 +2539,8 @@ def day_name(self, locale=None):
result = fields.get_date_name_field(values, 'day_name',
locale=locale)
result = self._maybe_mask_results(result)
return Index(result, name=self.name)
return CategoricalIndex(result, ordered=True, name=self.name,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this must have broken some tests as well

categories=ccalendar.DAYS_FULL)


DatetimeIndex._add_comparison_methods()
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/indexes/datetimes/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pandas as pd
import pandas.util.testing as tm
from pandas import (Index, DatetimeIndex, datetime, offsets,
date_range, Timestamp)
date_range, Timestamp, CategoricalIndex)


class TestTimeSeries(object):
Expand Down Expand Up @@ -283,7 +283,9 @@ def test_datetime_name_accessors(self, time_locale):
# GH 12805
dti = DatetimeIndex(freq='M', start='2012', end='2013')
result = dti.month_name(locale=time_locale)
expected = Index([month.capitalize() for month in expected_months])
expected = CategoricalIndex(
[month.capitalize() for month in expected_months],
ordered=True, categories=expected_months)
tm.assert_index_equal(result, expected)
for date, expected in zip(dti, expected_months):
result = date.month_name(locale=time_locale)
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/series/test_datetime_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ def test_dt_accessor_datetime_name_accessors(self, time_locale):

s = Series(DatetimeIndex(freq='M', start='2012', end='2013'))
result = s.dt.month_name(locale=time_locale)
expected = Series([month.capitalize() for month in expected_months])
expected = Series([month.capitalize() for month in expected_months])\
.astype('category', ordered=True, categories=expected_months)
tm.assert_series_equal(result, expected)
for s_date, expected in zip(s, expected_months):
result = s_date.month_name(locale=time_locale)
Expand Down