Skip to content

Commit a4b88df

Browse files
ENH: Support day_of_year over dayofyear (pandas-dev#9606)
Similar to changes for day_of_week, create aliases for day_of_year and add to respective test files to ensure back-compat. Also fix whatsnew rst file
1 parent 9320a7d commit a4b88df

File tree

12 files changed

+24
-20
lines changed

12 files changed

+24
-20
lines changed

doc/source/whatsnew/v1.2.0.rst

+3-10
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,11 @@ Enhancements
1515

1616
.. _whatsnew_120.improve_timeseries_accessor_naming:
1717
Improve Timeseries accessors Naming Convention
18-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19-
20-
In modules like Period, PeriodArray, and Timestamp, there are non-Pythonic property names ``dayofweek`` and ``dayofyear`` (:issue:`9606`). We continue to maintain backwards-compatibility by simply aliasing the old naming convention to the new, properly named functions ``day_of_week`` and ``day_of_year``.
21-
22-
For example:
23-
24-
.. ipython:: python
18+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2519

26-
import pandas as pd
20+
- Added ``day_of_week`` (compatibility alias ``dayofweek``) property to ``Timestamp``, ``DatetimeIndex``, ``Period``, ``PeriodIndex`` (:issue:`9605`)
2721

28-
per = pd.Period('2017-12-31 22:00', 'H')
29-
per.dayofweek == per.day_of_week == 6
22+
- Added ``day_of_year`` (compatibility alias ``dayofyear``) property to ``Timestamp``, ``DatetimeIndex``, ``Period``, ``PeriodIndex`` (:issue:`9605`)
3023

3124
.. _whatsnew_120.duplicate_labels:
3225

pandas/_libs/tslibs/nattype.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ class NaTType(_NaT):
357357

358358
week = property(fget=lambda self: np.nan)
359359
dayofyear = property(fget=lambda self: np.nan)
360+
day_of_year = property(fget=lambda self: np.nan)
360361
weekofyear = property(fget=lambda self: np.nan)
361362
days_in_month = property(fget=lambda self: np.nan)
362363
daysinmonth = property(fget=lambda self: np.nan)

pandas/_libs/tslibs/period.pyx

+7-7
Original file line numberDiff line numberDiff line change
@@ -1475,8 +1475,8 @@ cdef class _Period(PeriodMixin):
14751475
PeriodDtypeBase _dtype
14761476
BaseOffset freq
14771477

1478-
# Alias dayofweek function to day_of_week (GH-9606)
14791478
dayofweek = _Period.day_of_week
1479+
dayofyear = _Period.day_of_year
14801480

14811481
def __cinit__(self, int64_t ordinal, BaseOffset freq):
14821482
self.ordinal = ordinal
@@ -1989,7 +1989,7 @@ cdef class _Period(PeriodMixin):
19891989
return self.dayofweek
19901990

19911991
@property
1992-
def dayofyear(self) -> int:
1992+
def day_of_year(self) -> int:
19931993
"""
19941994
Return the day of the year.
19951995

@@ -2005,19 +2005,19 @@ cdef class _Period(PeriodMixin):
20052005
See Also
20062006
--------
20072007
Period.day : Return the day of the month.
2008-
Period.dayofweek : Return the day of week.
2009-
PeriodIndex.dayofyear : Return the day of year of all indexes.
2008+
Period.day_of_week : Return the day of week.
2009+
PeriodIndex.day_of_year : Return the day of year of all indexes.
20102010

20112011
Examples
20122012
--------
20132013
>>> period = pd.Period("2015-10-23", freq='H')
2014-
>>> period.dayofyear
2014+
>>> period.day_of_year
20152015
296
20162016
>>> period = pd.Period("2012-12-31", freq='D')
2017-
>>> period.dayofyear
2017+
>>> period.day_of_year
20182018
366
20192019
>>> period = pd.Period("2013-01-01", freq='D')
2020-
>>> period.dayofyear
2020+
>>> period.day_of_year
20212021
1
20222022
"""
20232023
base = self._dtype._dtype_code

pandas/_libs/tslibs/timestamps.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ cdef class _Timestamp(ABCTimestamp):
230230

231231
# higher than np.ndarray and np.matrix
232232
__array_priority__ = 100
233-
# Alias dayofweek function to day_of_week (GH-9606)
234233
dayofweek = _Timestamp.day_of_week
234+
dayofyear = _Timestamp.day_of_year
235235

236236
def __hash__(_Timestamp self):
237237
if self.nanosecond:
@@ -547,7 +547,7 @@ cdef class _Timestamp(ABCTimestamp):
547547
return self.weekday()
548548

549549
@property
550-
def dayofyear(self) -> int:
550+
def day_of_year(self) -> int:
551551
"""
552552
Return the day of the year.
553553
"""

pandas/core/arrays/datetimes.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ class DatetimeArray(dtl.TimelikeOps, dtl.DatelikeOps):
179179
"dayofweek",
180180
"day_of_week",
181181
"dayofyear",
182+
"day_of_year",
182183
"quarter",
183184
"days_in_month",
184185
"daysinmonth",
@@ -1538,13 +1539,14 @@ def weekofyear(self):
15381539
dayofweek = day_of_week
15391540
weekday = day_of_week
15401541

1541-
dayofyear = _field_accessor(
1542+
day_of_year = _field_accessor(
15421543
"dayofyear",
15431544
"doy",
15441545
"""
15451546
The ordinal day of the year.
15461547
""",
15471548
)
1549+
dayofyear = day_of_year
15481550
quarter = _field_accessor(
15491551
"quarter",
15501552
"q",

pandas/core/arrays/period.py

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ class PeriodArray(PeriodMixin, dtl.DatelikeOps):
142142
"dayofweek",
143143
"day_of_week",
144144
"dayofyear",
145+
"day_of_year",
145146
"quarter",
146147
"qyear",
147148
"days_in_month",

pandas/core/indexes/datetimes.py

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class DatetimeIndex(DatetimeTimedeltaMixin):
158158
time
159159
timetz
160160
dayofyear
161+
day_of_year
161162
weekofyear
162163
week
163164
dayofweek

pandas/core/indexes/period.py

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class PeriodIndex(DatetimeIndexOpsMixin, Int64Index):
9797
dayofweek
9898
day_of_week
9999
dayofyear
100+
day_of_year
100101
days_in_month
101102
daysinmonth
102103
end_time

pandas/tests/generic/test_finalize.py

+1
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ def test_datetime_method(method):
680680
"dayofweek",
681681
"day_of_week",
682682
"dayofyear",
683+
"day_of_year",
683684
"quarter",
684685
"is_month_start",
685686
"is_month_end",

pandas/tests/indexes/datetimes/test_scalar_compat.py

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def test_dti_date_out_of_range(self, data):
4040
"dayofweek",
4141
"day_of_week",
4242
"dayofyear",
43+
"day_of_year",
4344
"quarter",
4445
"days_in_month",
4546
"is_month_start",

pandas/tests/indexes/period/test_period.py

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ def _check_all_fields(self, periodindex):
251251
"dayofweek",
252252
"day_of_week",
253253
"dayofyear",
254+
"day_of_year",
254255
"quarter",
255256
"qyear",
256257
"days_in_month",

pandas/tests/scalar/timestamp/test_timestamp.py

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def check(value, equal):
6666
check(ts.day_of_week, 6)
6767
check(ts.quarter, 2)
6868
check(ts.dayofyear, 130)
69+
check(ts.day_of_year, 130)
6970
check(ts.week, 19)
7071
check(ts.daysinmonth, 31)
7172
check(ts.daysinmonth, 31)
@@ -87,6 +88,7 @@ def check(value, equal):
8788
check(ts.day_of_week, 2)
8889
check(ts.quarter, 4)
8990
check(ts.dayofyear, 365)
91+
check(ts.day_of_year, 365)
9092
check(ts.week, 1)
9193
check(ts.daysinmonth, 31)
9294

0 commit comments

Comments
 (0)