Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python binding for quarters #8862

Merged
merged 23 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/cudf/cudf/_lib/cpp/datetime.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ cdef extern from "cudf/datetime.hpp" namespace "cudf::datetime" nogil:
) except +
cdef unique_ptr[column] day_of_year(const column_view& column) except +
cdef unique_ptr[column] is_leap_year(const column_view& column) except +
cdef unique_ptr[column] extract_quarter(const column_view& column) except +
10 changes: 10 additions & 0 deletions python/cudf/cudf/_lib/datetime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,13 @@ def is_leap_year(Column col):
c_result = move(libcudf_datetime.is_leap_year(col_view))

return Column.from_unique_ptr(move(c_result))


def extract_quarter(Column col):
shaneding marked this conversation as resolved.
Show resolved Hide resolved
cdef unique_ptr[column] c_result
cdef column_view col_view = col.view()

with nogil:
c_result = move(libcudf_datetime.extract_quarter(col_view))

return Column.from_unique_ptr(move(c_result))
19 changes: 18 additions & 1 deletion python/cudf/cudf/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pandas._config import get_option

import cudf
from cudf._lib.datetime import is_leap_year
from cudf._lib.datetime import extract_quarter, is_leap_year
from cudf._lib.filling import sequence
from cudf._lib.search import search_sorted
from cudf._lib.table import Table
Expand Down Expand Up @@ -2357,6 +2357,23 @@ def is_leap_year(self):
res = is_leap_year(self._values).fillna(False)
return cupy.asarray(res)

@property
def quarter(self):
"""
Integer indicator for which quarter of the year the date belongs in.

There are 4 quarters in a year. With the first quarter being from
January - March, second quarter being April - June, third quarter
being July - September and fourth quarter being October - December.

Returns
-------
Series
shaneding marked this conversation as resolved.
Show resolved Hide resolved
Integer indicating which quarter the date belongs to.
"""
res = extract_quarter(self._values).fillna(False)
shaneding marked this conversation as resolved.
Show resolved Hide resolved
return Int64Index(res, dtype="int64")
shaneding marked this conversation as resolved.
Show resolved Hide resolved

def to_pandas(self):
nanos = self._values.astype("datetime64[ns]")
return pd.DatetimeIndex(nanos.to_pandas(), name=self.name)
Expand Down
23 changes: 23 additions & 0 deletions python/cudf/cudf/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -6446,6 +6446,29 @@ def is_leap_year(self):
name=self.series.name,
)

@property
def quarter(self):
"""
Integer indicator for which quarter of the year the date belongs in.

There are 4 quarters in a year. With the first quarter being from
January - March, second quarter being April - June, third quarter
being July - September and fourth quarter being October - December.

Returns
-------
Series
Integer indicating which quarter the date belongs to.
"""
res = libcudf.datetime.extract_quarter(self.series._column).fillna(
False
)
return Series._from_data(
ColumnAccessor({None: res}),
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
31 changes: 31 additions & 0 deletions python/cudf/cudf/tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1297,3 +1297,34 @@ def test_is_leap_year():
got2 = gIndex.is_leap_year

assert_eq(expect2, got2)


def test_quarter():
data = [
"2020-05-31 08:00:00",
"1999-12-31 18:40:00",
"2000-12-31 04:00:00",
"1900-02-28 07:00:00",
"1800-03-14 07:30:00",
"2100-03-14 07:30:00",
"1970-01-01 00:00:00",
"1969-12-31 12:59:00",
]

# Series
ps = pd.Series(data, dtype="datetime64[s]")
shaneding marked this conversation as resolved.
Show resolved Hide resolved
gs = cudf.from_pandas(ps)

expect = ps.dt.quarter
got = gs.dt.quarter.astype(np.int64)

assert_eq(expect, got)
shaneding marked this conversation as resolved.
Show resolved Hide resolved

# DatetimeIndex
pIndex = pd.DatetimeIndex(data)
gIndex = cudf.from_pandas(pIndex)

expect2 = pIndex.quarter
got2 = gIndex.quarter

assert_eq(expect2, got2)