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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
# 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.
# Tools for dealing with Arrow type metadata in Python
from enum import IntEnum
from pyarrow.lib import (is_boolean_value, # noqa
is_integer_value,
is_float_value)
import pyarrow.lib as lib
from pyarrow.util import doc
_SIGNED_INTEGER_TYPES = {lib.Type_INT8, lib.Type_INT16, lib.Type_INT32,
lib.Type_INT64}
_UNSIGNED_INTEGER_TYPES = {lib.Type_UINT8, lib.Type_UINT16, lib.Type_UINT32,
lib.Type_UINT64}
_INTEGER_TYPES = _SIGNED_INTEGER_TYPES | _UNSIGNED_INTEGER_TYPES
_FLOATING_TYPES = {lib.Type_HALF_FLOAT, lib.Type_FLOAT, lib.Type_DOUBLE}
_DECIMAL_TYPES = {lib.Type_DECIMAL32, lib.Type_DECIMAL64, lib.Type_DECIMAL128,
lib.Type_DECIMAL256}
_DATE_TYPES = {lib.Type_DATE32, lib.Type_DATE64}
_TIME_TYPES = {lib.Type_TIME32, lib.Type_TIME64}
_INTERVAL_TYPES = {lib.Type_INTERVAL_MONTH_DAY_NANO}
_TEMPORAL_TYPES = ({lib.Type_TIMESTAMP,
lib.Type_DURATION} | _TIME_TYPES | _DATE_TYPES |
_INTERVAL_TYPES)
_UNION_TYPES = {lib.Type_SPARSE_UNION, lib.Type_DENSE_UNION}
_NESTED_TYPES = {lib.Type_LIST, lib.Type_FIXED_SIZE_LIST, lib.Type_LARGE_LIST,
lib.Type_LIST_VIEW, lib.Type_LARGE_LIST_VIEW,
lib.Type_STRUCT, lib.Type_MAP} | _UNION_TYPES
class TypesEnum(IntEnum):
"""
An Enum that maps constant values to data types.
Exposes the underlying data types representation for type checking purposes.
Note that some of the types listed here are not supported by PyArrow yet:
INTERVAL_MONTHS and INTERVAL_DAY_TIME.
Examples
--------
>>> import pyarrow as pa
>>> from pyarrow.types import TypesEnum
>>> int8_field = pa.field('int8_field', pa.int8())
>>> int8_field.type.id == TypesEnum.INT8
True
>>> fixed_size_list = pa.list_(pa.uint16(), 3)
>>> fixed_size_list.id == TypesEnum.LIST
False
>>> fixed_size_list.id == TypesEnum.FIXED_SIZE_LIST
True
"""
NA = lib.Type_NA
BOOL = lib.Type_BOOL
INT8 = lib.Type_INT8
INT16 = lib.Type_INT16
INT32 = lib.Type_INT32
INT64 = lib.Type_INT64
UINT8 = lib.Type_UINT8
UINT16 = lib.Type_UINT16
UINT32 = lib.Type_UINT32
UINT64 = lib.Type_UINT64
HALF_FLOAT = lib.Type_HALF_FLOAT
FLOAT = lib.Type_FLOAT
DOUBLE = lib.Type_DOUBLE
BINARY = lib.Type_BINARY
BINARY_VIEW = lib.Type_BINARY_VIEW
LARGE_BINARY = lib.Type_LARGE_BINARY
STRING = lib.Type_STRING
STRING_VIEW = lib.Type_STRING_VIEW
LARGE_STRING = lib.Type_LARGE_STRING
FIXED_SIZE_BINARY = lib.Type_FIXED_SIZE_BINARY
DECIMAL32 = lib.Type_DECIMAL32
DECIMAL64 = lib.Type_DECIMAL64
DECIMAL128 = lib.Type_DECIMAL128
DECIMAL256 = lib.Type_DECIMAL256
LIST = lib.Type_LIST
LARGE_LIST = lib.Type_LARGE_LIST
LIST_VIEW = lib.Type_LIST_VIEW
LARGE_LIST_VIEW = lib.Type_LARGE_LIST_VIEW
MAP = lib.Type_MAP
FIXED_SIZE_LIST = lib.Type_FIXED_SIZE_LIST
STRUCT = lib.Type_STRUCT
SPARSE_UNION = lib.Type_SPARSE_UNION
DENSE_UNION = lib.Type_DENSE_UNION
RUN_END_ENCODED = lib.Type_RUN_END_ENCODED
DATE32 = lib.Type_DATE32
DATE64 = lib.Type_DATE64
TIME32 = lib.Type_TIME32
TIME64 = lib.Type_TIME64
TIMESTAMP = lib.Type_TIMESTAMP
INTERVAL_MONTHS = lib.Type_INTERVAL_MONTHS
INTERVAL_DAY_TIME = lib.Type_INTERVAL_DAY_TIME
INTERVAL_MONTH_DAY_NANO = lib.Type_INTERVAL_MONTH_DAY_NANO
DURATION = lib.Type_DURATION
DICTIONARY = lib.Type_DICTIONARY
@doc(datatype="null")
def is_null(t):
"""
Return True if value is an instance of type: {datatype}.
Parameters
----------
t : DataType
"""
return t.id == lib.Type_NA
@doc(is_null, datatype="boolean")
def is_boolean(t):
return t.id == lib.Type_BOOL
@doc(is_null, datatype="any integer")
def is_integer(t):
return t.id in _INTEGER_TYPES
@doc(is_null, datatype="signed integer")
def is_signed_integer(t):
return t.id in _SIGNED_INTEGER_TYPES
@doc(is_null, datatype="unsigned integer")
def is_unsigned_integer(t):
return t.id in _UNSIGNED_INTEGER_TYPES
@doc(is_null, datatype="int8")
def is_int8(t):
return t.id == lib.Type_INT8
@doc(is_null, datatype="int16")
def is_int16(t):
return t.id == lib.Type_INT16
@doc(is_null, datatype="int32")
def is_int32(t):
return t.id == lib.Type_INT32
@doc(is_null, datatype="int64")
def is_int64(t):
return t.id == lib.Type_INT64
@doc(is_null, datatype="uint8")
def is_uint8(t):
return t.id == lib.Type_UINT8
@doc(is_null, datatype="uint16")
def is_uint16(t):
return t.id == lib.Type_UINT16
@doc(is_null, datatype="uint32")
def is_uint32(t):
return t.id == lib.Type_UINT32
@doc(is_null, datatype="uint64")
def is_uint64(t):
return t.id == lib.Type_UINT64
@doc(is_null, datatype="floating point numeric")
def is_floating(t):
return t.id in _FLOATING_TYPES
@doc(is_null, datatype="float16 (half-precision)")
def is_float16(t):
return t.id == lib.Type_HALF_FLOAT
@doc(is_null, datatype="float32 (single precision)")
def is_float32(t):
return t.id == lib.Type_FLOAT
@doc(is_null, datatype="float64 (double precision)")
def is_float64(t):
return t.id == lib.Type_DOUBLE
@doc(is_null, datatype="list")
def is_list(t):
return t.id == lib.Type_LIST
@doc(is_null, datatype="large list")
def is_large_list(t):
return t.id == lib.Type_LARGE_LIST
@doc(is_null, datatype="fixed size list")
def is_fixed_size_list(t):
return t.id == lib.Type_FIXED_SIZE_LIST
@doc(is_null, datatype="list view")
def is_list_view(t):
return t.id == lib.Type_LIST_VIEW
@doc(is_null, datatype="large list view")
def is_large_list_view(t):
return t.id == lib.Type_LARGE_LIST_VIEW
@doc(is_null, datatype="struct")
def is_struct(t):
return t.id == lib.Type_STRUCT
@doc(is_null, datatype="union")
def is_union(t):
return t.id in _UNION_TYPES
@doc(is_null, datatype="nested type")
def is_nested(t):
return t.id in _NESTED_TYPES
@doc(is_null, datatype="run-end encoded")
def is_run_end_encoded(t):
return t.id == lib.Type_RUN_END_ENCODED
@doc(is_null, datatype="date, time, timestamp or duration")
def is_temporal(t):
return t.id in _TEMPORAL_TYPES
@doc(is_null, datatype="timestamp")
def is_timestamp(t):
return t.id == lib.Type_TIMESTAMP
@doc(is_null, datatype="duration")
def is_duration(t):
return t.id == lib.Type_DURATION
@doc(is_null, datatype="time")
def is_time(t):
return t.id in _TIME_TYPES
@doc(is_null, datatype="time32")
def is_time32(t):
return t.id == lib.Type_TIME32
@doc(is_null, datatype="time64")
def is_time64(t):
return t.id == lib.Type_TIME64
@doc(is_null, datatype="variable-length binary")
def is_binary(t):
return t.id == lib.Type_BINARY
@doc(is_null, datatype="large variable-length binary")
def is_large_binary(t):
return t.id == lib.Type_LARGE_BINARY
@doc(method="is_string")
def is_unicode(t):
"""
Alias for {method}.
Parameters
----------
t : DataType
"""
return is_string(t)
@doc(is_null, datatype="string (utf8 unicode)")
def is_string(t):
return t.id == lib.Type_STRING
@doc(is_unicode, method="is_large_string")
def is_large_unicode(t):
return is_large_string(t)
@doc(is_null, datatype="large string (utf8 unicode)")
def is_large_string(t):
return t.id == lib.Type_LARGE_STRING
@doc(is_null, datatype="fixed size binary")
def is_fixed_size_binary(t):
return t.id == lib.Type_FIXED_SIZE_BINARY
@doc(is_null, datatype="variable-length binary view")
def is_binary_view(t):
return t.id == lib.Type_BINARY_VIEW
@doc(is_null, datatype="variable-length string (utf-8) view")
def is_string_view(t):
return t.id == lib.Type_STRING_VIEW
@doc(is_null, datatype="date")
def is_date(t):
return t.id in _DATE_TYPES
@doc(is_null, datatype="date32 (days)")
def is_date32(t):
return t.id == lib.Type_DATE32
@doc(is_null, datatype="date64 (milliseconds)")
def is_date64(t):
return t.id == lib.Type_DATE64
@doc(is_null, datatype="map")
def is_map(t):
return t.id == lib.Type_MAP
@doc(is_null, datatype="decimal")
def is_decimal(t):
return t.id in _DECIMAL_TYPES
@doc(is_null, datatype="decimal32")
def is_decimal32(t):
return t.id == lib.Type_DECIMAL32
@doc(is_null, datatype="decimal64")
def is_decimal64(t):
return t.id == lib.Type_DECIMAL64
@doc(is_null, datatype="decimal128")
def is_decimal128(t):
return t.id == lib.Type_DECIMAL128
@doc(is_null, datatype="decimal256")
def is_decimal256(t):
return t.id == lib.Type_DECIMAL256
@doc(is_null, datatype="dictionary-encoded")
def is_dictionary(t):
return t.id == lib.Type_DICTIONARY
@doc(is_null, datatype="interval")
def is_interval(t):
return t.id == lib.Type_INTERVAL_MONTH_DAY_NANO
@doc(is_null, datatype="primitive type")
def is_primitive(t):
return lib._is_primitive(t.id)
|