From d9241a192193492692fefdf69c0b46f658771106 Mon Sep 17 00:00:00 2001 From: Francesco Bruzzesi <42817048+FBruzzesi@users.noreply.github.com> Date: Sun, 26 Nov 2023 19:54:48 +0100 Subject: [PATCH] v1.0.0 (#53) - v1.0.0 - Fixes #52 using Mike --- Makefile | 5 ++++- docs/user-guide/dataframe-modules.md | 18 +++++++++--------- iso_week_date/pandas_utils.py | 16 ++++++++-------- iso_week_date/polars_utils.py | 24 ++++++++++++------------ mkdocs.yaml | 7 ++++++- pyproject.toml | 3 ++- tests/pandas_utils_test.py | 20 +++++++++----------- tests/polars_utils_test.py | 24 +++++++++++------------- 8 files changed, 61 insertions(+), 56 deletions(-) diff --git a/Makefile b/Makefile index 0a2b75d..9a5fb11 100644 --- a/Makefile +++ b/Makefile @@ -43,9 +43,12 @@ docs-serve: mkdocs serve docs-deploy: - mkdocs gh-deploy + mike set-default --push --update-aliases $(shell grep -m 1 version pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3) latest pypi-push: rm -rf dist hatch build hatch publish + +get-version : + @echo $(shell grep -m 1 version pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3) diff --git a/docs/user-guide/dataframe-modules.md b/docs/user-guide/dataframe-modules.md index 4f6a800..15ea6aa 100644 --- a/docs/user-guide/dataframe-modules.md +++ b/docs/user-guide/dataframe-modules.md @@ -62,7 +62,7 @@ is_isoweekdate_series(s_iso + "abc") # False On the other hand the extensions[^1] approach extends the `pandas.Series` and `polars.Series`/`polars.Expr` classes with new methods. -The extensions are available through the `isoweek` namespace, and the methods available are the same as the functions. +The extensions are available through the `iwd` (isoweekdate) namespace, and the methods available are the same as the functions. "Translating" the previous examples to extensions: @@ -71,11 +71,11 @@ import pandas as pd from iso_week_date.pandas_utils import SeriesIsoWeek # noqa: F401 (1) s_date = pd.Series(pd.date_range(date(2023, 1, 1), date(2023, 1, 10), freq="1d")) -s_date.isoweek.datetime_to_isoweek(offset=pd.Timedelta(days=1)).to_list() +s_date.iwd.datetime_to_isoweek(offset=pd.Timedelta(days=1)).to_list() # ['2022-W52', '2022-W52', '2023-W01',..., '2023-W01', '2023-W02'] s_iso = pd.Series(["2022-W52", "2023-W01", "2023-W02"]) -s_iso.isoweek.isoweek_to_datetime(offset=pd.Timedelta(days=1)) +s_iso.iwd.isoweek_to_datetime(offset=pd.Timedelta(days=1)) ''' 0 2022-12-27 1 2023-01-03 @@ -83,8 +83,8 @@ s_iso.isoweek.isoweek_to_datetime(offset=pd.Timedelta(days=1)) dtype: datetime64[ns] ''' -s_iso.isoweek.is_isoweek(s_iso) # True -s_iso.isoweek.is_isoweek(s_iso + "abc") # False +s_iso.iwd.is_isoweek(s_iso) # True +s_iso.iwd.is_isoweek(s_iso + "abc") # False ``` 1. The import of `SeriesIsoWeek` is needed to register the extensions. @@ -96,11 +96,11 @@ import polars as pl from iso_week_date.polars_utils import SeriesIsoWeek # noqa: F401 (1) s_date = pl.date_range(date(2023, 1, 1), date(2023, 1, 10), interval="1d") -s_date.isoweek.datetime_to_isoweekdate(offset=timedelta(days=1)).to_list() +s_date.iwd.datetime_to_isoweekdate(offset=timedelta(days=1)).to_list() # ['2022-W52-6', '2022-W52-7', '2023-W01-1',..., '2023-W01-7', '2023-W02-1'] s_iso = pl.Series(["2022-W52-1", "2023-W01-2", "2023-W02-7"]) -s_iso.isoweek.isoweekdate_to_datetime(offset=timedelta(days=1)) +s_iso.iwd.isoweekdate_to_datetime(offset=timedelta(days=1)) ''' date 2022-12-27 @@ -108,8 +108,8 @@ date 2023-01-16 ''' -s_iso.isoweek.is_isoweekdate() # True -(s_iso + "abc").isoweek.is_isoweekdate_series() # False +s_iso.iwd.is_isoweekdate() # True +(s_iso + "abc").iwd.is_isoweekdate_series() # False ``` 1. The import of `SeriesIsoWeek` is needed to register the extensions. diff --git a/iso_week_date/pandas_utils.py b/iso_week_date/pandas_utils.py index 2a9cc72..35f5e58 100644 --- a/iso_week_date/pandas_utils.py +++ b/iso_week_date/pandas_utils.py @@ -304,7 +304,7 @@ def is_isoweekdate_series(series: pd.Series) -> bool: return _match_series(series, ISOWEEKDATE_PATTERN.pattern) -@pd.api.extensions.register_series_accessor("isoweek") +@pd.api.extensions.register_series_accessor("iwd") class SeriesIsoWeek: """Pandas Series extension that provides methods for working with ISO weeks and dates. @@ -320,7 +320,7 @@ class SeriesIsoWeek: from iso_week_date.pandas_utils import SeriesIsoWeek # noqa: F401 s = pd.Series(pd.date_range(date(2023, 1, 1), date(2023, 1, 10), freq="1d")) - s.isoweek.datetime_to_isoweek(offset=pd.Timedelta(days=1)).to_list() + s.iwd.datetime_to_isoweek(offset=pd.Timedelta(days=1)).to_list() # ['2022-W52', '2022-W52', '2023-W01',..., '2023-W01', '2023-W02'] ``` Parameters: @@ -353,7 +353,7 @@ def datetime_to_isoweek(self: Self, offset: Union[pd.Timedelta, int] = pd.Timede from iso_week_date.pandas_utils import SeriesIsoWeek # noqa: F401 s = pd.Series(pd.date_range(date(2023, 1, 1), date(2023, 1, 10), freq="1d")) - s.isoweek.datetime_to_isoweek(offset=pd.Timedelta(days=1)).to_list() + s.iwd.datetime_to_isoweek(offset=pd.Timedelta(days=1)).to_list() # ['2022-W52', '2022-W52', '2023-W01',..., '2023-W01', '2023-W02'] ``` """ @@ -379,7 +379,7 @@ def datetime_to_isoweekdate(self: Self, offset: Union[pd.Timedelta, int] = pd.Ti from iso_week_date.pandas_utils import SeriesIsoWeek # noqa: F401 s = pd.Series(pd.date_range(date(2023, 1, 1), date(2023, 1, 10), freq="1d")) - s.isoweek.datetime_to_isoweekdate(offset=pd.Timedelta(days=1)).to_list() + s.iwd.datetime_to_isoweekdate(offset=pd.Timedelta(days=1)).to_list() # ['2022-W52-6', '2022-W52-7', '2023-W01-1',..., '2023-W01-7', '2023-W02-1'] ``` """ @@ -413,7 +413,7 @@ def isoweek_to_datetime( from iso_week_date.pandas_utils import SeriesIsoWeek # noqa: F401 s = pd.Series(["2022-W52", "2023-W01", "2023-W02"]) - s.isoweek.isoweek_to_datetime(offset=pd.Timedelta(days=1)) + s.iwd.isoweek_to_datetime(offset=pd.Timedelta(days=1)) ''' 0 2022-12-27 1 2023-01-03 @@ -446,7 +446,7 @@ def isoweekdate_to_datetime(self: Self, offset: Union[pd.Timedelta, int] = pd.Ti from iso_week_date.pandas_utils import SeriesIsoWeek # noqa: F401 s = pd.Series(["2022-W52-1", "2023-W01-1", "2023-W02-1"]) - s.isoweek.isoweekdate_to_datetime(offset=pd.Timedelta(days=1)) + s.iwd.isoweekdate_to_datetime(offset=pd.Timedelta(days=1)) ''' 0 2022-12-27 1 2023-01-03 @@ -469,7 +469,7 @@ def is_isoweek(self: Self) -> bool: from iso_week_date.pandas_utils import SeriesIsoWeek # noqa: F401 s = pd.Series(["2022-W52", "2023-W01", "2023-W02"]) - s.isoweek.is_isoweek() # True + s.iwd.is_isoweek() # True ``` """ return is_isoweek_series(self._series) @@ -486,7 +486,7 @@ def is_isoweekdate(self: Self) -> bool: from iso_week_date.pandas_utils import SeriesIsoWeek # noqa: F401 s = pd.Series(["2022-W52-1", "2023-W01-1", "2023-W02-1"]) - s.isoweek.is_isoweekdate() # True + s.iwd.is_isoweekdate() # True ``` """ return is_isoweekdate_series(self._series) diff --git a/iso_week_date/polars_utils.py b/iso_week_date/polars_utils.py index cc94656..800a0b6 100644 --- a/iso_week_date/polars_utils.py +++ b/iso_week_date/polars_utils.py @@ -312,8 +312,8 @@ def is_isoweekdate_series(series: T) -> bool: return _match_series(series, ISOWEEKDATE_PATTERN.pattern) -@pl.api.register_series_namespace("isoweek") -@pl.api.register_expr_namespace("isoweek") +@pl.api.register_series_namespace("iwd") +@pl.api.register_expr_namespace("iwd") class SeriesIsoWeek(Generic[T]): """Polars Series and Expr extension that provides methods for working with ISO weeks and dates. @@ -329,10 +329,10 @@ class SeriesIsoWeek(Generic[T]): from iso_week_date.polars_utils import SeriesIsoWeek # noqa: F401 s = pl.date_range(date(2023, 1, 1), date(2023, 1, 10), interval="1d") - s.isoweek.datetime_to_isoweek(offset=timedelta(days=1)) + s.iwd.datetime_to_isoweek(offset=timedelta(days=1)) df = pl.DataFrame({"date": s}) - df.select(pl.col("date").isoweek.datetime_to_isoweek(offset=1)) + df.select(pl.col("date").iwd.datetime_to_isoweek(offset=1)) ``` Parameters: series: The pandas Series object the extension is attached to. @@ -366,10 +366,10 @@ def datetime_to_isoweek(self: Self, offset: Union[timedelta, int] = timedelta(0) from iso_week_date.polars_utils import SeriesIsoWeek # noqa: F401 s = pl.date_range(date(2023, 1, 1), date(2023, 1, 10), interval="1d") - s.isoweek.datetime_to_isoweek(offset=timedelta(days=1)) + s.iwd.datetime_to_isoweek(offset=timedelta(days=1)) df = pl.DataFrame({"date": s}) - df.select(pl.col("date").isoweek.datetime_to_isoweek(offset=1)) + df.select(pl.col("date").iwd.datetime_to_isoweek(offset=1)) ``` """ return datetime_to_isoweek(self._series, offset=offset) @@ -396,10 +396,10 @@ def datetime_to_isoweekdate(self: Self, offset: Union[timedelta, int] = timedelt from iso_week_date.polars_utils import SeriesIsoWeek # noqa: F401 s = pl.date_range(date(2023, 1, 1), date(2023, 1, 10), interval="1d") - s.isoweek.datetime_to_isoweekdate(offset=timedelta(days=1)) + s.iwd.datetime_to_isoweekdate(offset=timedelta(days=1)) df = pl.DataFrame({"date": s}) - df.select(pl.col("date").isoweek.datetime_to_isoweekdate(offset=1)) + df.select(pl.col("date").iwd.datetime_to_isoweekdate(offset=1)) ``` """ return datetime_to_isoweekdate(self._series, offset=offset) @@ -432,7 +432,7 @@ def isoweek_to_datetime(self: Self, offset: Union[timedelta, int] = timedelta(0) from iso_week_date.polars_utils import SeriesIsoWeek # noqa: F401 s = pl.Series(["2022-W52", "2023-W01", "2023-W02"]) - s.isoweek.isoweek_to_datetime(offset=timedelta(days=1)) + s.iwd.isoweek_to_datetime(offset=timedelta(days=1)) ''' date 2022-12-27 @@ -467,7 +467,7 @@ def isoweekdate_to_datetime(self: Self, offset: Union[timedelta, int] = timedelt from iso_week_date.polars_utils import SeriesIsoWeek # noqa: F401 s = pl.Series(["2022-W52-7", "2023-W01-1", "2023-W02-1"]) - s.isoweek.isoweekdate_to_datetime(offset=timedelta(days=1)) + s.iwd.isoweekdate_to_datetime(offset=timedelta(days=1)) ''' date 2022-01-02 @@ -490,7 +490,7 @@ def is_isoweek(self: Self) -> bool: from iso_week_date.polars_utils import SeriesIsoWeek # noqa: F401 s = pl.Series(["2022-W52", "2023-W01", "2023-W02"]) - s.isoweek.is_isoweek() # True + s.iwd.is_isoweek() # True ``` """ return is_isoweek_series(self._series) @@ -507,7 +507,7 @@ def is_isoweekdate(self: Self) -> bool: from iso_week_date.polars_utils import SeriesIsoWeek # noqa: F401 s = pl.Series(["2022-W52-1", "2023-W01-1", "2023-W02-1"]) - s.isoweek.is_isoweekdate() # True + s.iwd.is_isoweekdate() # True ``` """ return is_isoweekdate_series(self._series) diff --git a/mkdocs.yaml b/mkdocs.yaml index b44815c..a37ecfb 100644 --- a/mkdocs.yaml +++ b/mkdocs.yaml @@ -66,13 +66,18 @@ theme: # Plugins plugins: - - mkdocstrings - autorefs + - mike: + alias_type: symlink + canonical_version: latest + - mkdocstrings - search: separator: '[\s\-,:!=\[\]()"`/]+|\.(?!\d)|&[lg]t;|(?!\b)(?=[A-Z][a-z])' # Customization extra: + version: + provider: mike social: - icon: fontawesome/brands/github link: https://github.com/fbruzzesi diff --git a/pyproject.toml b/pyproject.toml index 0d677dc..8aee3c5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "iso-week-date" -version = "0.3.0" +version = "1.0.0" description = "Toolkit to work with str representing ISO Week date format" license = {file = "LICENSE"} @@ -56,6 +56,7 @@ docs = [ "mkdocs-material>=9.2.0", "mkdocstrings[python]>=0.20.0", "mkdocs-autorefs", + "mike==2.0.0" ] test = [ diff --git a/tests/pandas_utils_test.py b/tests/pandas_utils_test.py index 36c7e40..696b1eb 100644 --- a/tests/pandas_utils_test.py +++ b/tests/pandas_utils_test.py @@ -31,22 +31,20 @@ def test_datetime_to(periods: int, offset: int): # datetime_to_(format, isoweek) to_isoweek_g = _datetime_to_format(dt_series, offset=offset, _format=ISOWEEK__DATE_FORMAT) # from generic function to_isoweek_f = datetime_to_isoweek(dt_series, offset=offset) # from function - to_isoweek_m = dt_series.isoweek.datetime_to_isoweek(offset=offset) # from method extension + to_isoweek_m = dt_series.iwd.datetime_to_isoweek(offset=offset) # from method extension assert_series_equal(to_isoweek_g, to_isoweek_f) assert_series_equal(to_isoweek_g, to_isoweek_m) assert all([is_isoweek_series(to_isoweek_g), is_isoweek_series(to_isoweek_f), is_isoweek_series(to_isoweek_m)]) - assert all( - [to_isoweek_g.isoweek.is_isoweek(), to_isoweek_f.isoweek.is_isoweek(), to_isoweek_m.isoweek.is_isoweek()] - ) + assert all([to_isoweek_g.iwd.is_isoweek(), to_isoweek_f.iwd.is_isoweek(), to_isoweek_m.iwd.is_isoweek()]) # datetime_to_(format, isoweekdate) to_isoweekdate_g = _datetime_to_format( dt_series, offset=offset, _format=ISOWEEKDATE__DATE_FORMAT ) # from generic function to_isoweekdate_f = datetime_to_isoweekdate(dt_series, offset=offset) # from function - to_isoweekdate_m = dt_series.isoweek.datetime_to_isoweekdate(offset=offset) # from method extension + to_isoweekdate_m = dt_series.iwd.datetime_to_isoweekdate(offset=offset) # from method extension assert_series_equal(to_isoweekdate_g, to_isoweekdate_f) assert_series_equal(to_isoweekdate_g, to_isoweekdate_m) @@ -60,9 +58,9 @@ def test_datetime_to(periods: int, offset: int): ) assert all( [ - to_isoweekdate_g.isoweek.is_isoweekdate(), - to_isoweekdate_f.isoweek.is_isoweekdate(), - to_isoweekdate_m.isoweek.is_isoweekdate(), + to_isoweekdate_g.iwd.is_isoweekdate(), + to_isoweekdate_f.iwd.is_isoweekdate(), + to_isoweekdate_m.iwd.is_isoweekdate(), ] ) @@ -119,10 +117,10 @@ class CustomWeek(IsoWeek): iso_series = pd.Series(CustomWeek.from_date(_start - timedelta(weeks=1)).weeksout(periods)) dt_series_f = isoweek_to_datetime(iso_series, offset=offset, weekday=weekday) - dt_series_m = iso_series.isoweek.isoweek_to_datetime(offset=offset, weekday=weekday) + dt_series_m = iso_series.iwd.isoweek_to_datetime(offset=offset, weekday=weekday) assert all([is_datetime(dt_series_f), is_datetime(dt_series_m)]) - assert_series_equal(dt_series_f.isoweek.datetime_to_isoweek(offset=offset), iso_series) + assert_series_equal(dt_series_f.iwd.datetime_to_isoweek(offset=offset), iso_series) @pytest.mark.parametrize("periods", [5, 10, 52]) @@ -140,7 +138,7 @@ class CustomWeekDate(IsoWeekDate): iso_series = pd.Series(CustomWeekDate.from_date(_start - timedelta(days=1)).daysout(periods)) dt_series_f = isoweekdate_to_datetime(iso_series, offset=offset) - dt_series_m = iso_series.isoweek.isoweekdate_to_datetime(offset=offset) + dt_series_m = iso_series.iwd.isoweekdate_to_datetime(offset=offset) assert all([is_datetime(dt_series_f), is_datetime(dt_series_m)]) assert_series_equal(datetime_to_isoweekdate(dt_series_f, offset=offset), iso_series) diff --git a/tests/polars_utils_test.py b/tests/polars_utils_test.py index 838eb4e..8b9d4f9 100644 --- a/tests/polars_utils_test.py +++ b/tests/polars_utils_test.py @@ -30,21 +30,19 @@ def test_datetime_to_isoweek(periods, offset): # datetime_to_(format, isoweek) to_isoweek_g = _datetime_to_format(dt_series, offset=offset, _format=ISOWEEK__DATE_FORMAT) # from generic function to_isoweek_f = datetime_to_isoweek(dt_series, offset=offset) # from function - to_isoweek_m = dt_series.isoweek.datetime_to_isoweek(offset=offset) # from method extension + to_isoweek_m = dt_series.iwd.datetime_to_isoweek(offset=offset) # from method extension assert_series_equal(to_isoweek_g, to_isoweek_f) assert_series_equal(to_isoweek_g, to_isoweek_m) assert all([is_isoweek_series(to_isoweek_g), is_isoweek_series(to_isoweek_f), is_isoweek_series(to_isoweek_m)]) - assert all( - [to_isoweek_g.isoweek.is_isoweek(), to_isoweek_f.isoweek.is_isoweek(), to_isoweek_m.isoweek.is_isoweek()] - ) + assert all([to_isoweek_g.iwd.is_isoweek(), to_isoweek_f.iwd.is_isoweek(), to_isoweek_m.iwd.is_isoweek()]) # datetime_to_(format, isoweekdate) to_isoweekdate_g = _datetime_to_format( dt_series, offset=offset, _format=ISOWEEKDATE__DATE_FORMAT ) # from generic function to_isoweekdate_f = datetime_to_isoweekdate(dt_series, offset=offset) # from function - to_isoweekdate_m = dt_series.isoweek.datetime_to_isoweekdate(offset=offset) # from method extension + to_isoweekdate_m = dt_series.iwd.datetime_to_isoweekdate(offset=offset) # from method extension assert_series_equal(to_isoweekdate_g, to_isoweekdate_f) assert_series_equal(to_isoweekdate_g, to_isoweekdate_m) @@ -58,9 +56,9 @@ def test_datetime_to_isoweek(periods, offset): ) assert all( [ - to_isoweekdate_g.isoweek.is_isoweekdate(), - to_isoweekdate_f.isoweek.is_isoweekdate(), - to_isoweekdate_m.isoweek.is_isoweekdate(), + to_isoweekdate_g.iwd.is_isoweekdate(), + to_isoweekdate_f.iwd.is_isoweekdate(), + to_isoweekdate_m.iwd.is_isoweekdate(), ] ) @@ -115,10 +113,10 @@ class CustomWeek(IsoWeek): iso_series = pl.Series(CustomWeek.from_date(_start - timedelta(weeks=1)).weeksout(periods)) dt_series_f = isoweek_to_datetime(iso_series, offset=offset, weekday=weekday) - dt_series_m = iso_series.isoweek.isoweek_to_datetime(offset=offset, weekday=weekday) + dt_series_m = iso_series.iwd.isoweek_to_datetime(offset=offset, weekday=weekday) - assert_series_equal(dt_series_f.isoweek.datetime_to_isoweek(offset=offset), iso_series, check_names=False) - assert_series_equal(dt_series_m.isoweek.datetime_to_isoweek(offset=offset), iso_series, check_names=False) + assert_series_equal(dt_series_f.iwd.datetime_to_isoweek(offset=offset), iso_series, check_names=False) + assert_series_equal(dt_series_m.iwd.datetime_to_isoweek(offset=offset), iso_series, check_names=False) @pytest.mark.parametrize("periods", [5, 10, 52]) @@ -136,10 +134,10 @@ class CustomWeekDate(IsoWeekDate): iso_series = pl.Series(CustomWeekDate.from_date(_start - timedelta(days=1)).daysout(periods)) dt_series_f = isoweekdate_to_datetime(iso_series, offset=offset) - dt_series_m = iso_series.isoweek.isoweekdate_to_datetime(offset=offset) + dt_series_m = iso_series.iwd.isoweekdate_to_datetime(offset=offset) assert_series_equal(datetime_to_isoweekdate(dt_series_f, offset=offset), iso_series, check_names=False) - assert_series_equal(dt_series_m.isoweek.datetime_to_isoweekdate(offset=offset), iso_series, check_names=False) + assert_series_equal(dt_series_m.iwd.datetime_to_isoweekdate(offset=offset), iso_series, check_names=False) @pytest.mark.parametrize(