From 5188c783f538eb7592df02622123443dba6a4330 Mon Sep 17 00:00:00 2001 From: Loic Diridollou Date: Sun, 29 Sep 2024 14:29:36 -0400 Subject: [PATCH 1/4] GH648 Allowing dateoffset weekday from relativedelta --- pandas-stubs/_libs/tslibs/offsets.pyi | 3 ++- tests/test_timefuncs.py | 31 ++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) 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 = ..., diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index 1d7a11bf..f3d58886 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 @@ -19,7 +28,12 @@ from pandas._libs import NaTType from pandas._libs.tslibs import BaseOffset -from pandas._libs.tslibs.offsets import DateOffset +from pandas._libs.tslibs.offsets import ( + DateOffset, + FY5253Mixin, + WeekOfMonth, + WeekOfMonthMixin, +) if TYPE_CHECKING: from pandas._typing import FulldatetimeDict @@ -1284,6 +1298,21 @@ def test_weekofmonth_init(): ) +def test_dateoffset_weekday(): + """Check that you can create a `pd.DateOffset` from weekday of int or dateutil.relativedelta.""" + # check for int + check( + assert_type(pd.offsets.DateOffset(weekday=1), pd.offsets.DateOffset), + pd.offsets.DateOffset, + ) + # check for relativedelta + for weekday in [MO, TU, WE, TH, TH, FR, SA, SU]: + check( + assert_type(pd.offsets.DateOffset(weekday=weekday), pd.offsets.DateOffset), + pd.offsets.DateOffset, + ) + + def test_date_range_unit(): check( assert_type( From 27b60240ae6a3592da760ae82258dac317338819 Mon Sep 17 00:00:00 2001 From: Loic Diridollou Date: Sun, 29 Sep 2024 14:49:38 -0400 Subject: [PATCH 2/4] Fix lint --- tests/test_timefuncs.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index f3d58886..5d0ebf01 100644 --- a/tests/test_timefuncs.py +++ b/tests/test_timefuncs.py @@ -28,12 +28,7 @@ from pandas._libs import NaTType from pandas._libs.tslibs import BaseOffset -from pandas._libs.tslibs.offsets import ( - DateOffset, - FY5253Mixin, - WeekOfMonth, - WeekOfMonthMixin, -) +from pandas._libs.tslibs.offsets import DateOffset if TYPE_CHECKING: from pandas._typing import FulldatetimeDict From 6001e4beee13ec4c188c32016a6d12be75bd0484 Mon Sep 17 00:00:00 2001 From: Loic Diridollou Date: Sun, 29 Sep 2024 18:34:13 -0400 Subject: [PATCH 3/4] GH648 Clean up testing --- tests/test_timefuncs.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index 5d0ebf01..d5431e43 100644 --- a/tests/test_timefuncs.py +++ b/tests/test_timefuncs.py @@ -16,10 +16,12 @@ TH, TU, WE, + weekday as WeekdayClass, ) import numpy as np from numpy import typing as npt import pandas as pd +import pytest import pytz from typing_extensions import ( assert_never, @@ -1293,19 +1295,13 @@ def test_weekofmonth_init(): ) -def test_dateoffset_weekday(): - """Check that you can create a `pd.DateOffset` from weekday of int or dateutil.relativedelta.""" - # check for int +@pytest.mark.parametrize("weekday", [1, MO, TU, WE, TH, TH, FR, SA, SU]) +def test_dateoffset_weekday(weekday: int | WeekdayClass): + """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), + assert_type(pd.offsets.DateOffset(weekday=weekday), pd.offsets.DateOffset), pd.offsets.DateOffset, ) - # check for relativedelta - for weekday in [MO, TU, WE, TH, TH, FR, SA, SU]: - check( - assert_type(pd.offsets.DateOffset(weekday=weekday), pd.offsets.DateOffset), - pd.offsets.DateOffset, - ) def test_date_range_unit(): From b7a7f54582fcc4ad9133c4c36a98d6acc8bd523e Mon Sep 17 00:00:00 2001 From: Loic Diridollou Date: Tue, 1 Oct 2024 21:28:18 -0400 Subject: [PATCH 4/4] PR Feedback --- tests/test_timefuncs.py | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index d5431e43..ed6cf910 100644 --- a/tests/test_timefuncs.py +++ b/tests/test_timefuncs.py @@ -16,12 +16,10 @@ TH, TU, WE, - weekday as WeekdayClass, ) import numpy as np from numpy import typing as npt import pandas as pd -import pytest import pytz from typing_extensions import ( assert_never, @@ -1295,11 +1293,38 @@ def test_weekofmonth_init(): ) -@pytest.mark.parametrize("weekday", [1, MO, TU, WE, TH, TH, FR, SA, SU]) -def test_dateoffset_weekday(weekday: int | WeekdayClass): +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=weekday), pd.offsets.DateOffset), + 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, )