]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
1 | # Licensed to the Apache Software Foundation (ASF) under one |
2 | # or more contributor license agreements. See the NOTICE file | |
3 | # distributed with this work for additional information | |
4 | # regarding copyright ownership. The ASF licenses this file | |
5 | # to you under the Apache License, Version 2.0 (the | |
6 | # "License"); you may not use this file except in compliance | |
7 | # with the License. You may obtain a copy of the License at | |
8 | # | |
9 | # http://www.apache.org/licenses/LICENSE-2.0 | |
10 | # | |
11 | # Unless required by applicable law or agreed to in writing, | |
12 | # software distributed under the License is distributed on an | |
13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
14 | # KIND, either express or implied. See the License for the | |
15 | # specific language governing permissions and limitations | |
16 | # under the License. | |
17 | ||
18 | # Tools for dealing with Arrow type metadata in Python | |
19 | ||
20 | ||
21 | from pyarrow.lib import (is_boolean_value, # noqa | |
22 | is_integer_value, | |
23 | is_float_value) | |
24 | ||
25 | import pyarrow.lib as lib | |
26 | ||
27 | ||
28 | _SIGNED_INTEGER_TYPES = {lib.Type_INT8, lib.Type_INT16, lib.Type_INT32, | |
29 | lib.Type_INT64} | |
30 | _UNSIGNED_INTEGER_TYPES = {lib.Type_UINT8, lib.Type_UINT16, lib.Type_UINT32, | |
31 | lib.Type_UINT64} | |
32 | _INTEGER_TYPES = _SIGNED_INTEGER_TYPES | _UNSIGNED_INTEGER_TYPES | |
33 | _FLOATING_TYPES = {lib.Type_HALF_FLOAT, lib.Type_FLOAT, lib.Type_DOUBLE} | |
34 | _DECIMAL_TYPES = {lib.Type_DECIMAL128, lib.Type_DECIMAL256} | |
35 | _DATE_TYPES = {lib.Type_DATE32, lib.Type_DATE64} | |
36 | _TIME_TYPES = {lib.Type_TIME32, lib.Type_TIME64} | |
37 | _INTERVAL_TYPES = {lib.Type_INTERVAL_MONTH_DAY_NANO} | |
38 | _TEMPORAL_TYPES = ({lib.Type_TIMESTAMP, | |
39 | lib.Type_DURATION} | _TIME_TYPES | _DATE_TYPES | | |
40 | _INTERVAL_TYPES) | |
41 | _UNION_TYPES = {lib.Type_SPARSE_UNION, lib.Type_DENSE_UNION} | |
42 | _NESTED_TYPES = {lib.Type_LIST, lib.Type_LARGE_LIST, lib.Type_STRUCT, | |
43 | lib.Type_MAP} | _UNION_TYPES | |
44 | ||
45 | ||
46 | def is_null(t): | |
47 | """ | |
48 | Return True if value is an instance of a null type. | |
49 | ||
50 | Parameters | |
51 | ---------- | |
52 | t : DataType | |
53 | """ | |
54 | return t.id == lib.Type_NA | |
55 | ||
56 | ||
57 | def is_boolean(t): | |
58 | """ | |
59 | Return True if value is an instance of a boolean type. | |
60 | ||
61 | Parameters | |
62 | ---------- | |
63 | t : DataType | |
64 | """ | |
65 | return t.id == lib.Type_BOOL | |
66 | ||
67 | ||
68 | def is_integer(t): | |
69 | """ | |
70 | Return True if value is an instance of any integer type. | |
71 | ||
72 | Parameters | |
73 | ---------- | |
74 | t : DataType | |
75 | """ | |
76 | return t.id in _INTEGER_TYPES | |
77 | ||
78 | ||
79 | def is_signed_integer(t): | |
80 | """ | |
81 | Return True if value is an instance of any signed integer type. | |
82 | ||
83 | Parameters | |
84 | ---------- | |
85 | t : DataType | |
86 | """ | |
87 | return t.id in _SIGNED_INTEGER_TYPES | |
88 | ||
89 | ||
90 | def is_unsigned_integer(t): | |
91 | """ | |
92 | Return True if value is an instance of any unsigned integer type. | |
93 | ||
94 | Parameters | |
95 | ---------- | |
96 | t : DataType | |
97 | """ | |
98 | return t.id in _UNSIGNED_INTEGER_TYPES | |
99 | ||
100 | ||
101 | def is_int8(t): | |
102 | """ | |
103 | Return True if value is an instance of an int8 type. | |
104 | ||
105 | Parameters | |
106 | ---------- | |
107 | t : DataType | |
108 | """ | |
109 | return t.id == lib.Type_INT8 | |
110 | ||
111 | ||
112 | def is_int16(t): | |
113 | """ | |
114 | Return True if value is an instance of an int16 type. | |
115 | ||
116 | Parameters | |
117 | ---------- | |
118 | t : DataType | |
119 | """ | |
120 | return t.id == lib.Type_INT16 | |
121 | ||
122 | ||
123 | def is_int32(t): | |
124 | """ | |
125 | Return True if value is an instance of an int32 type. | |
126 | ||
127 | Parameters | |
128 | ---------- | |
129 | t : DataType | |
130 | """ | |
131 | return t.id == lib.Type_INT32 | |
132 | ||
133 | ||
134 | def is_int64(t): | |
135 | """ | |
136 | Return True if value is an instance of an int64 type. | |
137 | ||
138 | Parameters | |
139 | ---------- | |
140 | t : DataType | |
141 | """ | |
142 | return t.id == lib.Type_INT64 | |
143 | ||
144 | ||
145 | def is_uint8(t): | |
146 | """ | |
147 | Return True if value is an instance of an uint8 type. | |
148 | ||
149 | Parameters | |
150 | ---------- | |
151 | t : DataType | |
152 | """ | |
153 | return t.id == lib.Type_UINT8 | |
154 | ||
155 | ||
156 | def is_uint16(t): | |
157 | """ | |
158 | Return True if value is an instance of an uint16 type. | |
159 | ||
160 | Parameters | |
161 | ---------- | |
162 | t : DataType | |
163 | """ | |
164 | return t.id == lib.Type_UINT16 | |
165 | ||
166 | ||
167 | def is_uint32(t): | |
168 | """ | |
169 | Return True if value is an instance of an uint32 type. | |
170 | ||
171 | Parameters | |
172 | ---------- | |
173 | t : DataType | |
174 | """ | |
175 | return t.id == lib.Type_UINT32 | |
176 | ||
177 | ||
178 | def is_uint64(t): | |
179 | """ | |
180 | Return True if value is an instance of an uint64 type. | |
181 | ||
182 | Parameters | |
183 | ---------- | |
184 | t : DataType | |
185 | """ | |
186 | return t.id == lib.Type_UINT64 | |
187 | ||
188 | ||
189 | def is_floating(t): | |
190 | """ | |
191 | Return True if value is an instance of a floating point numeric type. | |
192 | ||
193 | Parameters | |
194 | ---------- | |
195 | t : DataType | |
196 | """ | |
197 | return t.id in _FLOATING_TYPES | |
198 | ||
199 | ||
200 | def is_float16(t): | |
201 | """ | |
202 | Return True if value is an instance of a float16 (half-precision) type. | |
203 | ||
204 | Parameters | |
205 | ---------- | |
206 | t : DataType | |
207 | """ | |
208 | return t.id == lib.Type_HALF_FLOAT | |
209 | ||
210 | ||
211 | def is_float32(t): | |
212 | """ | |
213 | Return True if value is an instance of a float32 (single precision) type. | |
214 | ||
215 | Parameters | |
216 | ---------- | |
217 | t : DataType | |
218 | """ | |
219 | return t.id == lib.Type_FLOAT | |
220 | ||
221 | ||
222 | def is_float64(t): | |
223 | """ | |
224 | Return True if value is an instance of a float64 (double precision) type. | |
225 | ||
226 | Parameters | |
227 | ---------- | |
228 | t : DataType | |
229 | """ | |
230 | return t.id == lib.Type_DOUBLE | |
231 | ||
232 | ||
233 | def is_list(t): | |
234 | """ | |
235 | Return True if value is an instance of a list type. | |
236 | ||
237 | Parameters | |
238 | ---------- | |
239 | t : DataType | |
240 | """ | |
241 | return t.id == lib.Type_LIST | |
242 | ||
243 | ||
244 | def is_large_list(t): | |
245 | """ | |
246 | Return True if value is an instance of a large list type. | |
247 | ||
248 | Parameters | |
249 | ---------- | |
250 | t : DataType | |
251 | """ | |
252 | return t.id == lib.Type_LARGE_LIST | |
253 | ||
254 | ||
255 | def is_fixed_size_list(t): | |
256 | """ | |
257 | Return True if value is an instance of a fixed size list type. | |
258 | ||
259 | Parameters | |
260 | ---------- | |
261 | t : DataType | |
262 | """ | |
263 | return t.id == lib.Type_FIXED_SIZE_LIST | |
264 | ||
265 | ||
266 | def is_struct(t): | |
267 | """ | |
268 | Return True if value is an instance of a struct type. | |
269 | ||
270 | Parameters | |
271 | ---------- | |
272 | t : DataType | |
273 | """ | |
274 | return t.id == lib.Type_STRUCT | |
275 | ||
276 | ||
277 | def is_union(t): | |
278 | """ | |
279 | Return True if value is an instance of a union type. | |
280 | ||
281 | Parameters | |
282 | ---------- | |
283 | t : DataType | |
284 | """ | |
285 | return t.id in _UNION_TYPES | |
286 | ||
287 | ||
288 | def is_nested(t): | |
289 | """ | |
290 | Return True if value is an instance of a nested type. | |
291 | ||
292 | Parameters | |
293 | ---------- | |
294 | t : DataType | |
295 | """ | |
296 | return t.id in _NESTED_TYPES | |
297 | ||
298 | ||
299 | def is_temporal(t): | |
300 | """ | |
301 | Return True if value is an instance of date, time, timestamp or duration. | |
302 | ||
303 | Parameters | |
304 | ---------- | |
305 | t : DataType | |
306 | """ | |
307 | return t.id in _TEMPORAL_TYPES | |
308 | ||
309 | ||
310 | def is_timestamp(t): | |
311 | """ | |
312 | Return True if value is an instance of a timestamp type. | |
313 | ||
314 | Parameters | |
315 | ---------- | |
316 | t : DataType | |
317 | """ | |
318 | return t.id == lib.Type_TIMESTAMP | |
319 | ||
320 | ||
321 | def is_duration(t): | |
322 | """ | |
323 | Return True if value is an instance of a duration type. | |
324 | ||
325 | Parameters | |
326 | ---------- | |
327 | t : DataType | |
328 | """ | |
329 | return t.id == lib.Type_DURATION | |
330 | ||
331 | ||
332 | def is_time(t): | |
333 | """ | |
334 | Return True if value is an instance of a time type. | |
335 | ||
336 | Parameters | |
337 | ---------- | |
338 | t : DataType | |
339 | """ | |
340 | return t.id in _TIME_TYPES | |
341 | ||
342 | ||
343 | def is_time32(t): | |
344 | """ | |
345 | Return True if value is an instance of a time32 type. | |
346 | ||
347 | Parameters | |
348 | ---------- | |
349 | t : DataType | |
350 | """ | |
351 | return t.id == lib.Type_TIME32 | |
352 | ||
353 | ||
354 | def is_time64(t): | |
355 | """ | |
356 | Return True if value is an instance of a time64 type. | |
357 | ||
358 | Parameters | |
359 | ---------- | |
360 | t : DataType | |
361 | """ | |
362 | return t.id == lib.Type_TIME64 | |
363 | ||
364 | ||
365 | def is_binary(t): | |
366 | """ | |
367 | Return True if value is an instance of a variable-length binary type. | |
368 | ||
369 | Parameters | |
370 | ---------- | |
371 | t : DataType | |
372 | """ | |
373 | return t.id == lib.Type_BINARY | |
374 | ||
375 | ||
376 | def is_large_binary(t): | |
377 | """ | |
378 | Return True if value is an instance of a large variable-length | |
379 | binary type. | |
380 | ||
381 | Parameters | |
382 | ---------- | |
383 | t : DataType | |
384 | """ | |
385 | return t.id == lib.Type_LARGE_BINARY | |
386 | ||
387 | ||
388 | def is_unicode(t): | |
389 | """ | |
390 | Alias for is_string. | |
391 | ||
392 | Parameters | |
393 | ---------- | |
394 | t : DataType | |
395 | """ | |
396 | return is_string(t) | |
397 | ||
398 | ||
399 | def is_string(t): | |
400 | """ | |
401 | Return True if value is an instance of string (utf8 unicode) type. | |
402 | ||
403 | Parameters | |
404 | ---------- | |
405 | t : DataType | |
406 | """ | |
407 | return t.id == lib.Type_STRING | |
408 | ||
409 | ||
410 | def is_large_unicode(t): | |
411 | """ | |
412 | Alias for is_large_string. | |
413 | ||
414 | Parameters | |
415 | ---------- | |
416 | t : DataType | |
417 | """ | |
418 | return is_large_string(t) | |
419 | ||
420 | ||
421 | def is_large_string(t): | |
422 | """ | |
423 | Return True if value is an instance of large string (utf8 unicode) type. | |
424 | ||
425 | Parameters | |
426 | ---------- | |
427 | t : DataType | |
428 | """ | |
429 | return t.id == lib.Type_LARGE_STRING | |
430 | ||
431 | ||
432 | def is_fixed_size_binary(t): | |
433 | """ | |
434 | Return True if value is an instance of a fixed size binary type. | |
435 | ||
436 | Parameters | |
437 | ---------- | |
438 | t : DataType | |
439 | """ | |
440 | return t.id == lib.Type_FIXED_SIZE_BINARY | |
441 | ||
442 | ||
443 | def is_date(t): | |
444 | """ | |
445 | Return True if value is an instance of a date type. | |
446 | ||
447 | Parameters | |
448 | ---------- | |
449 | t : DataType | |
450 | """ | |
451 | return t.id in _DATE_TYPES | |
452 | ||
453 | ||
454 | def is_date32(t): | |
455 | """ | |
456 | Return True if value is an instance of a date32 (days) type. | |
457 | ||
458 | Parameters | |
459 | ---------- | |
460 | t : DataType | |
461 | """ | |
462 | return t.id == lib.Type_DATE32 | |
463 | ||
464 | ||
465 | def is_date64(t): | |
466 | """ | |
467 | Return True if value is an instance of a date64 (milliseconds) type. | |
468 | ||
469 | Parameters | |
470 | ---------- | |
471 | t : DataType | |
472 | """ | |
473 | return t.id == lib.Type_DATE64 | |
474 | ||
475 | ||
476 | def is_map(t): | |
477 | """ | |
478 | Return True if value is an instance of a map logical type. | |
479 | ||
480 | Parameters | |
481 | ---------- | |
482 | t : DataType | |
483 | """ | |
484 | return t.id == lib.Type_MAP | |
485 | ||
486 | ||
487 | def is_decimal(t): | |
488 | """ | |
489 | Return True if value is an instance of a decimal type. | |
490 | ||
491 | Parameters | |
492 | ---------- | |
493 | t : DataType | |
494 | """ | |
495 | return t.id in _DECIMAL_TYPES | |
496 | ||
497 | ||
498 | def is_decimal128(t): | |
499 | """ | |
500 | Return True if value is an instance of a decimal type. | |
501 | ||
502 | Parameters | |
503 | ---------- | |
504 | t : DataType | |
505 | """ | |
506 | return t.id == lib.Type_DECIMAL128 | |
507 | ||
508 | ||
509 | def is_decimal256(t): | |
510 | """ | |
511 | Return True if value is an instance of a decimal type. | |
512 | ||
513 | Parameters | |
514 | ---------- | |
515 | t : DataType | |
516 | """ | |
517 | return t.id == lib.Type_DECIMAL256 | |
518 | ||
519 | ||
520 | def is_dictionary(t): | |
521 | """ | |
522 | Return True if value is an instance of a dictionary-encoded type. | |
523 | ||
524 | Parameters | |
525 | ---------- | |
526 | t : DataType | |
527 | """ | |
528 | return t.id == lib.Type_DICTIONARY | |
529 | ||
530 | ||
531 | def is_interval(t): | |
532 | """ | |
533 | Return True if the value is an instance of an interval type. | |
534 | ||
535 | Parameters | |
536 | ---------- | |
537 | t : DateType | |
538 | """ | |
539 | return t.id == lib.Type_INTERVAL_MONTH_DAY_NANO | |
540 | ||
541 | ||
542 | def is_primitive(t): | |
543 | """ | |
544 | Return True if the value is an instance of a primitive type. | |
545 | ||
546 | Parameters | |
547 | ---------- | |
548 | t : DataType | |
549 | """ | |
550 | return lib._is_primitive(t.id) |