# 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 PythonfromenumimportIntEnumfrompyarrow.libimport(is_boolean_value,# noqais_integer_value,is_float_value)importpyarrow.libaslibfrompyarrow.utilimportdoc_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[docs]classTypesEnum(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_NABOOL=lib.Type_BOOLINT8=lib.Type_INT8INT16=lib.Type_INT16INT32=lib.Type_INT32INT64=lib.Type_INT64UINT8=lib.Type_UINT8UINT16=lib.Type_UINT16UINT32=lib.Type_UINT32UINT64=lib.Type_UINT64HALF_FLOAT=lib.Type_HALF_FLOATFLOAT=lib.Type_FLOATDOUBLE=lib.Type_DOUBLEBINARY=lib.Type_BINARYBINARY_VIEW=lib.Type_BINARY_VIEWLARGE_BINARY=lib.Type_LARGE_BINARYSTRING=lib.Type_STRINGSTRING_VIEW=lib.Type_STRING_VIEWLARGE_STRING=lib.Type_LARGE_STRINGFIXED_SIZE_BINARY=lib.Type_FIXED_SIZE_BINARYDECIMAL32=lib.Type_DECIMAL32DECIMAL64=lib.Type_DECIMAL64DECIMAL128=lib.Type_DECIMAL128DECIMAL256=lib.Type_DECIMAL256LIST=lib.Type_LISTLARGE_LIST=lib.Type_LARGE_LISTLIST_VIEW=lib.Type_LIST_VIEWLARGE_LIST_VIEW=lib.Type_LARGE_LIST_VIEWMAP=lib.Type_MAPFIXED_SIZE_LIST=lib.Type_FIXED_SIZE_LISTSTRUCT=lib.Type_STRUCTSPARSE_UNION=lib.Type_SPARSE_UNIONDENSE_UNION=lib.Type_DENSE_UNIONRUN_END_ENCODED=lib.Type_RUN_END_ENCODEDDATE32=lib.Type_DATE32DATE64=lib.Type_DATE64TIME32=lib.Type_TIME32TIME64=lib.Type_TIME64TIMESTAMP=lib.Type_TIMESTAMPINTERVAL_MONTHS=lib.Type_INTERVAL_MONTHSINTERVAL_DAY_TIME=lib.Type_INTERVAL_DAY_TIMEINTERVAL_MONTH_DAY_NANO=lib.Type_INTERVAL_MONTH_DAY_NANODURATION=lib.Type_DURATIONDICTIONARY=lib.Type_DICTIONARY [docs]@doc(datatype="null")defis_null(t):""" Return True if value is an instance of type: {datatype}. Parameters ---------- t : DataType """returnt.id==lib.Type_NA [docs]@doc(is_null,datatype="boolean")defis_boolean(t):returnt.id==lib.Type_BOOL [docs]@doc(is_null,datatype="any integer")defis_integer(t):returnt.idin_INTEGER_TYPES [docs]@doc(is_null,datatype="signed integer")defis_signed_integer(t):returnt.idin_SIGNED_INTEGER_TYPES [docs]@doc(is_null,datatype="unsigned integer")defis_unsigned_integer(t):returnt.idin_UNSIGNED_INTEGER_TYPES [docs]@doc(is_null,datatype="int8")defis_int8(t):returnt.id==lib.Type_INT8 [docs]@doc(is_null,datatype="int16")defis_int16(t):returnt.id==lib.Type_INT16 [docs]@doc(is_null,datatype="int32")defis_int32(t):returnt.id==lib.Type_INT32 [docs]@doc(is_null,datatype="int64")defis_int64(t):returnt.id==lib.Type_INT64 [docs]@doc(is_null,datatype="uint8")defis_uint8(t):returnt.id==lib.Type_UINT8 [docs]@doc(is_null,datatype="uint16")defis_uint16(t):returnt.id==lib.Type_UINT16 [docs]@doc(is_null,datatype="uint32")defis_uint32(t):returnt.id==lib.Type_UINT32 [docs]@doc(is_null,datatype="uint64")defis_uint64(t):returnt.id==lib.Type_UINT64 [docs]@doc(is_null,datatype="floating point numeric")defis_floating(t):returnt.idin_FLOATING_TYPES [docs]@doc(is_null,datatype="float16 (half-precision)")defis_float16(t):returnt.id==lib.Type_HALF_FLOAT [docs]@doc(is_null,datatype="float32 (single precision)")defis_float32(t):returnt.id==lib.Type_FLOAT [docs]@doc(is_null,datatype="float64 (double precision)")defis_float64(t):returnt.id==lib.Type_DOUBLE [docs]@doc(is_null,datatype="list")defis_list(t):returnt.id==lib.Type_LIST [docs]@doc(is_null,datatype="large list")defis_large_list(t):returnt.id==lib.Type_LARGE_LIST [docs]@doc(is_null,datatype="fixed size list")defis_fixed_size_list(t):returnt.id==lib.Type_FIXED_SIZE_LIST [docs]@doc(is_null,datatype="list view")defis_list_view(t):returnt.id==lib.Type_LIST_VIEW [docs]@doc(is_null,datatype="large list view")defis_large_list_view(t):returnt.id==lib.Type_LARGE_LIST_VIEW [docs]@doc(is_null,datatype="struct")defis_struct(t):returnt.id==lib.Type_STRUCT [docs]@doc(is_null,datatype="union")defis_union(t):returnt.idin_UNION_TYPES [docs]@doc(is_null,datatype="nested type")defis_nested(t):returnt.idin_NESTED_TYPES [docs]@doc(is_null,datatype="run-end encoded")defis_run_end_encoded(t):returnt.id==lib.Type_RUN_END_ENCODED [docs]@doc(is_null,datatype="date, time, timestamp or duration")defis_temporal(t):returnt.idin_TEMPORAL_TYPES [docs]@doc(is_null,datatype="timestamp")defis_timestamp(t):returnt.id==lib.Type_TIMESTAMP [docs]@doc(is_null,datatype="duration")defis_duration(t):returnt.id==lib.Type_DURATION [docs]@doc(is_null,datatype="time")defis_time(t):returnt.idin_TIME_TYPES [docs]@doc(is_null,datatype="time32")defis_time32(t):returnt.id==lib.Type_TIME32 [docs]@doc(is_null,datatype="time64")defis_time64(t):returnt.id==lib.Type_TIME64 [docs]@doc(is_null,datatype="variable-length binary")defis_binary(t):returnt.id==lib.Type_BINARY [docs]@doc(is_null,datatype="large variable-length binary")defis_large_binary(t):returnt.id==lib.Type_LARGE_BINARY [docs]@doc(method="is_string")defis_unicode(t):""" Alias for {method}. Parameters ---------- t : DataType """returnis_string(t) [docs]@doc(is_null,datatype="string (utf8 unicode)")defis_string(t):returnt.id==lib.Type_STRING [docs]@doc(is_unicode,method="is_large_string")defis_large_unicode(t):returnis_large_string(t) [docs]@doc(is_null,datatype="large string (utf8 unicode)")defis_large_string(t):returnt.id==lib.Type_LARGE_STRING [docs]@doc(is_null,datatype="fixed size binary")defis_fixed_size_binary(t):returnt.id==lib.Type_FIXED_SIZE_BINARY [docs]@doc(is_null,datatype="variable-length binary view")defis_binary_view(t):returnt.id==lib.Type_BINARY_VIEW [docs]@doc(is_null,datatype="variable-length string (utf-8) view")defis_string_view(t):returnt.id==lib.Type_STRING_VIEW [docs]@doc(is_null,datatype="date")defis_date(t):returnt.idin_DATE_TYPES [docs]@doc(is_null,datatype="date32 (days)")defis_date32(t):returnt.id==lib.Type_DATE32 [docs]@doc(is_null,datatype="date64 (milliseconds)")defis_date64(t):returnt.id==lib.Type_DATE64 [docs]@doc(is_null,datatype="map")defis_map(t):returnt.id==lib.Type_MAP [docs]@doc(is_null,datatype="decimal")defis_decimal(t):returnt.idin_DECIMAL_TYPES @doc(is_null,datatype="decimal32")defis_decimal32(t):returnt.id==lib.Type_DECIMAL32@doc(is_null,datatype="decimal64")defis_decimal64(t):returnt.id==lib.Type_DECIMAL64[docs]@doc(is_null,datatype="decimal128")defis_decimal128(t):returnt.id==lib.Type_DECIMAL128 [docs]@doc(is_null,datatype="decimal256")defis_decimal256(t):returnt.id==lib.Type_DECIMAL256 [docs]@doc(is_null,datatype="dictionary-encoded")defis_dictionary(t):returnt.id==lib.Type_DICTIONARY [docs]@doc(is_null,datatype="interval")defis_interval(t):returnt.id==lib.Type_INTERVAL_MONTH_DAY_NANO [docs]@doc(is_null,datatype="primitive type")defis_primitive(t):returnlib._is_primitive(t.id)