Skip to content

Commit 8b71597

Browse files
committed
sketched fast accessor for grouping
1 parent b21871e commit 8b71597

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

pandas/core/index.py

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from pandas.util.decorators import cache_readonly
1313
import pandas._tseries as lib
1414
import pandas._engines as _gin
15+
import pandas._datetime as _dt
1516

1617
from datetime import datetime
1718

@@ -1100,6 +1101,10 @@ def __getitem__(self, key):
11001101

11011102
return DatetimeIndex(result, name=self.name)
11021103

1104+
# TODO: make accessors for fast groupby work
1105+
def year(self):
1106+
return _dt.fast_field_accessor(self.values.view('i8'), 'Y')
1107+
11031108
def __iter__(self):
11041109
# TODO: again, figure out how to expose elements as nice datetime
11051110
# objects so you can do obj.year etc

pandas/src/datetime.pyx

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
cimport numpy as cnp
1+
cimport numpy as np
22
cimport cython
33
cimport cpython
44
import numpy as np
55

6-
from numpy cimport int64_t, import_array
6+
from numpy cimport int32_t, int64_t, import_array, ndarray
77

88
# this is our datetime.pxd
99
from datetime cimport *
@@ -80,3 +80,33 @@ def pydt_to_dt64(object pydt):
8080
return PyArray_DatetimeStructToDatetime(g_out_bestunit, &g_dts)
8181

8282
raise ValueError("Expected a datetime, received a %s" % type(pydt))
83+
84+
def fast_field_accessor(ndarray[int64_t] dtindex, object field):
85+
cdef:
86+
npy_datetimestruct dts
87+
Py_ssize_t i, count = 0
88+
ndarray[int32_t] out
89+
90+
count = len(dtindex)
91+
out = np.empty(count, dtype='i4')
92+
93+
if field == 'Y':
94+
for i in range(count):
95+
PyArray_DatetimeToDatetimeStruct(dtindex[i], NPY_FR_us, &dts)
96+
out[i] = dts.year
97+
return out
98+
99+
elif field == 'M':
100+
pass
101+
elif field == 'D':
102+
pass
103+
elif field == 'h':
104+
pass
105+
elif field == 'm':
106+
pass
107+
elif field == 's':
108+
pass
109+
elif field == 'us':
110+
pass
111+
else:
112+
raise ValueError("Field %s not supported, must be Y,M,D,h,m,s,us" % field)

0 commit comments

Comments
 (0)