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
21from pyarrow.lib import (is_boolean_value,  # noqa
22                         is_integer_value,
23                         is_float_value)
24
25import 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_DATE_TYPES = {lib.Type_DATE32, lib.Type_DATE64}
35_TIME_TYPES = {lib.Type_TIME32, lib.Type_TIME64}
36_TEMPORAL_TYPES = {lib.Type_TIMESTAMP,
37                   lib.Type_DURATION} | _TIME_TYPES | _DATE_TYPES
38_NESTED_TYPES = {lib.Type_LIST, lib.Type_LARGE_LIST, lib.Type_STRUCT,
39                 lib.Type_UNION, lib.Type_MAP}
40
41
42def is_null(t):
43    """
44    Return True if value is an instance of a null type.
45    """
46    return t.id == lib.Type_NA
47
48
49def is_boolean(t):
50    """
51    Return True if value is an instance of a boolean type.
52    """
53    return t.id == lib.Type_BOOL
54
55
56def is_integer(t):
57    """
58    Return True if value is an instance of any integer type.
59    """
60    return t.id in _INTEGER_TYPES
61
62
63def is_signed_integer(t):
64    """
65    Return True if value is an instance of any signed integer type.
66    """
67    return t.id in _SIGNED_INTEGER_TYPES
68
69
70def is_unsigned_integer(t):
71    """
72    Return True if value is an instance of any unsigned integer type.
73    """
74    return t.id in _UNSIGNED_INTEGER_TYPES
75
76
77def is_int8(t):
78    """
79    Return True if value is an instance of an int8 type.
80    """
81    return t.id == lib.Type_INT8
82
83
84def is_int16(t):
85    """
86    Return True if value is an instance of an int16 type.
87    """
88    return t.id == lib.Type_INT16
89
90
91def is_int32(t):
92    """
93    Return True if value is an instance of an int32 type.
94    """
95    return t.id == lib.Type_INT32
96
97
98def is_int64(t):
99    """
100    Return True if value is an instance of an int64 type.
101    """
102    return t.id == lib.Type_INT64
103
104
105def is_uint8(t):
106    """
107    Return True if value is an instance of an uint8 type.
108    """
109    return t.id == lib.Type_UINT8
110
111
112def is_uint16(t):
113    """
114    Return True if value is an instance of an uint16 type.
115    """
116    return t.id == lib.Type_UINT16
117
118
119def is_uint32(t):
120    """
121    Return True if value is an instance of an uint32 type.
122    """
123    return t.id == lib.Type_UINT32
124
125
126def is_uint64(t):
127    """
128    Return True if value is an instance of an uint64 type.
129    """
130    return t.id == lib.Type_UINT64
131
132
133def is_floating(t):
134    """
135    Return True if value is an instance of a floating point numeric type.
136    """
137    return t.id in _FLOATING_TYPES
138
139
140def is_float16(t):
141    """
142    Return True if value is an instance of a float16 (half-precision) type.
143    """
144    return t.id == lib.Type_HALF_FLOAT
145
146
147def is_float32(t):
148    """
149    Return True if value is an instance of a float32 (single precision) type.
150    """
151    return t.id == lib.Type_FLOAT
152
153
154def is_float64(t):
155    """
156    Return True if value is an instance of a float64 (double precision) type.
157    """
158    return t.id == lib.Type_DOUBLE
159
160
161def is_list(t):
162    """
163    Return True if value is an instance of a list type.
164    """
165    return t.id == lib.Type_LIST
166
167
168def is_large_list(t):
169    """
170    Return True if value is an instance of a large list type.
171    """
172    return t.id == lib.Type_LARGE_LIST
173
174
175def is_fixed_size_list(t):
176    """
177    Return True if value is an instance of a fixed size list type.
178    """
179    return t.id == lib.Type_FIXED_SIZE_LIST
180
181
182def is_struct(t):
183    """
184    Return True if value is an instance of a struct type.
185    """
186    return t.id == lib.Type_STRUCT
187
188
189def is_union(t):
190    """
191    Return True if value is an instance of a union type.
192    """
193    return t.id == lib.Type_UNION
194
195
196def is_nested(t):
197    """
198    Return True if value is an instance of a nested type.
199    """
200    return t.id in _NESTED_TYPES
201
202
203def is_temporal(t):
204    """
205    Return True if value is an instance of date, time, timestamp or duration.
206    """
207    return t.id in _TEMPORAL_TYPES
208
209
210def is_timestamp(t):
211    """
212    Return True if value is an instance of a timestamp type.
213    """
214    return t.id == lib.Type_TIMESTAMP
215
216
217def is_duration(t):
218    """
219    Return True if value is an instance of a duration type.
220    """
221    return t.id == lib.Type_DURATION
222
223
224def is_time(t):
225    """
226    Return True if value is an instance of a time type.
227    """
228    return t.id in _TIME_TYPES
229
230
231def is_time32(t):
232    """
233    Return True if value is an instance of a time32 type.
234    """
235    return t.id == lib.Type_TIME32
236
237
238def is_time64(t):
239    """
240    Return True if value is an instance of a time64 type.
241    """
242    return t.id == lib.Type_TIME64
243
244
245def is_binary(t):
246    """
247    Return True if value is an instance of a variable-length binary type.
248    """
249    return t.id == lib.Type_BINARY
250
251
252def is_large_binary(t):
253    """
254    Return True if value is an instance of a large variable-length
255    binary type.
256    """
257    return t.id == lib.Type_LARGE_BINARY
258
259
260def is_unicode(t):
261    """
262    Alias for is_string.
263    """
264    return is_string(t)
265
266
267def is_string(t):
268    """
269    Return True if value is an instance of string (utf8 unicode) type.
270    """
271    return t.id == lib.Type_STRING
272
273
274def is_large_unicode(t):
275    """
276    Alias for is_large_string.
277    """
278    return is_large_string(t)
279
280
281def is_large_string(t):
282    """
283    Return True if value is an instance of large string (utf8 unicode) type.
284    """
285    return t.id == lib.Type_LARGE_STRING
286
287
288def is_fixed_size_binary(t):
289    """
290    Return True if value is an instance of a fixed size binary type.
291    """
292    return t.id == lib.Type_FIXED_SIZE_BINARY
293
294
295def is_date(t):
296    """
297    Return True if value is an instance of a date type.
298    """
299    return t.id in _DATE_TYPES
300
301
302def is_date32(t):
303    """
304    Return True if value is an instance of a date32 (days) type.
305    """
306    return t.id == lib.Type_DATE32
307
308
309def is_date64(t):
310    """
311    Return True if value is an instance of a date64 (milliseconds) type.
312    """
313    return t.id == lib.Type_DATE64
314
315
316def is_map(t):
317    """
318    Return True if value is an instance of a map logical type.
319    """
320    return t.id == lib.Type_MAP
321
322
323def is_decimal(t):
324    """
325    Return True if value is an instance of a decimal type.
326    """
327    return t.id == lib.Type_DECIMAL
328
329
330def is_dictionary(t):
331    """
332    Return True if value is an instance of a dictionary-encoded type.
333    """
334    return t.id == lib.Type_DICTIONARY
335
336
337def is_primitive(t):
338    """
339    Return True if the value is an instance of a primitive type.
340    """
341    return lib._is_primitive(t.id)
342