Skip to content

DEPR: Remove week & weekofyear for datetimes #49380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ I want to add a new column to the ``DataFrame`` containing only the month of the

By using ``Timestamp`` objects for dates, a lot of time-related
properties are provided by pandas. For example the ``month``, but also
``year``, ``weekofyear``, ``quarter``,… All of these properties are
``year``, ``quarter``,… All of these properties are
accessible by the ``dt`` accessor.

.. raw:: html
Expand Down
2 changes: 0 additions & 2 deletions doc/source/reference/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,6 @@ Time/date components
DatetimeIndex.timetz
DatetimeIndex.dayofyear
DatetimeIndex.day_of_year
DatetimeIndex.weekofyear
DatetimeIndex.week
DatetimeIndex.dayofweek
DatetimeIndex.day_of_week
DatetimeIndex.weekday
Expand Down
2 changes: 0 additions & 2 deletions doc/source/reference/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,6 @@ Datetime properties
Series.dt.second
Series.dt.microsecond
Series.dt.nanosecond
Series.dt.week
Series.dt.weekofyear
Series.dt.dayofweek
Series.dt.day_of_week
Series.dt.weekday
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ Removal of prior version deprecations/changes
- Removed deprecated :meth:`Index.to_native_types`, use ``obj.astype(str)`` instead (:issue:`36418`)
- Removed deprecated :meth:`Series.iteritems`, :meth:`DataFrame.iteritems`, use ``obj.items`` instead (:issue:`45321`)
- Removed deprecated :meth:`DatetimeIndex.union_many` (:issue:`45018`)
- Removed deprecated ``weekofyear`` and ``week`` attributes of :class:`DatetimeArray`, :class:`DatetimeIndex` and ``dt`` accessor in favor of ``isocalendar().week`` (:issue:`33595`)
- Removed deprecated :meth:`RangeIndex._start`, :meth:`RangeIndex._stop`, :meth:`RangeIndex._step`, use ``start``, ``stop``, ``step`` instead (:issue:`30482`)
- Removed deprecated :meth:`DatetimeIndex.to_perioddelta`, Use ``dtindex - dtindex.to_period(freq).to_timestamp()`` instead (:issue:`34853`)
- Enforced deprecation disallowing passing a timezone-aware :class:`Timestamp` and ``dtype="datetime64[ns]"`` to :class:`Series` or :class:`DataFrame` constructors (:issue:`41555`)
Expand Down
28 changes: 0 additions & 28 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,6 @@ def _scalar_type(self) -> type[Timestamp]:
"hour",
"minute",
"second",
"weekofyear",
"week",
"weekday",
"dayofweek",
"day_of_week",
Expand Down Expand Up @@ -1365,32 +1363,6 @@ def isocalendar(self) -> DataFrame:
iso_calendar_df.iloc[self._isnan] = None
return iso_calendar_df

@property
def weekofyear(self):
"""
The week ordinal of the year.

.. deprecated:: 1.1.0

weekofyear and week have been deprecated.
Please use DatetimeIndex.isocalendar().week instead.
"""
warnings.warn(
"weekofyear and week have been deprecated, please use "
"DatetimeIndex.isocalendar().week instead, which returns "
"a Series. To exactly reproduce the behavior of week and "
"weekofyear and return an Index, you may call "
"pd.Int64Index(idx.isocalendar().week)",
FutureWarning,
stacklevel=find_stack_level(),
)
week_series = self.isocalendar().week
if week_series.hasnans:
return week_series.to_numpy(dtype="float64", na_value=np.nan)
return week_series.to_numpy(dtype="int64")

week = weekofyear

year = _field_accessor(
"year",
"Y",
Expand Down
28 changes: 0 additions & 28 deletions pandas/core/indexes/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
from __future__ import annotations

from typing import TYPE_CHECKING
import warnings

import numpy as np

from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.common import (
is_categorical_dtype,
is_datetime64_dtype,
Expand Down Expand Up @@ -276,31 +273,6 @@ def isocalendar(self) -> DataFrame:
"""
return self._get_values().isocalendar().set_index(self._parent.index)

@property
def weekofyear(self):
"""
The week ordinal of the year according to the ISO 8601 standard.

.. deprecated:: 1.1.0

Series.dt.weekofyear and Series.dt.week have been deprecated. Please
call :func:`Series.dt.isocalendar` and access the ``week`` column
instead.
"""
warnings.warn(
"Series.dt.weekofyear and Series.dt.week have been deprecated. "
"Please use Series.dt.isocalendar().week instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
week_series = self.isocalendar().week
week_series.name = self.name
if week_series.hasnans:
return week_series.astype("float64")
return week_series.astype("int64")

week = weekofyear


@delegate_names(
delegate=TimedeltaArray, accessors=TimedeltaArray._datetimelike_ops, typ="property"
Expand Down
11 changes: 2 additions & 9 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,18 +805,11 @@ def test_bool_properties(self, arr1d, propname):

@pytest.mark.parametrize("propname", DatetimeArray._field_ops)
def test_int_properties(self, arr1d, propname):
warn = None
msg = "weekofyear and week have been deprecated, please use"
if propname in ["week", "weekofyear"]:
# GH#33595 Deprecate week and weekofyear
warn = FutureWarning

dti = self.index_cls(arr1d)
arr = arr1d

with tm.assert_produces_warning(warn, match=msg):
result = getattr(arr, propname)
expected = np.array(getattr(dti, propname), dtype=result.dtype)
result = getattr(arr, propname)
expected = np.array(getattr(dti, propname), dtype=result.dtype)

tm.assert_numpy_array_equal(result, expected)

Expand Down
3 changes: 0 additions & 3 deletions pandas/tests/arrays/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ def test_non_nano(self, unit, reso, dtype):
assert tz_compare(dta.tz, dta[0].tz)
assert (dta[0] == dta[:1]).all()

@pytest.mark.filterwarnings(
"ignore:weekofyear and week have been deprecated:FutureWarning"
)
@pytest.mark.parametrize(
"field", DatetimeArray._field_ops + DatetimeArray._bool_ops
)
Expand Down
12 changes: 0 additions & 12 deletions pandas/tests/indexes/datetimes/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ def test_datetimeindex_accessors(self):

# non boolean accessors -> return Index
for accessor in DatetimeArray._field_ops:
if accessor in ["week", "weekofyear"]:
# GH#33595 Deprecate week and weekofyear
continue
res = getattr(dti, accessor)
assert len(res) == 365
assert isinstance(res, Index)
Expand Down Expand Up @@ -287,15 +284,6 @@ def test_iter_readonly():
list(dti)


def test_week_and_weekofyear_are_deprecated():
# GH#33595 Deprecate week and weekofyear
idx = date_range(start="2019-12-29", freq="D", periods=4)
with tm.assert_produces_warning(FutureWarning):
idx.week
with tm.assert_produces_warning(FutureWarning):
idx.weekofyear


def test_add_timedelta_preserves_freq():
# GH#37295 should hold for any DTI with freq=None or Tick freq
tz = "Canada/Eastern"
Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/scalar/test_nat.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ def test_nat_vector_field_access():
# on NaT/Timestamp for compat with datetime
if field == "weekday":
continue
if field in ["week", "weekofyear"]:
# GH#33595 Deprecate week and weekofyear
continue

result = getattr(idx, field)
expected = Index([getattr(x, field) for x in idx])
Expand All @@ -88,9 +85,6 @@ def test_nat_vector_field_access():
# on NaT/Timestamp for compat with datetime
if field == "weekday":
continue
if field in ["week", "weekofyear"]:
# GH#33595 Deprecate week and weekofyear
continue

result = getattr(ser.dt, field)
expected = [getattr(x, field) for x in idx]
Expand Down
3 changes: 0 additions & 3 deletions pandas/tests/series/accessors/test_cat_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,6 @@ def test_dt_accessor_api_for_categorical(self, idx):
tm.assert_equal(res, exp)

for attr in attr_names:
if attr in ["week", "weekofyear"]:
# GH#33595 Deprecate week and weekofyear
continue
res = getattr(cat.dt, attr)
exp = getattr(ser.dt, attr)

Expand Down
15 changes: 2 additions & 13 deletions pandas/tests/series/accessors/test_dt_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ def test_dt_namespace_accessor_datetime64(self, freq):

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

for prop in ok_for_dt_methods:
Expand Down Expand Up @@ -146,8 +145,7 @@ def test_dt_namespace_accessor_datetime64tz(self):
for prop in ok_for_dt:

# we test freq below
# we ignore week and weekofyear because they are deprecated
if prop not in ["freq", "week", "weekofyear"]:
if prop != "freq":
self._compare(ser, prop)

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


def test_week_and_weekofyear_are_deprecated():
# GH#33595 Deprecate week and weekofyear
series = pd.to_datetime(Series(["2020-01-01"]))
with tm.assert_produces_warning(FutureWarning):
series.dt.week
with tm.assert_produces_warning(FutureWarning):
series.dt.weekofyear


def test_normalize_pre_epoch_dates():
# GH: 36294
ser = pd.to_datetime(Series(["1969-01-01 09:00:00", "2016-01-01 09:00:00"]))
Expand Down