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

[REVIEW] Add duration types support in cudf(python/cython) #5781

Merged
merged 121 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from 107 commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
21f80bb
add duration types support in cudf(python/cython)
galipremsagar Jul 28, 2020
a144fd5
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Jul 28, 2020
d13f1aa
add type-cast methods
galipremsagar Jul 28, 2020
48b455a
change copyright
galipremsagar Jul 28, 2020
bd8c1d4
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Jul 28, 2020
db5cb2a
handle test failures
galipremsagar Jul 28, 2020
c0cbc9c
add binops support for timedeltas and datetime with timedeltas
galipremsagar Jul 29, 2020
f462cc1
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Jul 29, 2020
826f626
add support for truediv and floordiv in duration types
galipremsagar Jul 29, 2020
b324cb4
add series init tests
galipremsagar Jul 29, 2020
7af6a3b
add type-cast in timedelta column
galipremsagar Jul 29, 2020
f788e14
add to type-cast tests
galipremsagar Jul 29, 2020
e4702b6
add from typecast and from_pandas
galipremsagar Jul 29, 2020
3be15fc
add to_array tests
galipremsagar Jul 29, 2020
ce9a9a5
add to_pandas tests
galipremsagar Jul 29, 2020
065f61d
handle scalar binops properly
galipremsagar Jul 30, 2020
348a4c3
add ops tests between duration types
galipremsagar Jul 30, 2020
e1f3786
handle truediv issue
galipremsagar Jul 30, 2020
d1a68b4
handle output unit types when rhs is timedelta
galipremsagar Jul 30, 2020
5288103
add tests for datetime arithmetic with timedelta
galipremsagar Jul 30, 2020
207a92e
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Jul 30, 2020
4989d88
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Jul 30, 2020
e403d1f
handle nan values in Scalar
galipremsagar Jul 30, 2020
16eabd6
handle scalar and vectors in datetime ops
galipremsagar Jul 30, 2020
e5cd985
add tests for finding difference between two datetime types
galipremsagar Jul 30, 2020
bd7c33b
handle scalar timedelta scalar inputs
galipremsagar Jul 30, 2020
9b08fb5
add timedelta ops with scalars tests
galipremsagar Jul 30, 2020
526089c
add support for reduction ops and tests for the same in duration type
galipremsagar Jul 31, 2020
ef2e562
add support for reflect in datetime binops
galipremsagar Jul 31, 2020
61c691c
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Jul 31, 2020
e5a9d34
add TimedeltaIndex support
galipremsagar Jul 31, 2020
cd04bc2
Add TimeDeltaProperties
galipremsagar Jul 31, 2020
234ea87
handle more cases in binary ops
galipremsagar Jul 31, 2020
7541a6d
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Jul 31, 2020
626d669
add pymod for duration types
galipremsagar Jul 31, 2020
399854a
add support for manually overriding mask value with valid flag in Sca…
galipremsagar Jul 31, 2020
e82afe8
add Scalar to is_scalar check
galipremsagar Jul 31, 2020
a2630cf
add support for kwargs in default_na_value
galipremsagar Jul 31, 2020
212718e
add full support for components property in TimedeltaProperties
galipremsagar Jul 31, 2020
ddf84ae
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 1, 2020
1d942d7
add feature support for timedeltaproperties.dt.components and .dt.sec…
galipremsagar Aug 1, 2020
d7dc356
add tests for timedelta properties
galipremsagar Aug 1, 2020
d007b25
add valid property to Scalar object
galipremsagar Aug 3, 2020
edcd42c
add subtract ufunc and generalize ops cops for timedelta
galipremsagar Aug 3, 2020
c7377a7
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 3, 2020
9721f86
add datetime index tests
galipremsagar Aug 3, 2020
879775d
add operation between timedeltaIndex and DatetimeIndex
galipremsagar Aug 3, 2020
2fdc2d0
add TimedeltaIndex ops tests
galipremsagar Aug 3, 2020
84aa4e6
add ufun support for Index object and related tests
galipremsagar Aug 3, 2020
f60344e
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 3, 2020
5183ed9
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 3, 2020
b720831
add properties for TimedeltaIndex
galipremsagar Aug 3, 2020
bf0cb3f
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 4, 2020
4693626
add docs
galipremsagar Aug 4, 2020
41fc2b2
fix fillna and add repr related tests
galipremsagar Aug 4, 2020
037c75b
add inferred_freq
galipremsagar Aug 4, 2020
474ecc6
add TimedeltaIndex to docs
galipremsagar Aug 4, 2020
bf5cd6d
Update CHANGELOG.md
galipremsagar Aug 4, 2020
ae68c2e
Update CHANGELOG.md
galipremsagar Aug 4, 2020
a5186e3
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 6, 2020
f353b89
Add ability to handle Null scalar only when value is None
galipremsagar Aug 6, 2020
5587b87
Add from_arrow class methods for each Column type
galipremsagar Aug 6, 2020
7711af7
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 6, 2020
1c04cea
Update python/cudf/cudf/core/column/timedelta.py
galipremsagar Aug 6, 2020
6be87b8
Update python/cudf/cudf/core/column/timedelta.py
galipremsagar Aug 6, 2020
924b069
Update python/cudf/cudf/core/column/timedelta.py
galipremsagar Aug 6, 2020
b3a51d8
Update python/cudf/cudf/tests/test_timedelta.py
galipremsagar Aug 6, 2020
3db5aac
Mandate size parameter for TimeDeltaColumn
galipremsagar Aug 6, 2020
eac76b0
Merge branch 'durationtypes' of https://github.com/galipremsagar/cudf…
galipremsagar Aug 6, 2020
e767fd7
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 6, 2020
d70e1b6
fix add binop
galipremsagar Aug 7, 2020
ba7a846
add docs for unsupported param
galipremsagar Aug 7, 2020
5f607bf
move imports out of functions
galipremsagar Aug 7, 2020
191ec4d
remove unreachable code in default_na_value
galipremsagar Aug 7, 2020
ff59046
raise Informative error
galipremsagar Aug 7, 2020
2b47c4f
fix __contains__ and enable timedelta related test
galipremsagar Aug 7, 2020
9b5ed38
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 7, 2020
1fbb746
add unit map for datetime and timedelta types
galipremsagar Aug 7, 2020
afe1478
handle timestamp and timedelta scalars
galipremsagar Aug 7, 2020
4b7ca69
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 7, 2020
548b475
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 8, 2020
0ed23d2
add Timedelta series repr tests
galipremsagar Aug 8, 2020
11b06ad
add timedelta string conversion bindings
galipremsagar Aug 8, 2020
94b027a
add special handling for __repr__ incase of timedelta column
galipremsagar Aug 8, 2020
7d75fe1
add tostr typecast tests
galipremsagar Aug 9, 2020
94290f1
add repr handling for Index and dataframe
galipremsagar Aug 9, 2020
b64cf1f
raise error in pd.to_datetime and xfail pandas failures
galipremsagar Aug 9, 2020
d783f4b
fix str type-cast tests
galipremsagar Aug 9, 2020
edb9508
branch out each binop and add tigheter error handling
galipremsagar Aug 9, 2020
d331cda
xfail floordiv pandas issue
galipremsagar Aug 9, 2020
8dc4336
extract common data
galipremsagar Aug 9, 2020
fe3bd71
move common code into a single place for datetime and timedelta add a…
galipremsagar Aug 10, 2020
db383cd
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 10, 2020
8ef8c43
fix from str type-cast and add rountrip to and from str typecast test
galipremsagar Aug 10, 2020
8bcc7db
add invalid tests for timedelta binaryops
galipremsagar Aug 10, 2020
cf55108
add timedelta to datetime invalid cast test
galipremsagar Aug 10, 2020
4aeb2e2
typecast from numeric to timedelta types
galipremsagar Aug 10, 2020
b728d83
add ufunc ops tests
galipremsagar Aug 10, 2020
1dfa88c
add docs
galipremsagar Aug 10, 2020
b35f86c
separate non-overflow data for binaryops
galipremsagar Aug 10, 2020
2363c66
add timedelta contains tests
galipremsagar Aug 10, 2020
f93c89a
fix mod api
galipremsagar Aug 10, 2020
3dcaf41
add get_time_unit utility function
galipremsagar Aug 10, 2020
c8deaec
add datetime scalar ops tests and invalid ops tests
galipremsagar Aug 10, 2020
1e96271
add invalid param to to_datetime tests
galipremsagar Aug 10, 2020
59f3c2e
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 10, 2020
560fdbb
handle nullable_pd_dtype param according to types
galipremsagar Aug 10, 2020
c5f7cca
handle nullable_pd_dtype for timedelta series in a test
galipremsagar Aug 11, 2020
c25c0c9
handle pandas datetime and duration scalars in Scalar object creation
galipremsagar Aug 11, 2020
7b69063
add support for Duration Types in from_arrow and to_arrow
galipremsagar Aug 11, 2020
2899377
add docs for TimedeltaProperties
galipremsagar Aug 11, 2020
4ec9cf9
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 11, 2020
18aaf47
remove unused Truediv code introduced
galipremsagar Aug 11, 2020
dcdd02e
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 11, 2020
a7786d3
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 11, 2020
9e66b69
add rpymod for duration types scalar
galipremsagar Aug 11, 2020
b604a7e
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 11, 2020
c27efdd
refactor some operations to re-use existing apis
galipremsagar Aug 12, 2020
4992443
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 12, 2020
4675f76
parameterizing duration types tests
galipremsagar Aug 12, 2020
bfa8ec7
Merge remote-tracking branch 'upstream/branch-0.15' into durationtypes
galipremsagar Aug 12, 2020
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
- PR #5845 Add support for `mask_to_bools`
- PR #5851 Add support for `Index.sort_values`
- PR #5859 Add conversion form `fixed_point` to `bool`
- PR #5781 Add duration types support in cudf(python/cython)
- PR #5815 LIST Support for ColumnVector

## Improvements
Expand Down
23 changes: 22 additions & 1 deletion cpp/src/binaryop/jit/code/operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,23 @@ const char* operation =
};

struct TrueDiv {
template <typename TypeOut, typename TypeLhs, typename TypeRhs>
template <typename TypeOut, typename TypeLhs, typename TypeRhs,
enable_if_t<(!is_duration_v<TypeLhs>)>* = nullptr>
static TypeOut operate(TypeLhs x, TypeRhs y) {
return (static_cast<double>(x) / static_cast<double>(y));
}

template <typename TypeOut, typename TypeLhs, typename TypeRhs,
enable_if_t<(is_duration_v<TypeLhs>)>* = nullptr>
static TypeOut operate(TypeLhs x, TypeRhs y) {
return DurationDivide<TypeOut>(x, y);
}

template <typename TypeOut, typename TypeLhs, typename TypeRhs,
enable_if_t<(is_integral_v<TypeRhs> || is_duration_v<TypeRhs>)>* = nullptr>
static TypeOut DurationDivide(TypeLhs x, TypeRhs y) {
return x / y;
}
};

struct RTrueDiv {
Expand Down Expand Up @@ -221,6 +234,14 @@ const char* operation =
double y1 = static_cast<double>(y);
return fmod(fmod(x1, y1) + y1, y1);
}

template <typename TypeOut,
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
typename TypeLhs,
typename TypeRhs,
enable_if_t<(is_duration_v<TypeLhs> && is_duration_v<TypeOut>)>* = nullptr>
static TypeOut operate(TypeLhs x, TypeRhs y) {
return ((x % y) + y) % y;
}
};

struct RPyMod {
Expand Down
8 changes: 8 additions & 0 deletions docs/cudf/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ DatetimeIndex
:inherited-members:
:exclude-members: deserialize, serialize, device_deserialize, device_serialize, host_deserialize, host_serialize, tolist, to_list

TimedeltaIndex
--------------
.. currentmodule:: cudf.core.index
.. autoclass:: TimedeltaIndex
:members:
:inherited-members:
:exclude-members: deserialize, serialize, device_deserialize, device_serialize, host_deserialize, host_serialize, tolist, to_list

Categories
----------
.. currentmodule:: cudf.core.column.categorical
Expand Down
5 changes: 5 additions & 0 deletions python/cudf/cudf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
MultiIndex,
RangeIndex,
Series,
TimedeltaIndex,
UInt8Index,
UInt16Index,
UInt32Index,
Expand All @@ -34,6 +35,7 @@
from cudf.core.dtypes import CategoricalDtype
from cudf.core.groupby import Grouper
from cudf.core.ops import (
add,
arccos,
arcsin,
arctan,
Expand All @@ -44,10 +46,13 @@
logical_and,
logical_not,
logical_or,
multiply,
remainder,
sin,
sqrt,
subtract,
tan,
true_divide,
)
from cudf.core.reshape import concat, get_dummies, melt, merge_sorted
from cudf.core.series import isclose
Expand Down
10 changes: 10 additions & 0 deletions python/cudf/cudf/_lib/cpp/scalar/scalar.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ cdef extern from "cudf/scalar/scalar.hpp" namespace "cudf" nogil:
int32_t ticks_since_epoch_32 "ticks_since_epoch"() except +
T value() except +

cdef cppclass duration_scalar[T](scalar):
duration_scalar() except +
duration_scalar(duration_scalar other) except +
duration_scalar(int64_t value) except +
duration_scalar(int64_t value, bool is_valid) except +
duration_scalar(int32_t value) except +
duration_scalar(int32_t value, bool is_valid) except +
int64_t ticks "count"() except +
T value() except +

cdef cppclass string_scalar(scalar):
string_scalar() except +
string_scalar(string st) except +
Expand Down
19 changes: 19 additions & 0 deletions python/cudf/cudf/_lib/cpp/strings/convert/convert_durations.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2020, NVIDIA CORPORATION.

from cudf._lib.cpp.column.column cimport column
from cudf._lib.cpp.column.column_view cimport column_view
from cudf._lib.cpp.types cimport data_type

from libcpp.memory cimport unique_ptr
from libcpp.string cimport string

cdef extern from "cudf/strings/convert/convert_durations.hpp" namespace \
"cudf::strings" nogil:
cdef unique_ptr[column] to_durations(
const column_view & strings_col,
data_type duration_type,
const string & format) except +

cdef unique_ptr[column] from_durations(
const column_view & durations,
const string & format) except +
4 changes: 4 additions & 0 deletions python/cudf/cudf/_lib/cpp/types.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ cdef extern from "cudf/types.hpp" namespace "cudf" nogil:
STRING "cudf::type_id::STRING"
LIST "cudf::type_id::LIST"
NUM_TYPE_IDS "cudf::type_id::NUM_TYPE_IDS"
DURATION_SECONDS "cudf::type_id::DURATION_SECONDS"
DURATION_MILLISECONDS "cudf::type_id::DURATION_MILLISECONDS"
DURATION_MICROSECONDS "cudf::type_id::DURATION_MICROSECONDS"
DURATION_NANOSECONDS "cudf::type_id::DURATION_NANOSECONDS"

cdef cppclass data_type:
data_type() except +
Expand Down
10 changes: 10 additions & 0 deletions python/cudf/cudf/_lib/cpp/wrappers/durations.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2020, NVIDIA CORPORATION.

from libc.stdint cimport int64_t


cdef extern from "cudf/wrappers/durations.hpp" namespace "cudf" nogil:
ctypedef int64_t duration_s
ctypedef int64_t duration_ms
ctypedef int64_t duration_us
ctypedef int64_t duration_ns
3 changes: 3 additions & 0 deletions python/cudf/cudf/_lib/scalar.pxd
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) 2020, NVIDIA CORPORATION.

from libcpp.memory cimport unique_ptr
from libcpp cimport bool

from cudf._lib.cpp.scalar.scalar cimport scalar

Expand All @@ -10,3 +11,5 @@ cdef class Scalar:

@staticmethod
cdef Scalar from_unique_ptr(unique_ptr[scalar] ptr)

cpdef bool is_valid(Scalar s)
122 changes: 105 additions & 17 deletions python/cudf/cudf/_lib/scalar.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ from libc.stdint cimport (
from libcpp.memory cimport unique_ptr
from libcpp cimport bool

from cudf._lib.types import np_to_cudf_types, cudf_to_np_types
import cudf
from cudf._lib.types import cudf_to_np_types, duration_unit_map
from cudf._lib.types import datetime_unit_map
from cudf._lib.types cimport underlying_type_t_type_id
from cudf._lib.move cimport move

Expand All @@ -26,10 +28,17 @@ from cudf._lib.cpp.wrappers.timestamps cimport (
timestamp_us,
timestamp_ns
)
from cudf._lib.cpp.wrappers.durations cimport(
duration_s,
duration_ms,
duration_us,
duration_ns
)
from cudf._lib.cpp.scalar.scalar cimport (
scalar,
numeric_scalar,
timestamp_scalar,
duration_scalar,
string_scalar
)
cimport cudf._lib.cpp.types as libcudf_types
Expand All @@ -51,9 +60,9 @@ cdef class Scalar:
dtype : dtype
A NumPy dtype.
"""
from cudf.utils.dtypes import to_cudf_compatible_scalar

value = to_cudf_compatible_scalar(value, dtype=dtype)
value = cudf.utils.dtypes.to_cudf_compatible_scalar(value, dtype=dtype)

valid = value is not None

if dtype is None:
Expand All @@ -74,11 +83,14 @@ cdef class Scalar:
_set_datetime64_from_np_scalar(
self.c_value, value, dtype, valid
)
elif pd.api.types.is_timedelta64_dtype(dtype):
_set_timedelta64_from_np_scalar(
self.c_value, value, dtype, valid
)
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
else:
raise ValueError(
"Cannot convert value of type {} to cudf scalar".format(
type(value).__name__
)
f"Cannot convert value of type "
f"{type(value).__name__} to cudf scalar"
)

@property
Expand All @@ -101,11 +113,19 @@ cdef class Scalar:
return _get_np_scalar_from_numeric(self.c_value)
elif pd.api.types.is_datetime64_dtype(self.dtype):
return _get_np_scalar_from_timestamp64(self.c_value)
elif pd.api.types.is_timedelta64_dtype(self.dtype):
return _get_np_scalar_from_timedelta64(self.c_value)
else:
raise ValueError(
"Could not convert cudf::scalar to a Python value"
)

cpdef bool is_valid(self):
"""
Returns if the Scalar is valid or not(i.e., <NA>).
"""
return self.c_value.get()[0].is_valid()

def __repr__(self):
if self.value is None:
return f"Scalar({self.value}, {self.dtype.__repr__()})"
Expand All @@ -128,8 +148,8 @@ cdef _set_string_from_np_string(unique_ptr[scalar]& s, value, bool valid=True):


cdef _set_numeric_from_np_scalar(unique_ptr[scalar]& s,
value,
dtype,
object value,
object dtype,
bool valid=True):
value = value if valid else 0
if dtype == "int8":
Expand All @@ -155,14 +175,16 @@ cdef _set_numeric_from_np_scalar(unique_ptr[scalar]& s,
elif dtype == "bool":
s.reset(new numeric_scalar[bool](<bool>value, valid))
else:
raise ValueError("dtype not supported: {}".format(dtype))
raise ValueError(f"dtype not supported: {dtype}")


cdef _set_datetime64_from_np_scalar(unique_ptr[scalar]& s,
value,
dtype,
object value,
object dtype,
bool valid=True):

value = value if valid else 0

if dtype == "datetime64[s]":
s.reset(
new timestamp_scalar[timestamp_s](<int64_t>np.int64(value), valid)
Expand All @@ -180,8 +202,33 @@ cdef _set_datetime64_from_np_scalar(unique_ptr[scalar]& s,
new timestamp_scalar[timestamp_ns](<int64_t>np.int64(value), valid)
)
else:
raise ValueError("dtype not supported: {}".format(dtype))
raise ValueError(f"dtype not supported: {dtype}")

cdef _set_timedelta64_from_np_scalar(unique_ptr[scalar]& s,
object value,
object dtype,
bool valid=True):

value = value if valid else 0

if dtype == "timedelta64[s]":
s.reset(
new duration_scalar[duration_s](<int64_t>np.int64(value), valid)
)
elif dtype == "timedelta64[ms]":
s.reset(
new duration_scalar[duration_ms](<int64_t>np.int64(value), valid)
)
elif dtype == "timedelta64[us]":
s.reset(
new duration_scalar[duration_us](<int64_t>np.int64(value), valid)
)
elif dtype == "timedelta64[ns]":
s.reset(
new duration_scalar[duration_ns](<int64_t>np.int64(value), valid)
)
kkraus14 marked this conversation as resolved.
Show resolved Hide resolved
else:
raise ValueError(f"dtype not supported: {dtype}")

cdef _get_py_string_from_string(unique_ptr[scalar]& s):
if not s.get()[0].is_valid():
Expand Down Expand Up @@ -236,28 +283,69 @@ cdef _get_np_scalar_from_timestamp64(unique_ptr[scalar]& s):
(
<timestamp_scalar[timestamp_ms]*> s_ptr
)[0].ticks_since_epoch_64(),
"s"
datetime_unit_map[<underlying_type_t_type_id>(cdtype.id())]
)
elif cdtype.id() == libcudf_types.TIMESTAMP_MILLISECONDS:
return np.datetime64(
(
<timestamp_scalar[timestamp_ms]*> s_ptr
)[0].ticks_since_epoch_64(),
"ms"
datetime_unit_map[<underlying_type_t_type_id>(cdtype.id())]
)
elif cdtype.id() == libcudf_types.TIMESTAMP_MICROSECONDS:
return np.datetime64(
(
<timestamp_scalar[timestamp_ms]*> s_ptr
)[0].ticks_since_epoch_64(),
"us"
datetime_unit_map[<underlying_type_t_type_id>(cdtype.id())]
)
elif cdtype.id() == libcudf_types.TIMESTAMP_NANOSECONDS:
return np.datetime64(
(
<timestamp_scalar[timestamp_ms]*> s_ptr
)[0].ticks_since_epoch_64(),
"ns"
datetime_unit_map[<underlying_type_t_type_id>(cdtype.id())]
)
else:
raise ValueError("Could not convert cudf::scalar to numpy scalar")


cdef _get_np_scalar_from_timedelta64(unique_ptr[scalar]& s):

cdef scalar* s_ptr = s.get()

if not s_ptr[0].is_valid():
return None

cdef libcudf_types.data_type cdtype = s_ptr[0].type()

if cdtype.id() == libcudf_types.DURATION_SECONDS:
return np.timedelta64(
(
<duration_scalar[duration_s]*> s_ptr
)[0].ticks(),
duration_unit_map[<underlying_type_t_type_id>(cdtype.id())]
)
elif cdtype.id() == libcudf_types.DURATION_MILLISECONDS:
return np.timedelta64(
(
<duration_scalar[duration_ms]*> s_ptr
)[0].ticks(),
duration_unit_map[<underlying_type_t_type_id>(cdtype.id())]
)
elif cdtype.id() == libcudf_types.DURATION_MICROSECONDS:
return np.timedelta64(
(
<duration_scalar[duration_us]*> s_ptr
)[0].ticks(),
duration_unit_map[<underlying_type_t_type_id>(cdtype.id())]
)
elif cdtype.id() == libcudf_types.DURATION_NANOSECONDS:
return np.timedelta64(
(
<duration_scalar[duration_ns]*> s_ptr
)[0].ticks(),
duration_unit_map[<underlying_type_t_type_id>(cdtype.id())]
)
else:
raise ValueError("Could not convert cudf::scalar to numpy scalar")
Expand All @@ -270,4 +358,4 @@ def as_scalar(val, dtype=None):
else:
return Scalar(val.value, dtype)
else:
return Scalar(val, dtype)
return Scalar(value=val, dtype=dtype)
Loading