Skip to content

Commit

Permalink
Series datetime is_year_end and is_year_start (#8954)
Browse files Browse the repository at this point in the history
This PR aims to allow users to be able to determine whether a datetime is the beginning or end of a year. This PR closes #8680

Authors:
  - Marlene  (https://github.com/marlenezw)
  - GALI PREM SAGAR (https://github.com/galipremsagar)

Approvers:
  - GALI PREM SAGAR (https://github.com/galipremsagar)
  - https://github.com/brandon-b-miller

URL: #8954
  • Loading branch information
marlenezw authored Aug 12, 2021
1 parent d22fdcf commit 59b84f3
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
70 changes: 70 additions & 0 deletions python/cudf/cudf/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -6082,6 +6082,76 @@ def is_month_end(self):
)
return (self.day == last_day.dt.day).fillna(False)

@property
def is_year_start(self):
"""
Boolean indicator if the date is the first day of the year.
Returns
-------
Series
Booleans indicating if dates are the first day of the year.
Example
-------
>>> import pandas as pd, cudf
>>> s = cudf.Series(pd.date_range("2017-12-30", periods=3))
>>> dates
0 2017-12-30
1 2017-12-31
2 2018-01-01
dtype: datetime64[ns]
>>> dates.dt.is_year_start
0 False
1 False
2 True
dtype: bool
"""
outcol = self.series._column.get_dt_field(
"day_of_year"
) == cudf.Scalar(1)
return Series._from_data(
{None: outcol.fillna(False)},
index=self.series._index,
name=self.series.name,
)

@property
def is_year_end(self):
"""
Boolean indicator if the date is the last day of the year.
Returns
-------
Series
Booleans indicating if dates are the last day of the year.
Example
-------
>>> import pandas as pd, cudf
>>> dates = cudf.Series(pd.date_range("2017-12-30", periods=3))
>>> dates
0 2017-12-30
1 2017-12-31
2 2018-01-01
dtype: datetime64[ns]
>>> dates.dt.is_year_end
0 False
1 True
2 False
dtype: bool
"""
day_of_year = self.series._column.get_dt_field("day_of_year")
leap_dates = libcudf.datetime.is_leap_year(self.series._column)

leap = day_of_year == cudf.Scalar(366)
non_leap = day_of_year == cudf.Scalar(365)
result = cudf._lib.copying.copy_if_else(leap, non_leap, leap_dates)
result = result.fillna(False)
return Series._from_data(
{None: result}, index=self.series._index, name=self.series.name,
)

def _get_dt_field(self, field):
out_column = self.series._column.get_dt_field(field)
return Series(
Expand Down
66 changes: 66 additions & 0 deletions python/cudf/cudf/tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1409,3 +1409,69 @@ def test_is_month_end(data, dtype):
got = gs.dt.is_month_end

assert_eq(expect, got)


@pytest.mark.parametrize(
"data",
[
[
"2020-05-31",
None,
"1999-12-01",
"2000-12-21",
None,
"1900-01-01",
"1800-03-14",
"2100-03-10",
"1970-01-01",
"1969-12-11",
"2017-12-30",
"2017-12-31",
"2018-01-01",
]
],
)
@pytest.mark.parametrize("dtype", ["datetime64[ns]"])
def test_is_year_start(data, dtype):
ps = pd.Series(data, dtype=dtype)
gs = cudf.from_pandas(ps)

expect = ps.dt.is_year_start
got = gs.dt.is_year_start

assert_eq(expect, got)


@pytest.mark.parametrize(
"data",
[
[
"2020-05-31",
None,
"1999-12-01",
"2000-12-21",
None,
"1900-12-31",
"1800-03-14",
"2017-12-30",
"2017-12-31",
"2020-12-31 08:00:00",
None,
"1999-12-31 18:40:00",
"2000-12-31 04:00:00",
None,
"1800-12-14 07:30:00",
"2100-12-14 07:30:00",
"2020-05-31",
]
],
)
@pytest.mark.parametrize("dtype", ["datetime64[ns]"])
def test_is_year_end(data, dtype):
ps = pd.Series(data, dtype=dtype)
gs = cudf.from_pandas(ps)

expect = ps.dt.is_year_end
got = gs.dt.is_year_end

assert_eq(expect, got)

0 comments on commit 59b84f3

Please sign in to comment.