Skip to content

Commit 76b6636

Browse files
committed
Address user comments
1 parent 72c945b commit 76b6636

File tree

8 files changed

+71
-62
lines changed

8 files changed

+71
-62
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ Deprecations
289289
- ``Series.valid`` is deprecated. Use :meth:`Series.dropna` instead (:issue:`18800`).
290290
- :func:`read_excel` has deprecated the ``skip_footer`` parameter. Use ``skipfooter`` instead (:issue:`18836`)
291291
- The ``is_copy`` attribute is deprecated and will be removed in a future version (:issue:`18801`).
292+
- :attr:`Timestamp.weekday_name`, :attr:`DatetimeIndex.weekday_name`, and :attr:`Series.dt.weekday_name` are deprecated in favor of :meth:`Timestamp.day_name`, :meth:`DatetimeIndex.day_name`, and :meth:`Series.dt.day_name` (:issue:`12806`)
292293

293294
.. _whatsnew_0230.prior_deprecations:
294295

pandas/_libs/tslibs/ccalendar.pyx

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ cimport numpy as np
1313
from numpy cimport int64_t, int32_t
1414
np.import_array()
1515

16-
import locale
17-
from pandas.util.testing import set_locale
18-
from strptime import LocaleTime
1916

2017
# ----------------------------------------------------------------------
2118
# Constants
@@ -42,7 +39,7 @@ MONTHS = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL',
4239
# The first blank line is consistent with calendar.month_name in the calendar
4340
# standard library
4441
MONTHS_FULL = ['', 'January', 'February', 'March', 'April', 'May', 'June',
45-
'July', 'August, 'September', 'October', 'November',
42+
'July', 'August', 'September', 'October', 'November',
4643
'December']
4744
MONTH_NUMBERS = {name: num for num, name in enumerate(MONTHS)}
4845
MONTH_ALIASES = {(num + 1): name for num, name in enumerate(MONTHS)}
@@ -184,7 +181,7 @@ cpdef int32_t get_week_of_year(int year, int month, int day) nogil:
184181
return woy
185182

186183

187-
cpdef get_locale_names(object name_type, object time_locale=None) nogil:
184+
cpdef get_locale_names(object name_type, object time_locale=None):
188185
"""Returns an array of localized day or month names
189186
190187
Parameters
@@ -199,9 +196,11 @@ cpdef get_locale_names(object name_type, object time_locale=None) nogil:
199196
200197
"""
201198
cdef:
202-
object locale_time = LocaleTime()
203199
list locale_names
204200

201+
import locale
202+
from pandas.util.testing import set_locale
203+
from strptime import LocaleTime
205204
with set_locale(time_locale, locale.LC_TIME):
206205
locale_names = getattr(LocaleTime(), name_type)
207206
return locale_names

pandas/_libs/tslibs/fields.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ cimport numpy as np
1313
from numpy cimport ndarray, int64_t, int32_t, int8_t
1414
np.import_array()
1515

16-
16+
from ccalendar import get_locale_names, MONTHS_FULL, DAYS_FULL
1717
from ccalendar cimport (get_days_in_month, is_leapyear, dayofweek,
18-
get_week_of_year, get_locale_names, MONTHS_FULL,
19-
DAYS_FULL)
18+
get_week_of_year)
2019
from np_datetime cimport (pandas_datetimestruct, pandas_timedeltastruct,
2120
dt64_to_dtstruct, td64_to_tdstruct)
2221
from nattype cimport NPY_NAT
@@ -111,7 +110,8 @@ def get_date_name_field(ndarray[int64_t] dtindex, object field,
111110
dt64_to_dtstruct(dtindex[i], &dts)
112111
dow = dayofweek(dts.year, dts.month, dts.day)
113112
out[i] = _dayname[dow]
114-
if field == 'day_name':
113+
return out
114+
elif field == 'day_name':
115115
if time_locale is None:
116116
_dayname = np.array(DAYS_FULL, dtype=np.object_)
117117
else:

pandas/_libs/tslibs/nattype.pyx

+24-29
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,18 @@ _nat_scalar_rules[Py_GE] = False
3737

3838
# ----------------------------------------------------------------------
3939

40-
def _make_missing_value_func(func_name, missing_value, doc):
41-
"""Generate function that returns a missing value
42-
43-
Parameters
44-
----------
45-
func_name : string
46-
missing_value : nan value
47-
doc: string
48-
49-
Returns
50-
-------
51-
f : function
52-
"""
40+
41+
def _make_nan_func(func_name, doc):
5342
def f(*args, **kwargs):
54-
return missing_value
43+
return np.nan
44+
f.__name__ = func_name
45+
f.__doc__ = doc
46+
return f
47+
5548

49+
def _make_nat_func(func_name, doc):
50+
def f(*args, **kwargs):
51+
return NaT
5652
f.__name__ = func_name
5753
f.__doc__ = doc
5854
return f
@@ -316,10 +312,9 @@ class NaTType(_NaT):
316312
# These are the ones that can get their docstrings from datetime.
317313

318314
# nan methods
319-
weekday = _make_missing_value_func('weekday', np.nan, datetime.__doc__)
320-
isoweekday = _make_missing_value_func('isoweekday', np.nan,
321-
datetime.__doc__)
322-
month_name = _make_missing_value_func('month_name', np.nan, # noqa:E128
315+
weekday = _make_nan_func('weekday', datetime.weekday.__doc__)
316+
isoweekday = _make_nan_func('isoweekday', datetime.isoweekday.__doc__)
317+
month_name = _make_nan_func('month_name', # noqa:E128
323318
"""
324319
Return the month name of the Timestamp with specified locale.
325320
@@ -332,7 +327,7 @@ class NaTType(_NaT):
332327
-------
333328
month_name : string
334329
""")
335-
day_name = _make_missing_value_func('day_name', np.nan, # noqa:E128
330+
day_name = _make_nan_func('day_name', # noqa:E128
336331
"""
337332
Return the day name of the Timestamp with specified locale.
338333
@@ -346,7 +341,7 @@ class NaTType(_NaT):
346341
day_name : string
347342
""")
348343
# _nat_methods
349-
date = _make_missing_value_func('date', NaT, datetime.__doc__)
344+
date = _make_nat_func('date', datetime.date.__doc__)
350345

351346
utctimetuple = _make_error_func('utctimetuple', datetime)
352347
timetz = _make_error_func('timetz', datetime)
@@ -436,14 +431,14 @@ class NaTType(_NaT):
436431
""")
437432

438433
# _nat_methods
439-
to_pydatetime = _make_missing_value_func('to_pydatetime', NaT, # noqa:E128
434+
to_pydatetime = _make_nat_func('to_pydatetime', # noqa:E128
440435
"""
441436
Convert a Timestamp object to a native Python datetime object.
442437
443438
If warn=True, issue a warning if nanoseconds is nonzero.
444439
""")
445440

446-
now = _make_missing_value_func('now', NaT, # noqa:E128
441+
now = _make_nat_func('now', # noqa:E128
447442
"""
448443
Timestamp.now(tz=None)
449444
@@ -455,7 +450,7 @@ class NaTType(_NaT):
455450
tz : str or timezone object, default None
456451
Timezone to localize to
457452
""")
458-
today = _make_missing_value_func('today', NaT, # noqa:E128
453+
today = _make_nat_func('today', # noqa:E128
459454
"""
460455
Timestamp.today(cls, tz=None)
461456
@@ -468,7 +463,7 @@ class NaTType(_NaT):
468463
tz : str or timezone object, default None
469464
Timezone to localize to
470465
""")
471-
round = _make_missing_value_func('round', NaT, # noqa:E128
466+
round = _make_nat_func('round', # noqa:E128
472467
"""
473468
Round the Timestamp to the specified resolution
474469
@@ -484,15 +479,15 @@ class NaTType(_NaT):
484479
------
485480
ValueError if the freq cannot be converted
486481
""")
487-
floor = _make_missing_value_func('floor', NaT, # noqa:E128
482+
floor = _make_nat_func('floor', # noqa:E128
488483
"""
489484
return a new Timestamp floored to this resolution
490485
491486
Parameters
492487
----------
493488
freq : a freq string indicating the flooring resolution
494489
""")
495-
ceil = _make_missing_value_func('ceil', NaT, # noqa:E128
490+
ceil = _make_nat_func('ceil', # noqa:E128
496491
"""
497492
return a new Timestamp ceiled to this resolution
498493
@@ -501,7 +496,7 @@ class NaTType(_NaT):
501496
freq : a freq string indicating the ceiling resolution
502497
""")
503498

504-
tz_convert = _make_missing_value_func('tz_convert', NaT, # noqa:E128
499+
tz_convert = _make_nat_func('tz_convert', # noqa:E128
505500
"""
506501
Convert tz-aware Timestamp to another time zone.
507502
@@ -520,7 +515,7 @@ class NaTType(_NaT):
520515
TypeError
521516
If Timestamp is tz-naive.
522517
""")
523-
tz_localize = _make_missing_value_func('tz_localize', NaT, # noqa:E128
518+
tz_localize = _make_nat_func('tz_localize', # noqa:E128
524519
"""
525520
Convert naive Timestamp to local time zone, or remove
526521
timezone from tz-aware Timestamp.
@@ -555,7 +550,7 @@ class NaTType(_NaT):
555550
TypeError
556551
If the Timestamp is tz-aware and tz is not None.
557552
""")
558-
replace = _make_missing_value_func('replace', NaT, # noqa:E128
553+
replace = _make_nat_func('replace', # noqa:E128
559554
"""
560555
implements datetime.replace, handles nanoseconds
561556

pandas/_libs/tslibs/timestamps.pyx

+9-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ from util cimport (is_datetime64_object, is_timedelta64_object,
2121
INT64_MAX)
2222

2323
cimport ccalendar
24+
from ccalendar import get_locale_names, MONTHS_FULL, DAYS_FULL
2425
from conversion import tz_localize_to_utc, date_normalize
2526
from conversion cimport (tz_convert_single, _TSObject,
2627
convert_to_tsobject, convert_datetime_to_tsobject)
@@ -699,10 +700,10 @@ class Timestamp(_Timestamp):
699700
day_name : string
700701
"""
701702
if time_locale is None:
702-
days = dict(enumerate(ccalendar.DAYS_FULL))
703+
names = DAYS_FULL
703704
else:
704-
names = ccalendar.get_locale_names('f_weekday', time_locale)
705-
days = dict(enumerate(names))
705+
names = get_locale_names('f_weekday', time_locale)
706+
days = dict(enumerate(names))
706707
return days[self.weekday()].capitalize()
707708

708709
def month_name(self, time_locale=None):
@@ -719,21 +720,21 @@ class Timestamp(_Timestamp):
719720
month_name : string
720721
"""
721722
if time_locale is None:
722-
months = dict(enumerate(ccalendar.MONTHS_FULL))
723+
names = MONTHS_FULL
723724
else:
724-
names = ccalendar.get_locale_names('f_month', time_locale)
725-
months = dict(enumerate(names))
725+
names = get_locale_names('f_month', time_locale)
726+
months = dict(enumerate(names))
726727
return months[self.month].capitalize()
727728

728729
@property
729730
def weekday_name(self):
730731
"""
731-
.. depreciated:: 0.23.0
732+
.. deprecated:: 0.23.0
732733
Use ``Timestamp.day_name()`` instead
733734
"""
734735
warnings.warn("`weekday_name` is deprecated and will be removed in a "
735736
"future version. Use `day_name` instead",
736-
FutureWarning, stacklevel=2)
737+
DeprecationWarning, stacklevel=3)
737738
cdef dict wdays = {0: 'Monday', 1: 'Tuesday', 2: 'Wednesday',
738739
3: 'Thursday', 4: 'Friday', 5: 'Saturday',
739740
6: 'Sunday'}

pandas/core/indexes/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1688,7 +1688,7 @@ def freq(self, value):
16881688
weekday_name = _field_accessor(
16891689
'weekday_name',
16901690
'weekday_name',
1691-
"The name of day in a week (ex: Friday)\n\n.. depreciated:: 0.23.0")
1691+
"The name of day in a week (ex: Friday)\n\n.. deprecated:: 0.23.0")
16921692

16931693
dayofyear = _field_accessor('dayofyear', 'doy',
16941694
"The ordinal day of the year")

pandas/tests/indexes/datetimes/test_misc.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def test_datetimeindex_accessors(self):
329329

330330
# GH 12806
331331
@pytest.mark.skipif(not tm.get_locales(), reason='No available locales')
332-
@pytest.mark.parametrize('time_locale', tm.get_locales())
332+
@pytest.mark.parametrize('time_locale', tm.get_locales() + [None])
333333
def test_datetime_name_accessors(self, time_locale):
334334
with tm.set_locale(time_locale, locale.LC_TIME):
335335
# GH 11128
@@ -341,20 +341,22 @@ def test_datetime_name_accessors(self, time_locale):
341341
calendar.day_name,
342342
english_days):
343343
# Test Monday -> Sunday
344+
name = name.capitalize()
344345
assert dti.weekday_name[day] == eng_name
345-
assert dti.day_name(time_locale=time_locale) == name
346+
assert dti.day_name(time_locale=time_locale)[day] == name
346347
ts = Timestamp(datetime(2016, 4, day))
347348
assert ts.weekday_name == eng_name
348349
assert ts.day_name(time_locale=time_locale) == name
349350

351+
with tm.set_locale(time_locale, locale.LC_TIME):
350352
# GH 12805
351353
dti = DatetimeIndex(freq='M', start='2012', end='2013')
354+
expected_months = calendar.month_name[1:]
352355
# Test January -> December
353-
result = dti.month_name
354-
expected = Index([month.capitalize()
355-
for month in calendar.month_name[1:]])
356+
result = dti.month_name(time_locale=time_locale)
357+
expected = Index([month.capitalize() for month in expected_months])
356358
tm.assert_index_equal(result, expected)
357-
for date, expected in zip(dti, calendar.month_name[1:]):
359+
for date, expected in zip(dti, expected_months):
358360
result = date.month_name(time_locale=time_locale)
359361
assert result == expected.capitalize()
360362

pandas/tests/scalar/test_timestamp.py

+19-8
Original file line numberDiff line numberDiff line change
@@ -625,15 +625,26 @@ def check(value, equal):
625625
@pytest.mark.parametrize('data',
626626
[Timestamp('2017-08-28 23:00:00'),
627627
Timestamp('2017-08-28 23:00:00', tz='EST')])
628-
@pytest.mark.parametrize('time_locale', tm.get_locales())
629-
def test_day_name(self, data, time_locale):
628+
@pytest.mark.parametrize('time_locale', tm.get_locales() + [None])
629+
def test_names(self, data, time_locale):
630630
# GH 17354
631-
# Test .weekday_name and .day_name()
632-
assert data.weekday_name == 'Monday'
633-
with tm.set_locale(time_locale, locale.LC_TIME):
634-
expected = calendar.day_name[0].capitalize()
635-
result = data.day_name(time_locale)
636-
assert result == expected
631+
# Test .weekday_name, .day_name(), .month_name
632+
with tm.assert_produces_warning(DeprecationWarning,
633+
check_stacklevel=False):
634+
assert data.weekday_name == 'Monday'
635+
if time_locale is None:
636+
assert data.day_name(time_locale) == 'Monday'
637+
assert data.month_name(time_locale) == 'August'
638+
else:
639+
with tm.set_locale(time_locale, locale.LC_TIME):
640+
expected = calendar.day_name[0].capitalize()
641+
result = data.day_name(time_locale)
642+
assert result == expected
643+
644+
with tm.set_locale(time_locale, locale.LC_TIME):
645+
expected = calendar.month_name[8].capitalize()
646+
result = data.month_name(time_locale)
647+
assert result == expected
637648

638649
def test_pprint(self):
639650
# GH12622

0 commit comments

Comments
 (0)