Skip to content

Commit c80451a

Browse files
committed
Deprecate week and weekofyear in Series.dt/DatetimeIndex
1 parent 3d4f9dc commit c80451a

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

pandas/core/indexes/accessors.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,32 @@ def isocalendar(self):
250250
"""
251251
return self._get_values().isocalendar().set_index(self._parent.index)
252252

253+
@property
254+
def weekofyear(self):
255+
"""
256+
The week ordinal of the year.
257+
258+
.. deprecated:: 1.1.0
259+
260+
Series.dt.weekofyear and Series.dt.week have been deprecated.
261+
Please use Series.dt.isocalendar().week instead.
262+
"""
263+
import warnings
264+
265+
warnings.warn(
266+
"Series.dt.weekofyear and Series.dt.week have been deprecated. "
267+
"Please use Series.dt.isocalendar().week instead.",
268+
FutureWarning,
269+
stacklevel=2,
270+
)
271+
week_series = self.isocalendar().week
272+
week_series.name = self.name
273+
if week_series.hasnans:
274+
return week_series.astype("float64")
275+
return week_series.astype("int64")
276+
277+
week = weekofyear
278+
253279

254280
@delegate_names(
255281
delegate=TimedeltaArray, accessors=TimedeltaArray._datetimelike_ops, typ="property"

pandas/core/indexes/datetimes.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,32 @@ def indexer_between_time(
809809

810810
return mask.nonzero()[0]
811811

812+
@property
813+
def weekofyear(self):
814+
"""
815+
The week ordinal of the year.
816+
817+
.. deprecated:: 1.1.0
818+
819+
weekofyear and week have been deprecated.
820+
Please use DatetimeIndex.isocalendar().week instead.
821+
"""
822+
import pandas as pd
823+
import warnings
824+
825+
warnings.warn(
826+
"weekofyear and week have been deprecated, please use "
827+
"DatetimeIndex.isocalendar().week instead, which returns "
828+
"a Series. To exactly reproduce the behavior of week and "
829+
"weekofyear and return an Index, you may call "
830+
"pd.Int64Index(idx.isocalendar().week)",
831+
FutureWarning,
832+
stacklevel=2,
833+
)
834+
return pd.Int64Index(self.isocalendar().week)
835+
836+
week = weekofyear
837+
812838

813839
DatetimeIndex._add_numeric_methods_disabled()
814840
DatetimeIndex._add_logical_methods_disabled()

pandas/tests/indexes/datetimes/test_misc.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,14 @@ def test_iter_readonly():
375375
list(dti)
376376

377377

378+
def test_week_and_weekofyear_are_deprecated():
379+
idx = pd.date_range(start="2019-12-29", freq="D", periods=4)
380+
with tm.assert_produces_warning(FutureWarning):
381+
idx.week
382+
with tm.assert_produces_warning(FutureWarning):
383+
idx.weekofyear
384+
385+
378386
def test_isocalendar_returns_correct_values_close_to_new_year_with_tz():
379387
# GH 6538: Check that DatetimeIndex and its TimeStamp elements
380388
# return the same weekofyear accessor close to new year w/ tz

pandas/tests/series/test_datetime_values.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def test_dt_namespace_accessor(self):
5050
"day_name",
5151
"month_name",
5252
"isocalendar",
53+
"week",
54+
"weekofyear",
5355
]
5456
ok_for_td = TimedeltaIndex._datetimelike_ops
5557
ok_for_td_methods = [
@@ -684,3 +686,11 @@ def test_isocalendar(self, input_series, expected_output):
684686
expected_output, columns=["year", "week", "day"], dtype="UInt32"
685687
)
686688
tm.assert_frame_equal(result, expected_frame)
689+
690+
691+
def test_week_and_weekofyear_are_deprecated():
692+
series = pd.to_datetime(pd.Series(["2020-01-01"]))
693+
with tm.assert_produces_warning(FutureWarning):
694+
series.dt.week
695+
with tm.assert_produces_warning(FutureWarning):
696+
series.dt.weekofyear

0 commit comments

Comments
 (0)