Skip to content

Commit d1524a4

Browse files
CLN: Deprecate dayofweek/hello day_of_week (pandas-dev#9606)
Rename dayofweek properties in Period, Datetime, and PeriodArray classes. Add day_of_week property to respective tests for each class.
1 parent 18b4864 commit d1524a4

File tree

11 files changed

+44
-15
lines changed

11 files changed

+44
-15
lines changed

pandas/_libs/tslibs/nattype.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,8 @@ class NaTType(_NaT):
361361
days_in_month = property(fget=lambda self: np.nan)
362362
daysinmonth = property(fget=lambda self: np.nan)
363363
dayofweek = property(fget=lambda self: np.nan)
364-
364+
day_of_week = property(fget=lambda self: np.nan)
365+
365366
# inject Timedelta properties
366367
days = property(fget=lambda self: np.nan)
367368
seconds = property(fget=lambda self: np.nan)

pandas/_libs/tslibs/period.pyx

+14-9
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,7 @@ cdef accessor _get_accessor_func(str field):
13731373
return <accessor>pweek
13741374
elif field == "day_of_year":
13751375
return <accessor>pday_of_year
1376-
elif field == "weekday":
1376+
elif field == "weekday" or field == "day_of_week":
13771377
return <accessor>pweekday
13781378
elif field == "days_in_month":
13791379
return <accessor>pdays_in_month
@@ -1882,7 +1882,7 @@ cdef class _Period(PeriodMixin):
18821882
return self.weekofyear
18831883

18841884
@property
1885-
def dayofweek(self) -> int:
1885+
def day_of_week(self) -> int:
18861886
"""
18871887
Day of the week the period lies in, with Monday=0 and Sunday=6.
18881888

@@ -1900,38 +1900,43 @@ cdef class _Period(PeriodMixin):
19001900

19011901
See Also
19021902
--------
1903-
Period.dayofweek : Day of the week the period lies in.
1904-
Period.weekday : Alias of Period.dayofweek.
1903+
Period.day_of_week : Day of the week the period lies in.
1904+
Period.weekday : Alias of Period.day_of_week.
19051905
Period.day : Day of the month.
19061906
Period.dayofyear : Day of the year.
19071907

19081908
Examples
19091909
--------
19101910
>>> per = pd.Period('2017-12-31 22:00', 'H')
1911-
>>> per.dayofweek
1911+
>>> per.day_of_week
19121912
6
19131913

19141914
For periods that span over multiple days, the day at the beginning of
19151915
the period is returned.
19161916

19171917
>>> per = pd.Period('2017-12-31 22:00', '4H')
1918-
>>> per.dayofweek
1918+
>>> per.day_of_week
19191919
6
1920-
>>> per.start_time.dayofweek
1920+
>>> per.start_time.day_of_week
19211921
6
19221922

19231923
For periods with a frequency higher than days, the last day of the
19241924
period is returned.
19251925

19261926
>>> per = pd.Period('2018-01', 'M')
1927-
>>> per.dayofweek
1927+
>>> per.day_of_week
19281928
2
1929-
>>> per.end_time.dayofweek
1929+
>>> per.end_time.day_of_week
19301930
2
19311931
"""
19321932
base = self._dtype._dtype_code
19331933
return pweekday(self.ordinal, base)
19341934

1935+
@property
1936+
def dayofweek(self) -> int:
1937+
"""This property is deprecated. Please use day_of_week instead."""
1938+
return self.day_of_week
1939+
19351940
@property
19361941
def weekday(self) -> int:
19371942
"""

pandas/_libs/tslibs/timestamps.pyx

+8-1
Original file line numberDiff line numberDiff line change
@@ -538,12 +538,19 @@ cdef class _Timestamp(ABCTimestamp):
538538
return bool(ccalendar.is_leapyear(self.year))
539539

540540
@property
541-
def dayofweek(self) -> int:
541+
def day_of_week(self) -> int:
542542
"""
543543
Return day of the week.
544544
"""
545545
return self.weekday()
546546

547+
@property
548+
def dayofweek(self) -> int:
549+
"""
550+
Deprecated. Use day_of_week instead.
551+
"""
552+
return self.day_of_week
553+
547554
@property
548555
def dayofyear(self) -> int:
549556
"""

pandas/core/arrays/datetimes.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class DatetimeArray(dtl.TimelikeOps, dtl.DatelikeOps):
177177
"week",
178178
"weekday",
179179
"dayofweek",
180+
"day_of_week",
180181
"dayofyear",
181182
"quarter",
182183
"days_in_month",
@@ -1533,8 +1534,9 @@ def weekofyear(self):
15331534
2017-01-08 6
15341535
Freq: D, dtype: int64
15351536
"""
1536-
dayofweek = _field_accessor("dayofweek", "dow", _dayofweek_doc)
1537-
weekday = dayofweek
1537+
day_of_week = _field_accessor("day_of_week", "dow", _dayofweek_doc)
1538+
dayofweek = day_of_week
1539+
weekday = day_of_week
15381540

15391541
dayofyear = _field_accessor(
15401542
"dayofyear",

pandas/core/arrays/period.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class PeriodArray(PeriodMixin, dtl.DatelikeOps):
140140
"weekday",
141141
"week",
142142
"dayofweek",
143+
"day_of_week",
143144
"dayofyear",
144145
"quarter",
145146
"qyear",
@@ -381,12 +382,13 @@ def __arrow_array__(self, type=None):
381382
""",
382383
)
383384
week = weekofyear
384-
dayofweek = _field_accessor(
385-
"weekday",
385+
day_of_week = _field_accessor(
386+
"day_of_week",
386387
"""
387388
The day of the week with Monday=0, Sunday=6.
388389
""",
389390
)
391+
dayofweek = day_of_week
390392
weekday = dayofweek
391393
dayofyear = day_of_year = _field_accessor(
392394
"day_of_year",

pandas/core/indexes/datetimes.py

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class DatetimeIndex(DatetimeTimedeltaMixin):
161161
weekofyear
162162
week
163163
dayofweek
164+
day_of_week
164165
weekday
165166
quarter
166167
tz

pandas/core/indexes/period.py

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class PeriodIndex(DatetimeIndexOpsMixin, Int64Index):
9595
----------
9696
day
9797
dayofweek
98+
day_of_week
9899
dayofyear
99100
days_in_month
100101
daysinmonth

pandas/tests/generic/test_finalize.py

+2
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,8 @@ def test_datetime_method(method):
678678
"microsecond",
679679
"nanosecond",
680680
"dayofweek",
681+
# Add better named field for GH-9606
682+
"day_of_week",
681683
"dayofyear",
682684
"quarter",
683685
"is_month_start",

pandas/tests/indexes/datetimes/test_scalar_compat.py

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def test_dti_date_out_of_range(self, data):
3838
"field",
3939
[
4040
"dayofweek",
41+
# Add better named field for GH-9606
42+
"day_of_week",
4143
"dayofyear",
4244
"quarter",
4345
"days_in_month",

pandas/tests/indexes/period/test_period.py

+2
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ def _check_all_fields(self, periodindex):
249249
"weekofyear",
250250
"week",
251251
"dayofweek",
252+
# Add better named field for GH-9606
253+
"day_of_week",
252254
"dayofyear",
253255
"quarter",
254256
"qyear",

pandas/tests/scalar/timestamp/test_timestamp.py

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def test_properties_business(self):
2626
ts = Timestamp("2017-10-01", freq="B")
2727
control = Timestamp("2017-10-01")
2828
assert ts.dayofweek == 6
29+
assert ts.day_of_week == 6
2930
assert not ts.is_month_start # not a weekday
3031
assert not ts.is_quarter_start # not a weekday
3132
# Control case: non-business is month/qtr start
@@ -35,6 +36,7 @@ def test_properties_business(self):
3536
ts = Timestamp("2017-09-30", freq="B")
3637
control = Timestamp("2017-09-30")
3738
assert ts.dayofweek == 5
39+
assert ts.day_of_week == 5
3840
assert not ts.is_month_end # not a weekday
3941
assert not ts.is_quarter_end # not a weekday
4042
# Control case: non-business is month/qtr start
@@ -61,6 +63,7 @@ def check(value, equal):
6163
check(ts.microsecond, 100)
6264
check(ts.nanosecond, 1)
6365
check(ts.dayofweek, 6)
66+
check(ts.day_of_week, 6)
6467
check(ts.quarter, 2)
6568
check(ts.dayofyear, 130)
6669
check(ts.week, 19)
@@ -81,6 +84,7 @@ def check(value, equal):
8184
check(ts.microsecond, 0)
8285
check(ts.nanosecond, 0)
8386
check(ts.dayofweek, 2)
87+
check(ts.day_of_week, 2)
8488
check(ts.quarter, 4)
8589
check(ts.dayofyear, 365)
8690
check(ts.week, 1)

0 commit comments

Comments
 (0)