@@ -9,41 +9,29 @@ from libcpp.utility cimport move
9
9
10
10
cimport pylibcudf.libcudf.datetime as libcudf_datetime
11
11
from pylibcudf.libcudf.column.column cimport column
12
- from pylibcudf.libcudf.column.column_view cimport column_view
13
12
from pylibcudf.libcudf.filling cimport calendrical_month_sequence
14
13
from pylibcudf.libcudf.scalar.scalar cimport scalar
15
14
from pylibcudf.libcudf.types cimport size_type
16
- from pylibcudf.datetime import DatetimeComponent
15
+ from pylibcudf.datetime import DatetimeComponent, RoundingFrequency
17
16
18
17
from cudf._lib.column cimport Column
19
18
from cudf._lib.scalar cimport DeviceScalar
19
+ import pylibcudf as plc
20
20
21
21
22
22
@ acquire_spill_lock ()
23
23
def add_months (Column col , Column months ):
24
24
# months must be int16 dtype
25
- cdef unique_ptr[column] c_result
26
- cdef column_view col_view = col.view()
27
- cdef column_view months_view = months.view()
28
-
29
- with nogil:
30
- c_result = move(
31
- libcudf_datetime.add_calendrical_months(
32
- col_view,
33
- months_view
34
- )
25
+ return Column.from_pylibcudf(
26
+ plc.datetime.add_calendrical_months(
27
+ col.to_pylibcudf(mode = " read" ),
28
+ months.to_pylibcudf(mode = " read" )
35
29
)
36
-
37
- return Column.from_unique_ptr(move(c_result))
30
+ )
38
31
39
32
40
33
@ acquire_spill_lock ()
41
34
def extract_datetime_component (Column col , object field ):
42
-
43
- cdef unique_ptr[column] c_result
44
- cdef column_view col_view = col.view()
45
- cdef libcudf_datetime.datetime_component component
46
-
47
35
component_names = {
48
36
" year" : DatetimeComponent.YEAR,
49
37
" month" : DatetimeComponent.MONTH,
@@ -57,27 +45,25 @@ def extract_datetime_component(Column col, object field):
57
45
" nanosecond" : DatetimeComponent.NANOSECOND,
58
46
}
59
47
if field == " day_of_year" :
60
- with nogil:
61
- c_result = move(libcudf_datetime.day_of_year(col_view))
48
+ result = Column.from_pylibcudf(
49
+ plc.datetime.day_of_year(
50
+ col.to_pylibcudf(mode = " read" )
51
+ )
52
+ )
62
53
elif field in component_names:
63
- component = component_names[field]
64
- with nogil:
65
- c_result = move(
66
- libcudf_datetime.extract_datetime_component(
67
- col_view,
68
- component
69
- )
54
+ result = Column.from_pylibcudf(
55
+ plc.datetime.extract_datetime_component(
56
+ col.to_pylibcudf(mode = " read" ),
57
+ component_names[field],
70
58
)
59
+ )
60
+ if field == " weekday" :
61
+ # Pandas counts Monday-Sunday as 0-6
62
+ # while libcudf counts Monday-Sunday as 1-7
63
+ result = result - result.dtype.type(1 )
71
64
else :
72
65
raise ValueError (f" Invalid field: '{field}'" )
73
66
74
- result = Column.from_unique_ptr(move(c_result))
75
-
76
- if field == " weekday" :
77
- # Pandas counts Monday-Sunday as 0-6
78
- # while libcudf counts Monday-Sunday as 1-7
79
- result = result - result.dtype.type(1 )
80
-
81
67
return result
82
68
83
69
@@ -101,78 +87,61 @@ cdef libcudf_datetime.rounding_frequency _get_rounding_frequency(object freq):
101
87
FutureWarning
102
88
)
103
89
freq = old_to_new_freq_map.get(freq)
104
- if freq == " D" :
105
- freq_val = libcudf_datetime.rounding_frequency.DAY
106
- elif freq == " h" :
107
- freq_val = libcudf_datetime.rounding_frequency.HOUR
108
- elif freq == " min" :
109
- freq_val = libcudf_datetime.rounding_frequency.MINUTE
110
- elif freq == " s" :
111
- freq_val = libcudf_datetime.rounding_frequency.SECOND
112
- elif freq == " ms" :
113
- freq_val = libcudf_datetime.rounding_frequency.MILLISECOND
114
- elif freq == " us" :
115
- freq_val = libcudf_datetime.rounding_frequency.MICROSECOND
116
- elif freq == " ns" :
117
- freq_val = libcudf_datetime.rounding_frequency.NANOSECOND
90
+ rounding_fequency_map = {
91
+ " D" : RoundingFrequency.DAY,
92
+ " h" : RoundingFrequency.HOUR,
93
+ " min" : RoundingFrequency.MINUTE,
94
+ " s" : RoundingFrequency.SECOND,
95
+ " ms" : RoundingFrequency.MILLISECOND,
96
+ " us" : RoundingFrequency.MICROSECOND,
97
+ " ns" : RoundingFrequency.NANOSECOND,
98
+ }
99
+ if freq in rounding_fequency_map:
100
+ freq_val = rounding_fequency_map[freq]
118
101
else :
119
102
raise ValueError (f" Invalid resolution: '{freq}'" )
120
103
return freq_val
121
104
122
105
123
106
@ acquire_spill_lock ()
124
107
def ceil_datetime (Column col , object freq ):
125
- cdef unique_ptr[column] c_result
126
- cdef column_view col_view = col.view()
127
- cdef libcudf_datetime.rounding_frequency freq_val = \
128
- _get_rounding_frequency(freq)
129
-
130
- with nogil:
131
- c_result = move(libcudf_datetime.ceil_datetimes(col_view, freq_val))
132
-
133
- result = Column.from_unique_ptr(move(c_result))
134
- return result
108
+ return Column.from_pylibcudf(
109
+ plc.datetime.ceil_datetimes(
110
+ col.to_pylibcudf(mode = " read" ),
111
+ _get_rounding_frequency(freq),
112
+ )
113
+ )
135
114
136
115
137
116
@ acquire_spill_lock ()
138
117
def floor_datetime (Column col , object freq ):
139
- cdef unique_ptr[column] c_result
140
- cdef column_view col_view = col.view()
141
- cdef libcudf_datetime.rounding_frequency freq_val = \
142
- _get_rounding_frequency(freq)
143
-
144
- with nogil:
145
- c_result = move(libcudf_datetime.floor_datetimes(col_view, freq_val))
146
-
147
- result = Column.from_unique_ptr(move(c_result))
148
- return result
118
+ return Column.from_pylibcudf(
119
+ plc.datetime.floor_datetimes(
120
+ col.to_pylibcudf(mode = " read" ),
121
+ _get_rounding_frequency(freq),
122
+ )
123
+ )
149
124
150
125
151
126
@ acquire_spill_lock ()
152
127
def round_datetime (Column col , object freq ):
153
- cdef unique_ptr[column] c_result
154
- cdef column_view col_view = col.view()
155
- cdef libcudf_datetime.rounding_frequency freq_val = \
156
- _get_rounding_frequency(freq)
157
-
158
- with nogil:
159
- c_result = move(libcudf_datetime.round_datetimes(col_view, freq_val))
160
-
161
- result = Column.from_unique_ptr(move(c_result))
162
- return result
128
+ return Column.from_pylibcudf(
129
+ plc.datetime.round_datetimes(
130
+ col.to_pylibcudf(mode = " read" ),
131
+ _get_rounding_frequency(freq),
132
+ )
133
+ )
163
134
164
135
165
136
@ acquire_spill_lock ()
166
137
def is_leap_year (Column col ):
167
138
""" Returns a boolean indicator whether the year of the date is a leap year
168
139
"""
169
- cdef unique_ptr[column] c_result
170
- cdef column_view col_view = col.view()
171
-
172
- with nogil:
173
- c_result = move(libcudf_datetime.is_leap_year(col_view))
174
-
175
- return Column.from_unique_ptr(move(c_result))
140
+ return Column.from_pylibcudf(
141
+ plc.datetime.is_leap_year(
142
+ col.to_pylibcudf(mode = " read" )
143
+ )
144
+ )
176
145
177
146
178
147
@ acquire_spill_lock ()
@@ -199,34 +168,28 @@ def extract_quarter(Column col):
199
168
Returns a column which contains the corresponding quarter of the year
200
169
for every timestamp inside the input column.
201
170
"""
202
- cdef unique_ptr[column] c_result
203
- cdef column_view col_view = col.view()
204
-
205
- with nogil:
206
- c_result = move(libcudf_datetime.extract_quarter(col_view))
207
-
208
- return Column.from_unique_ptr(move(c_result))
171
+ return Column.from_pylibcudf(
172
+ plc.datetime.extract_quarter(
173
+ col.to_pylibcudf(mode = " read" )
174
+ )
175
+ )
209
176
210
177
211
178
@ acquire_spill_lock ()
212
179
def days_in_month (Column col ):
213
180
""" Extracts the number of days in the month of the date
214
181
"""
215
- cdef unique_ptr[column] c_result
216
- cdef column_view col_view = col.view()
217
-
218
- with nogil:
219
- c_result = move(libcudf_datetime.days_in_month(col_view))
220
-
221
- return Column.from_unique_ptr(move(c_result))
182
+ return Column.from_pylibcudf(
183
+ plc.datetime.days_in_month(
184
+ col.to_pylibcudf(mode = " read" )
185
+ )
186
+ )
222
187
223
188
224
189
@ acquire_spill_lock ()
225
190
def last_day_of_month (Column col ):
226
- cdef unique_ptr[column] c_result
227
- cdef column_view col_view = col.view()
228
-
229
- with nogil:
230
- c_result = move(libcudf_datetime.last_day_of_month(col_view))
231
-
232
- return Column.from_unique_ptr(move(c_result))
191
+ return Column.from_pylibcudf(
192
+ plc.datetime.last_day_of_month(
193
+ col.to_pylibcudf(mode = " read" )
194
+ )
195
+ )
0 commit comments