Skip to content

Commit 4007381

Browse files
committed
Deprecate week and weekofyear in Series.dt/DatetimeIndex
1 parent 8282ee2 commit 4007381

File tree

4 files changed

+68
-8
lines changed

4 files changed

+68
-8
lines changed

pandas/core/arrays/datetimes.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,32 @@ def isocalendar(self):
12981298
iso_calendar_df.iloc[self._isnan] = None
12991299
return iso_calendar_df
13001300

1301+
@property
1302+
def weekofyear(self):
1303+
"""
1304+
The week ordinal of the year.
1305+
1306+
.. deprecated:: 1.1.0
1307+
1308+
weekofyear and week have been deprecated.
1309+
Please use DatetimeIndex.isocalendar().week instead.
1310+
"""
1311+
import pandas as pd
1312+
import warnings
1313+
1314+
warnings.warn(
1315+
"weekofyear and week have been deprecated, please use "
1316+
"DatetimeIndex.isocalendar().week instead, which returns "
1317+
"a Series. To exactly reproduce the behavior of week and "
1318+
"weekofyear and return an Index, you may call "
1319+
"pd.Int64Index(idx.isocalendar().week)",
1320+
FutureWarning,
1321+
stacklevel=3,
1322+
)
1323+
return pd.Int64Index(self.isocalendar().week)
1324+
1325+
week = weekofyear
1326+
13011327
year = _field_accessor(
13021328
"year",
13031329
"Y",
@@ -1482,14 +1508,6 @@ def isocalendar(self):
14821508
dtype: int64
14831509
""",
14841510
)
1485-
weekofyear = _field_accessor(
1486-
"weekofyear",
1487-
"woy",
1488-
"""
1489-
The week ordinal of the year.
1490-
""",
1491-
)
1492-
week = weekofyear
14931511
_dayofweek_doc = """
14941512
The day of the week with Monday=0, Sunday=6.
14951513

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/tests/indexes/datetimes/test_misc.py

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

385385

386+
def test_week_and_weekofyear_are_deprecated():
387+
idx = pd.date_range(start="2019-12-29", freq="D", periods=4)
388+
with tm.assert_produces_warning(FutureWarning):
389+
idx.week
390+
with tm.assert_produces_warning(FutureWarning):
391+
idx.weekofyear
392+
393+
386394
def test_isocalendar_returns_correct_values_close_to_new_year_with_tz():
387395
# GH 6538: Check that DatetimeIndex and its TimeStamp elements
388396
# return the same weekofyear accessor close to new year w/ tz

pandas/tests/series/test_datetime_values.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,3 +684,11 @@ def test_isocalendar(self, input_series, expected_output):
684684
expected_output, columns=["year", "week", "day"], dtype="UInt32"
685685
)
686686
tm.assert_frame_equal(result, expected_frame)
687+
688+
689+
def test_week_and_weekofyear_are_deprecated():
690+
series = pd.to_datetime(pd.Series(["2020-01-01"]))
691+
with tm.assert_produces_warning(FutureWarning):
692+
series.dt.week
693+
with tm.assert_produces_warning(FutureWarning):
694+
series.dt.weekofyear

0 commit comments

Comments
 (0)