forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.pyx
More file actions
266 lines (196 loc) · 6.75 KB
/
Copy pathtable.pyx
File metadata and controls
266 lines (196 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# cython: profile=False
# distutils: language = c++
# cython: embedsignature = True
from pyarrow.includes.libarrow cimport *
cimport pyarrow.includes.pyarrow as pyarrow
import pyarrow.config
from pyarrow.array cimport Array, box_arrow_array
from pyarrow.compat import frombytes, tobytes
from pyarrow.error cimport check_status
from pyarrow.schema cimport box_data_type, box_schema
cdef class ChunkedArray:
'''
Do not call this class's constructor directly.
'''
def __cinit__(self):
self.chunked_array = NULL
cdef init(self, const shared_ptr[CChunkedArray]& chunked_array):
self.sp_chunked_array = chunked_array
self.chunked_array = chunked_array.get()
cdef _check_nullptr(self):
if self.chunked_array == NULL:
raise ReferenceError("ChunkedArray object references a NULL pointer."
"Not initialized.")
def length(self):
self._check_nullptr()
return self.chunked_array.length()
def __len__(self):
return self.length()
property null_count:
def __get__(self):
self._check_nullptr()
return self.chunked_array.null_count()
property num_chunks:
def __get__(self):
self._check_nullptr()
return self.chunked_array.num_chunks()
def chunk(self, i):
self._check_nullptr()
return box_arrow_array(self.chunked_array.chunk(i))
def iterchunks(self):
for i in range(self.num_chunks):
yield self.chunk(i)
cdef class Column:
'''
Do not call this class's constructor directly.
'''
def __cinit__(self):
self.column = NULL
cdef init(self, const shared_ptr[CColumn]& column):
self.sp_column = column
self.column = column.get()
def to_pandas(self):
"""
Convert the arrow::Column to a pandas Series
"""
cdef:
PyObject* arr
import pandas as pd
check_status(pyarrow.ArrowToPandas(self.sp_column, self, &arr))
return pd.Series(<object>arr, name=self.name)
cdef _check_nullptr(self):
if self.column == NULL:
raise ReferenceError("Column object references a NULL pointer."
"Not initialized.")
def __len__(self):
self._check_nullptr()
return self.column.length()
def length(self):
self._check_nullptr()
return self.column.length()
property shape:
def __get__(self):
self._check_nullptr()
return (self.length(),)
property null_count:
def __get__(self):
self._check_nullptr()
return self.column.null_count()
property name:
def __get__(self):
return frombytes(self.column.name())
property type:
def __get__(self):
return box_data_type(self.column.type())
property data:
def __get__(self):
cdef ChunkedArray chunked_array = ChunkedArray()
chunked_array.init(self.column.data())
return chunked_array
cdef class Table:
'''
Do not call this class's constructor directly.
'''
def __cinit__(self):
self.table = NULL
cdef init(self, const shared_ptr[CTable]& table):
self.sp_table = table
self.table = table.get()
cdef _check_nullptr(self):
if self.table == NULL:
raise ReferenceError("Table object references a NULL pointer."
"Not initialized.")
@staticmethod
def from_pandas(df, name=None):
pass
@staticmethod
def from_arrays(names, arrays, name=None):
cdef:
Array arr
Table result
c_string c_name
vector[shared_ptr[CField]] fields
vector[shared_ptr[CColumn]] columns
shared_ptr[CSchema] schema
shared_ptr[CTable] table
cdef int K = len(arrays)
fields.resize(K)
columns.resize(K)
for i in range(K):
arr = arrays[i]
c_name = tobytes(names[i])
fields[i].reset(new CField(c_name, arr.type.sp_type, True))
columns[i].reset(new CColumn(fields[i], arr.sp_array))
if name is None:
c_name = ''
else:
c_name = tobytes(name)
schema.reset(new CSchema(fields))
table.reset(new CTable(c_name, schema, columns))
result = Table()
result.init(table)
return result
def to_pandas(self):
"""
Convert the arrow::Table to a pandas DataFrame
"""
cdef:
PyObject* arr
shared_ptr[CColumn] col
Column column
import pandas as pd
names = []
data = []
for i in range(self.table.num_columns()):
col = self.table.column(i)
column = self.column(i)
check_status(pyarrow.ArrowToPandas(col, column, &arr))
names.append(frombytes(col.get().name()))
data.append(<object> arr)
return pd.DataFrame(dict(zip(names, data)), columns=names)
property name:
def __get__(self):
self._check_nullptr()
return frombytes(self.table.name())
property schema:
def __get__(self):
raise box_schema(self.table.schema())
def column(self, index):
self._check_nullptr()
cdef Column column = Column()
column.init(self.table.column(index))
return column
def __getitem__(self, i):
return self.column(i)
def itercolumns(self):
for i in range(self.num_columns):
yield self.column(i)
property num_columns:
def __get__(self):
self._check_nullptr()
return self.table.num_columns()
property num_rows:
def __get__(self):
self._check_nullptr()
return self.table.num_rows()
def __len__(self):
return self.num_rows
property shape:
def __get__(self):
return (self.num_rows, self.num_columns)