Repository: pandas-dev/pandas-stubs (language: python)

Issue:
`DateOffset` for `weekday` argument requires `weekday` argument from `dateutil`
The issue is that the type specification for the `weekday` argument in `pandas-stubs/_libs/tslibs/offsets.pyi` says that `weekday`  is an `int`.  But it can be a `weekday` class or an `int`.  See https://dateutil.readthedocs.io/en/stable/relativedelta.html  To fix this, we have to make `types-python-dateutil` a dependency, which involves modifying `pyproject.toml` and takes some care.

Created a docs issue for pandas at https://github.com/pandas-dev/pandas/issues/52650

_Originally posted by @Dr-Irv in https://github.com/pandas-dev/pandas-stubs/issues/645#issuecomment-1507053774_
            



Discussion / hints:
In quite a few cases, the code seems to explicitly test that weekday is between 0 and 6 (WeekOfMonthMixin, Week, WeekOfMonth).

Fix patch:
diff --git a/pandas-stubs/_libs/tslibs/offsets.pyi b/pandas-stubs/_libs/tslibs/offsets.pyi
index d2265883..1acdadf7 100644
--- a/pandas-stubs/_libs/tslibs/offsets.pyi
+++ b/pandas-stubs/_libs/tslibs/offsets.pyi
@@ -12,6 +12,7 @@ from typing import (
     overload,
 )
 
+from dateutil.relativedelta import weekday as WeekdayClass
 import numpy as np
 from pandas.core.indexes.datetimes import DatetimeIndex
 from typing_extensions import Self
@@ -257,7 +258,7 @@ class DateOffset(RelativeDeltaOffset):
         year: int = ...,
         month: int = ...,
         day: int = ...,
-        weekday: int = ...,
+        weekday: int | WeekdayClass = ...,
         hour: int = ...,
         minute: int = ...,
         second: int = ...,


Test patch:
diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py
index 1d7a11bf..ed6cf910 100644
--- a/tests/test_timefuncs.py
+++ b/tests/test_timefuncs.py
@@ -8,6 +8,15 @@
     cast,
 )
 
+from dateutil.relativedelta import (
+    FR,
+    MO,
+    SA,
+    SU,
+    TH,
+    TU,
+    WE,
+)
 import numpy as np
 from numpy import typing as npt
 import pandas as pd
@@ -1284,6 +1293,42 @@ def test_weekofmonth_init():
     )
 
 
+def test_dateoffset_weekday() -> None:
+    """Check that you can create a `pd.DateOffset` from weekday of int or relativedelta.weekday."""
+    check(
+        assert_type(pd.offsets.DateOffset(weekday=1), pd.offsets.DateOffset),
+        pd.offsets.DateOffset,
+    )
+    check(
+        assert_type(pd.offsets.DateOffset(weekday=MO), pd.offsets.DateOffset),
+        pd.offsets.DateOffset,
+    )
+    check(
+        assert_type(pd.offsets.DateOffset(weekday=TU), pd.offsets.DateOffset),
+        pd.offsets.DateOffset,
+    )
+    check(
+        assert_type(pd.offsets.DateOffset(weekday=WE), pd.offsets.DateOffset),
+        pd.offsets.DateOffset,
+    )
+    check(
+        assert_type(pd.offsets.DateOffset(weekday=TH), pd.offsets.DateOffset),
+        pd.offsets.DateOffset,
+    )
+    check(
+        assert_type(pd.offsets.DateOffset(weekday=FR), pd.offsets.DateOffset),
+        pd.offsets.DateOffset,
+    )
+    check(
+        assert_type(pd.offsets.DateOffset(weekday=SA), pd.offsets.DateOffset),
+        pd.offsets.DateOffset,
+    )
+    check(
+        assert_type(pd.offsets.DateOffset(weekday=SU), pd.offsets.DateOffset),
+        pd.offsets.DateOffset,
+    )
+
+
 def test_date_range_unit():
     check(
         assert_type(