Skip to content

Commit 8258b03

Browse files
mroeschkenoatamir
authored andcommitted
DEPR: Remove week & weekofyear for datetimes (pandas-dev#49380)
1 parent ad6ae60 commit 8258b03

File tree

12 files changed

+6
-107
lines changed

12 files changed

+6
-107
lines changed

doc/source/getting_started/intro_tutorials/09_timeseries.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ I want to add a new column to the ``DataFrame`` containing only the month of the
144144
145145
By using ``Timestamp`` objects for dates, a lot of time-related
146146
properties are provided by pandas. For example the ``month``, but also
147-
``year``, ``weekofyear``, ``quarter``,… All of these properties are
147+
``year``, ``quarter``,… All of these properties are
148148
accessible by the ``dt`` accessor.
149149

150150
.. raw:: html

doc/source/reference/indexing.rst

-2
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,6 @@ Time/date components
343343
DatetimeIndex.timetz
344344
DatetimeIndex.dayofyear
345345
DatetimeIndex.day_of_year
346-
DatetimeIndex.weekofyear
347-
DatetimeIndex.week
348346
DatetimeIndex.dayofweek
349347
DatetimeIndex.day_of_week
350348
DatetimeIndex.weekday

doc/source/reference/series.rst

-2
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,6 @@ Datetime properties
311311
Series.dt.second
312312
Series.dt.microsecond
313313
Series.dt.nanosecond
314-
Series.dt.week
315-
Series.dt.weekofyear
316314
Series.dt.dayofweek
317315
Series.dt.day_of_week
318316
Series.dt.weekday

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Removal of prior version deprecations/changes
170170
- Removed deprecated :meth:`Index.to_native_types`, use ``obj.astype(str)`` instead (:issue:`36418`)
171171
- Removed deprecated :meth:`Series.iteritems`, :meth:`DataFrame.iteritems`, use ``obj.items`` instead (:issue:`45321`)
172172
- Removed deprecated :meth:`DatetimeIndex.union_many` (:issue:`45018`)
173+
- Removed deprecated ``weekofyear`` and ``week`` attributes of :class:`DatetimeArray`, :class:`DatetimeIndex` and ``dt`` accessor in favor of ``isocalendar().week`` (:issue:`33595`)
173174
- Removed deprecated :meth:`RangeIndex._start`, :meth:`RangeIndex._stop`, :meth:`RangeIndex._step`, use ``start``, ``stop``, ``step`` instead (:issue:`30482`)
174175
- Removed deprecated :meth:`DatetimeIndex.to_perioddelta`, Use ``dtindex - dtindex.to_period(freq).to_timestamp()`` instead (:issue:`34853`)
175176
- Enforced deprecation disallowing passing a timezone-aware :class:`Timestamp` and ``dtype="datetime64[ns]"`` to :class:`Series` or :class:`DataFrame` constructors (:issue:`41555`)

pandas/core/arrays/datetimes.py

-28
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,6 @@ def _scalar_type(self) -> type[Timestamp]:
216216
"hour",
217217
"minute",
218218
"second",
219-
"weekofyear",
220-
"week",
221219
"weekday",
222220
"dayofweek",
223221
"day_of_week",
@@ -1365,32 +1363,6 @@ def isocalendar(self) -> DataFrame:
13651363
iso_calendar_df.iloc[self._isnan] = None
13661364
return iso_calendar_df
13671365

1368-
@property
1369-
def weekofyear(self):
1370-
"""
1371-
The week ordinal of the year.
1372-
1373-
.. deprecated:: 1.1.0
1374-
1375-
weekofyear and week have been deprecated.
1376-
Please use DatetimeIndex.isocalendar().week instead.
1377-
"""
1378-
warnings.warn(
1379-
"weekofyear and week have been deprecated, please use "
1380-
"DatetimeIndex.isocalendar().week instead, which returns "
1381-
"a Series. To exactly reproduce the behavior of week and "
1382-
"weekofyear and return an Index, you may call "
1383-
"pd.Int64Index(idx.isocalendar().week)",
1384-
FutureWarning,
1385-
stacklevel=find_stack_level(),
1386-
)
1387-
week_series = self.isocalendar().week
1388-
if week_series.hasnans:
1389-
return week_series.to_numpy(dtype="float64", na_value=np.nan)
1390-
return week_series.to_numpy(dtype="int64")
1391-
1392-
week = weekofyear
1393-
13941366
year = _field_accessor(
13951367
"year",
13961368
"Y",

pandas/core/indexes/accessors.py

-28
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
from __future__ import annotations
55

66
from typing import TYPE_CHECKING
7-
import warnings
87

98
import numpy as np
109

11-
from pandas.util._exceptions import find_stack_level
12-
1310
from pandas.core.dtypes.common import (
1411
is_categorical_dtype,
1512
is_datetime64_dtype,
@@ -276,31 +273,6 @@ def isocalendar(self) -> DataFrame:
276273
"""
277274
return self._get_values().isocalendar().set_index(self._parent.index)
278275

279-
@property
280-
def weekofyear(self):
281-
"""
282-
The week ordinal of the year according to the ISO 8601 standard.
283-
284-
.. deprecated:: 1.1.0
285-
286-
Series.dt.weekofyear and Series.dt.week have been deprecated. Please
287-
call :func:`Series.dt.isocalendar` and access the ``week`` column
288-
instead.
289-
"""
290-
warnings.warn(
291-
"Series.dt.weekofyear and Series.dt.week have been deprecated. "
292-
"Please use Series.dt.isocalendar().week instead.",
293-
FutureWarning,
294-
stacklevel=find_stack_level(),
295-
)
296-
week_series = self.isocalendar().week
297-
week_series.name = self.name
298-
if week_series.hasnans:
299-
return week_series.astype("float64")
300-
return week_series.astype("int64")
301-
302-
week = weekofyear
303-
304276

305277
@delegate_names(
306278
delegate=TimedeltaArray, accessors=TimedeltaArray._datetimelike_ops, typ="property"

pandas/tests/arrays/test_datetimelike.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -805,18 +805,11 @@ def test_bool_properties(self, arr1d, propname):
805805

806806
@pytest.mark.parametrize("propname", DatetimeArray._field_ops)
807807
def test_int_properties(self, arr1d, propname):
808-
warn = None
809-
msg = "weekofyear and week have been deprecated, please use"
810-
if propname in ["week", "weekofyear"]:
811-
# GH#33595 Deprecate week and weekofyear
812-
warn = FutureWarning
813-
814808
dti = self.index_cls(arr1d)
815809
arr = arr1d
816810

817-
with tm.assert_produces_warning(warn, match=msg):
818-
result = getattr(arr, propname)
819-
expected = np.array(getattr(dti, propname), dtype=result.dtype)
811+
result = getattr(arr, propname)
812+
expected = np.array(getattr(dti, propname), dtype=result.dtype)
820813

821814
tm.assert_numpy_array_equal(result, expected)
822815

pandas/tests/arrays/test_datetimes.py

-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ def test_non_nano(self, unit, reso, dtype):
7575
assert tz_compare(dta.tz, dta[0].tz)
7676
assert (dta[0] == dta[:1]).all()
7777

78-
@pytest.mark.filterwarnings(
79-
"ignore:weekofyear and week have been deprecated:FutureWarning"
80-
)
8178
@pytest.mark.parametrize(
8279
"field", DatetimeArray._field_ops + DatetimeArray._bool_ops
8380
)

pandas/tests/indexes/datetimes/test_misc.py

-12
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ def test_datetimeindex_accessors(self):
9898

9999
# non boolean accessors -> return Index
100100
for accessor in DatetimeArray._field_ops:
101-
if accessor in ["week", "weekofyear"]:
102-
# GH#33595 Deprecate week and weekofyear
103-
continue
104101
res = getattr(dti, accessor)
105102
assert len(res) == 365
106103
assert isinstance(res, Index)
@@ -287,15 +284,6 @@ def test_iter_readonly():
287284
list(dti)
288285

289286

290-
def test_week_and_weekofyear_are_deprecated():
291-
# GH#33595 Deprecate week and weekofyear
292-
idx = date_range(start="2019-12-29", freq="D", periods=4)
293-
with tm.assert_produces_warning(FutureWarning):
294-
idx.week
295-
with tm.assert_produces_warning(FutureWarning):
296-
idx.weekofyear
297-
298-
299287
def test_add_timedelta_preserves_freq():
300288
# GH#37295 should hold for any DTI with freq=None or Tick freq
301289
tz = "Canada/Eastern"

pandas/tests/scalar/test_nat.py

-6
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ def test_nat_vector_field_access():
7373
# on NaT/Timestamp for compat with datetime
7474
if field == "weekday":
7575
continue
76-
if field in ["week", "weekofyear"]:
77-
# GH#33595 Deprecate week and weekofyear
78-
continue
7976

8077
result = getattr(idx, field)
8178
expected = Index([getattr(x, field) for x in idx])
@@ -88,9 +85,6 @@ def test_nat_vector_field_access():
8885
# on NaT/Timestamp for compat with datetime
8986
if field == "weekday":
9087
continue
91-
if field in ["week", "weekofyear"]:
92-
# GH#33595 Deprecate week and weekofyear
93-
continue
9488

9589
result = getattr(ser.dt, field)
9690
expected = [getattr(x, field) for x in idx]

pandas/tests/series/accessors/test_cat_accessor.py

-3
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,6 @@ def test_dt_accessor_api_for_categorical(self, idx):
209209
tm.assert_equal(res, exp)
210210

211211
for attr in attr_names:
212-
if attr in ["week", "weekofyear"]:
213-
# GH#33595 Deprecate week and weekofyear
214-
continue
215212
res = getattr(cat.dt, attr)
216213
exp = getattr(ser.dt, attr)
217214

pandas/tests/series/accessors/test_dt_accessor.py

+2-13
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ def test_dt_namespace_accessor_datetime64(self, freq):
107107

108108
for prop in ok_for_dt:
109109
# we test freq below
110-
# we ignore week and weekofyear because they are deprecated
111-
if prop not in ["freq", "week", "weekofyear"]:
110+
if prop != "freq":
112111
self._compare(ser, prop)
113112

114113
for prop in ok_for_dt_methods:
@@ -146,8 +145,7 @@ def test_dt_namespace_accessor_datetime64tz(self):
146145
for prop in ok_for_dt:
147146

148147
# we test freq below
149-
# we ignore week and weekofyear because they are deprecated
150-
if prop not in ["freq", "week", "weekofyear"]:
148+
if prop != "freq":
151149
self._compare(ser, prop)
152150

153151
for prop in ok_for_dt_methods:
@@ -794,15 +792,6 @@ def test_to_period(self, input_vals):
794792
tm.assert_series_equal(result, expected)
795793

796794

797-
def test_week_and_weekofyear_are_deprecated():
798-
# GH#33595 Deprecate week and weekofyear
799-
series = pd.to_datetime(Series(["2020-01-01"]))
800-
with tm.assert_produces_warning(FutureWarning):
801-
series.dt.week
802-
with tm.assert_produces_warning(FutureWarning):
803-
series.dt.weekofyear
804-
805-
806795
def test_normalize_pre_epoch_dates():
807796
# GH: 36294
808797
ser = pd.to_datetime(Series(["1969-01-01 09:00:00", "2016-01-01 09:00:00"]))

0 commit comments

Comments
 (0)