From 0078bf694313f0b02c0c936cb343d91ddc3f486e Mon Sep 17 00:00:00 2001 From: Sarah Yurick <53962159+sarahyurick@users.noreply.github.com> Date: Wed, 28 Jul 2021 11:05:54 -0400 Subject: [PATCH] Series datetime is_month_start (#8844) Addressing feature request #8678. `is_month_start` returns a series indicating whether each date is the first day of the month. Authors: - Sarah Yurick (https://github.com/sarahyurick) Approvers: - Ram (Ramakrishna Prabhu) (https://github.com/rgsl888prabhu) - https://github.com/brandon-b-miller URL: https://github.com/rapidsai/cudf/pull/8844 --- python/cudf/cudf/core/series.py | 12 ++++++++++ python/cudf/cudf/tests/test_datetime.py | 29 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/python/cudf/cudf/core/series.py b/python/cudf/cudf/core/series.py index ac1861e2cfc..fb197fbc90d 100644 --- a/python/cudf/cudf/core/series.py +++ b/python/cudf/cudf/core/series.py @@ -6446,6 +6446,18 @@ def is_leap_year(self): name=self.series.name, ) + @property + def is_month_start(self): + """ + Boolean indicator if the date is the first day of the month. + + Returns + ------- + Series + Booleans indicating if dates are the first day of the month. + """ + return (self.day == 1).fillna(False) + def _get_dt_field(self, field): out_column = self.series._column.get_dt_field(field) return Series( diff --git a/python/cudf/cudf/tests/test_datetime.py b/python/cudf/cudf/tests/test_datetime.py index 022c9d93676..5f5a0a78414 100644 --- a/python/cudf/cudf/tests/test_datetime.py +++ b/python/cudf/cudf/tests/test_datetime.py @@ -1297,3 +1297,32 @@ def test_is_leap_year(): got2 = gIndex.is_leap_year assert_eq(expect2, got2) + + +@pytest.mark.parametrize( + "data", + [ + [ + "2020-05-31", + None, + "1999-12-01", + "2000-12-21", + None, + "1900-02-28", + "1800-03-14", + "2100-03-10", + "1970-01-01", + "1969-12-11", + ] + ], +) +@pytest.mark.parametrize("dtype", ["datetime64[ns]"]) +def test_is_month_start(data, dtype): + # Series + ps = pd.Series(data, dtype=dtype) + gs = cudf.from_pandas(ps) + + expect = ps.dt.is_month_start + got = gs.dt.is_month_start + + assert_eq(expect, got)