-
-
Notifications
You must be signed in to change notification settings - Fork 18.9k
ENH: Return locale based month_name and weekday_name values (#12805, #12806) #18164
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
Conversation
need to add month_name to nat docstrings generation |
doc/source/whatsnew/v0.21.1.txt
Outdated
- | ||
- | ||
- :attr:`Timestamp.month_name`, :attr:`DatetimeIndex.month_name`, and :attr:`Series.dt.month_name` are now available (:issue:`12805`) | ||
- ``month_name` and ``weekday_name`` attributes will now return locale aware values (:issue:`12806`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move the 2nd point to bug fix; only mention weekday_name and use attr (as u did for month_name)
|
||
# GH 12806 | ||
@pytest.mark.parametrize('time_locale', tm.get_locales()) | ||
def test_datetimeindex_accessors(self, time_locale): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can u split out the month_name and weekday_name to a separate test
skip if number of locales is 0
can then check that we don’t skip on build where we should have a locales defined
ca6eb78
to
52a1464
Compare
Added |
doc/source/whatsnew/v0.21.1.txt
Outdated
@@ -22,7 +22,7 @@ Other Enhancements | |||
^^^^^^^^^^^^^^^^^^ | |||
|
|||
- :meth:`Timestamp.timestamp` is now available in Python 2.7. (:issue:`17329`) | |||
- | |||
- :attr:`Timestamp.month_name`, :attr:`DatetimeIndex.month_name`, and :attr:`Series.dt.month_name` are now available (:issue:`12805`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually move these to 0.22, we can't backport this as you are using some of the new timeseries stuff
52a1464
to
6f39578
Compare
Codecov Report
@@ Coverage Diff @@
## master #18164 +/- ##
==========================================
+ Coverage 91.4% 91.4% +<.01%
==========================================
Files 163 163
Lines 50064 50069 +5
==========================================
+ Hits 45759 45768 +9
+ Misses 4305 4301 -4
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #18164 +/- ##
==========================================
+ Coverage 91.69% 91.69% +<.01%
==========================================
Files 150 150
Lines 49043 48997 -46
==========================================
- Hits 44968 44929 -39
+ Misses 4075 4068 -7
Continue to review full report at Codecov.
|
doc/source/whatsnew/v0.22.0.txt
Outdated
@@ -94,7 +94,7 @@ Bug Fixes | |||
Conversion | |||
^^^^^^^^^^ | |||
|
|||
- | |||
- Bug in :attr:`Timestamp.weekday_name` and :attr:`DatetimeIndex.weekday_name` not returning locale aware values (:issue:`12806`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should this as a bug, it is rather an API change as we deliberately returned non-locale dependent values.
(and to be honest, I am also not sure that I really like locale-dependent return values)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could add an additonal accessor, e.g. weekday_name_for_locale
? (hate adding things, but I agree that this might not be only desirable in some cases).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could turn weekday_name
and month_name
into functions that can accept a locale
argument and default it to 'en_US.UTF8'
? I think that would be clean, but it would definitely be an API change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, that's an idea. we could could make new ones weekday_names()
(and then just deprecate weekday_name
That's an option. Another option is to leave it as is but to clearly document in those attributes how to get your locale dependent names. Eg it would be nice that the following would work:
(the problem here is that This works:
(second issue is that But I think both issues could be solved to make this easier. |
(On the other hand, such a pointer in the docstring is certainly less discoverable than a |
Index.map dict issue is reported here: #13517 |
I'll summarize the proposals and add an additional one:
I think 4) would be a nice option to maintain the existing behavior and minimize altering existing methods. Otherwise I think I'd prefer 3) over 2) (I realize the Pandas API is already large). What do you guys think @jreback @jorisvandenbossche? |
so I actually like renaming
I wonder how common the need to look things up in different locales is, if its not that common (guess), then this makes sense
|
6f39578
to
c4837dc
Compare
c4837dc
to
18822e6
Compare
I decided to implement a Also, should I got ahead and also depreciate the |
18822e6
to
1162f9a
Compare
that sounds good
yes this is also ok |
1162f9a
to
ab57006
Compare
Sorry for being idle on this PR. Rebased, and added the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you also deprecate weekday_name (in favor of day_name)
pandas/_libs/tslibs/fields.pyx
Outdated
|
||
count = len(dtindex) | ||
out = np.empty(count, dtype=object) | ||
|
||
if field == 'weekday_name': | ||
_dayname = np.array(['Monday', 'Tuesday', 'Wednesday', 'Thursday', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move (and import from) ccalendar
pandas/_libs/tslibs/fields.pyx
Outdated
'friday', 'saturday', 'sunday'], | ||
dtype=np.object_) | ||
else: | ||
with set_locale(time_locale, locale.LC_TIME): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this function (the locale conversion of the names) should be in ccalandar.pyx
just have a function which takes the locale (default None), so this is much simpler here
pandas/_libs/tslibs/nattype.pyx
Outdated
@@ -320,7 +325,32 @@ class NaTType(_NaT): | |||
# nan methods | |||
weekday = _make_nan_func('weekday', datetime) | |||
isoweekday = _make_nan_func('isoweekday', datetime) | |||
month_name = _make_nan_func('month_name', # noqa:E128 | |||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make this a named parameter
pandas/_libs/tslibs/nattype.pyx
Outdated
@@ -42,8 +42,13 @@ _nat_scalar_rules[Py_GE] = False | |||
def _make_nan_func(func_name, cls): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make doc an optional parameter doc
ab57006
to
b0aeb3e
Compare
Created Although, I am not sure how to add a deprecation warning to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need tests that catch the deprecation warning
pandas/_libs/tslibs/timestamps.pyx
Outdated
@property | ||
def weekday_name(self): | ||
""" | ||
.. depreciated:: 0.23.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deprecated
@@ -342,6 +327,37 @@ def test_datetimeindex_accessors(self): | |||
assert dates.weekofyear.tolist() == expected | |||
assert [d.weekofyear for d in dates] == expected | |||
|
|||
# GH 12806 | |||
@pytest.mark.skipif(not tm.get_locales(), reason='No available locales') | |||
@pytest.mark.parametrize('time_locale', tm.get_locales()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should still test for locale is None
pandas/core/indexes/datetimes.py
Outdated
@@ -1686,7 +1688,7 @@ def freq(self, value): | |||
weekday_name = _field_accessor( | |||
'weekday_name', | |||
'weekday_name', | |||
"The name of day in a week (ex: Friday)\n\n.. versionadded:: 0.18.1") | |||
"The name of day in a week (ex: Friday)\n\n.. depreciated:: 0.23.0") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deprecated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to show a deprecation warning for this as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvm i see this below
doc/source/whatsnew/v0.23.0.txt
Outdated
@@ -202,6 +202,8 @@ Other Enhancements | |||
- ``Resampler`` objects now have a functioning :attr:`~pandas.core.resample.Resampler.pipe` method. | |||
Previously, calls to ``pipe`` were diverted to the ``mean`` method (:issue:`17905`). | |||
- :func:`~pandas.api.types.is_scalar` now returns ``True`` for ``DateOffset`` objects (:issue:`18943`). | |||
- :meth:`Timestamp.month_name`, :meth:`DatetimeIndex.month_name`, and :meth:`Series.dt.month_name` are now available (:issue:`12805`) | |||
- :meth:`Timestamp.day_name` and :meth:`DatetimeIndex.day_name` are now available to return day names with a specified locale (:issue:`12806`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need an entry in Deprecated section
pandas/_libs/tslibs/ccalendar.pyx
Outdated
@@ -13,6 +13,9 @@ cimport numpy as np | |||
from numpy cimport int64_t, int32_t | |||
np.import_array() | |||
|
|||
import locale | |||
from pandas.util.testing import set_locale | |||
from strptime import LocaleTime |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move these imports inside get_locale_names
pandas/_libs/tslibs/timestamps.pyx
Outdated
days = dict(enumerate(ccalendar.DAYS_FULL)) | ||
else: | ||
names = ccalendar.get_locale_names('f_weekday', time_locale) | ||
days = dict(enumerate(names)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move the dict(enumerate after the if (eg do it once)
pandas/_libs/tslibs/timestamps.pyx
Outdated
months = dict(enumerate(ccalendar.MONTHS_FULL)) | ||
else: | ||
names = ccalendar.get_locale_names('f_month', time_locale) | ||
months = dict(enumerate(names)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
62181e6
to
025ab67
Compare
rebase on master |
616665a
to
565b5b6
Compare
Rebased on master and addressed your comments @jreback. Looks like a travis build timed out. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you update the test that hits Series.dt.day_name (you have to add the new methods and remove the old one), in the series tests
pandas/_libs/tslibs/ccalendar.pyx
Outdated
@@ -199,3 +208,27 @@ cpdef int32_t get_day_of_year(int year, int month, int day) nogil: | |||
|
|||
day_of_year = mo_off + day | |||
return day_of_year | |||
|
|||
|
|||
cpdef get_locale_names(object name_type, object time_locale=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only needs to be cdef unless called elsewhere
pandas/_libs/tslibs/ccalendar.pyx
Outdated
from pandas.util.testing import set_locale | ||
|
||
with set_locale(time_locale, locale.LC_TIME): | ||
locale_names = getattr(LocaleTime(), name_type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can just return this directly, no need to have a variable locale_names
pandas/_libs/tslibs/fields.pyx
Outdated
@@ -85,7 +85,8 @@ def build_field_sarray(ndarray[int64_t] dtindex): | |||
|
|||
@cython.wraparound(False) | |||
@cython.boundscheck(False) | |||
def get_date_name_field(ndarray[int64_t] dtindex, object field): | |||
def get_date_name_field(ndarray[int64_t] dtindex, object field, | |||
object time_locale=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename to locale
@@ -96,23 +97,37 @@ def get_date_name_field(ndarray[int64_t] dtindex, object field): | |||
pandas_datetimestruct dts | |||
int dow | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of using _dayname, _monthname, use cdef names ndarray[object]
pandas/_libs/tslibs/nattype.pyx
Outdated
month_name : string | ||
""") | ||
day_name = _make_nan_func('day_name', # noqa:E128 | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you added versionadded 0.23 to these
expected_months = calendar.month_name[1:] | ||
|
||
# GH 11128 | ||
dti = DatetimeIndex(freq='D', start=datetime(1998, 1, 1), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a NaT here
@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): | ||
if time_locale is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a comment what you are setting here
for day, name, eng_name in zip(range(4, 11), | ||
expected_days, | ||
english_days): | ||
# Test Monday -> Sunday |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you move this comment to near the GH issue number
|
||
# GH 12805 | ||
dti = DatetimeIndex(freq='M', start='2012', end='2013') | ||
# Test January -> December |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same, move this up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a NaT here
# GH 17354 | ||
assert data.weekday_name == expected | ||
# Test .weekday_name, .day_name(), .month_name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test with NaT
72f5b01
to
a1cd27b
Compare
I believe I addressed everything
|
pandas/_libs/tslibs/fields.pyx
Outdated
""" | ||
Given a int64-based datetime index, return array of strings of date | ||
name based on requested field (e.g. weekday_name) | ||
""" | ||
cdef: | ||
Py_ssize_t i, count = 0 | ||
ndarray[object] out | ||
ndarray[object] out, name | ||
pandas_datetimestruct dts |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be names
""" | ||
warnings.warn("`weekday_name` is deprecated and will be removed in a " | ||
"future version. Use `day_name` instead", | ||
DeprecationWarning) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can u have this call day_name
006ac9e
to
dbbfbe7
Compare
Changed name to names, and used the day_name method for weekday_name. All green |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very small comments, also needs a rebase. ping on green.
@@ -581,6 +581,8 @@ These can be accessed like ``Series.dt.<property>``. | |||
Series.dt.round | |||
Series.dt.floor | |||
Series.dt.ceil | |||
Series.dt.month_name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if weekday_name is anywhere here can you remove
@@ -262,6 +262,8 @@ class DatetimeIndex(DatelikeOps, TimelikeOps, DatetimeIndexOpsMixin, | |||
to_pydatetime | |||
to_series | |||
to_frame |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if weekday_name is here can u remove
@jorisvandenbossche @TomAugspurger lgtm. if any final comments. |
Fix localiztion of months Add series test and whatsnew notes Modify test and lint Add Timestamp rst Address comments Lint, move whatsnew, remove hardcoded Monday, check that get_locale is None create functions instead of attributes Fix white space Modify tests Lint Rebase and add depreciation message Deprecate weekday_name, create localizing function in ccalendar.pyx clean up imports Address user comments Try to address ci issues Rebase and adjust tests Move some imports to top of file, remove dup implimention of day_name in fields Fix failing test allow path to accept weekday_name as well address review address review missed files fix typo and call internal method Fix lint modify pytest param for appvoyer fix more appvoyer tests Remove weekday_name availability
Removed doc instances of |
thanks @mroeschke nice patch! |
@mroeschke a number of deprecation warnings need catching.
|
closes #12805
closes #12806
git diff upstream/master -u -- "*.py" | flake8 --diff
I am not sure what locales are available on the CI machines, but the test will run all available locales.