import copy import glob import os import random import string import sys import uuid from collections import OrderedDict import numpy as np import pyarrow import pytest from numpy.testing import assert_array_equal import tiledb from tiledb.dataframe_ import ColumnInfo from .common import ( DiskTestCase, assert_dict_arrays_equal, dtype_max, dtype_min, has_pandas, rand_ascii, rand_ascii_bytes, rand_datetime64_array, rand_utf8, ) from .datatypes import RaggedDtype if not has_pandas(): pytest.skip("pandas>=1.0,<3.0 not installed", allow_module_level=True) else: import pandas as pd tm = pd._testing def make_dataframe_basic1(col_size=10): # ensure no duplicates when using as string dim chars = list() for _ in range(col_size): next = rand_ascii_bytes(2) while next in chars: next = rand_ascii_bytes(2) chars.append(next) data_dict = { "time": rand_datetime64_array(col_size, include_extremes=False), "x": np.array([rand_ascii(4).encode("UTF-8") for _ in range(col_size)]), "chars": np.array(chars), "cccc": np.arange(0, col_size), "q": np.array([rand_utf8(np.random.randint(1, 100)) for _ in range(col_size)]), "t": np.array([rand_utf8(4) for _ in range(col_size)]), "r": np.array( [rand_ascii_bytes(np.random.randint(1, 100)) for _ in range(col_size)] ), "s": np.array([rand_ascii() for _ in range(col_size)]), "u": np.array([rand_ascii_bytes().decode() for _ in range(col_size)]), "v": np.array([rand_ascii_bytes() for _ in range(col_size)]), "vals_int64": np.random.randint( dtype_max(np.int64), size=col_size, dtype=np.int64 ), "vals_float64": np.random.rand(col_size), } # TODO: dump this dataframe to pickle/base64 so that it can be reconstructed if # there are weird failures on CI? df = pd.DataFrame.from_dict(data_dict) return df def make_dataframe_basic2(): # This code is from Pandas feather i/o tests "test_basic" function: # https://github.com/pandas-dev/pandas/blob/master/pandas/tests/io/test_feather.py # (available under BSD 3-clause license # https://github.com/pandas-dev/pandas/blob/master/LICENSE df = pd.DataFrame( { "string": list("abc"), "int": list(range(1, 4)), "uint": np.arange(3, 6).astype("u1"), "float": np.arange(4.0, 7.0, dtype="float64"), # TODO "float_with_null": [1.0, np.nan, 3], "bool": [True, False, True], # TODO "bool_with_null": [True, np.nan, False], # "cat": pd.Categorical(list("abc")), "dt": pd.date_range("20130101", periods=3), # "dttz": pd.date_range("20130101", periods=3, tz="US/Eastern"), # "dt_with_null": [ # pd.Timestamp("20130101"), # pd.NaT, # pd.Timestamp("20130103"), # ], "dtns": pd.date_range("20130101", periods=3, freq="ns"), } ) return df def make_dataframe_basic3(col_size=10, time_range=(None, None)): df_dict = { "time": rand_datetime64_array( col_size, start=time_range[0], stop=time_range[1], include_extremes=False ), "double_range": np.linspace(-1000, 1000, col_size), "int_vals": np.random.randint( dtype_max(np.int64), size=col_size, dtype=np.int64 ), } df = pd.DataFrame(df_dict) return df def make_dataframe_categorical(): df = pd.DataFrame( { "int": [0, 1, 2, 3], "categorical_string": pd.Series(["A", "B", "A", "B"], dtype="category"), "categorical_int": pd.Series( np.array([1, 2, 3, 4], dtype=np.int64), dtype="category" ), # 'categorical_bool': pd.Series([True, False, True, False], dtype="category"), } ) return df class TestColumnInfo(DiskTestCase): def assertColumnInfo(self, info, info_dtype, info_repr=None, info_nullable=False): assert isinstance(info.dtype, np.dtype) assert info.dtype == info_dtype assert info.repr is None or isinstance(info.repr, str) assert info.repr == info_repr assert isinstance(info.nullable, bool) assert info.nullable == info_nullable @pytest.mark.parametrize( "type_specs, info_dtype, info_repr, info_nullable", [ # bool types ( [bool, "b1"], np.dtype("uint8" if tiledb.libtiledb.version() < (2, 10) else "bool"), "bool", False, ), ( [pd.BooleanDtype()], np.dtype("uint8" if tiledb.libtiledb.version() < (2, 10) else "bool"), "boolean", True, ), # numeric types ([np.uint8, "u1"], np.dtype("uint8"), None, False), ([np.uint16, "u2"], np.dtype("uint16"), None, False), ([np.uint32, "u4"], np.dtype("uint32"), None, False), ([np.uint64, "u8"], np.dtype("uint64"), None, False), ([np.int8, "i1"], np.dtype("int8"), None, False), ([np.int16, "i2"], np.dtype("int16"), None, False), ([np.int32, "i4"], np.dtype("int32"), None, False), ([np.int64, "i8"], np.dtype("int64"), None, False), ([np.float32, "f4"], np.dtype("float32"), None, False), ([np.float64, "f8", float], np.dtype("float64"), None, False), # nullable int types ([pd.UInt8Dtype(), "UInt8"], np.dtype("uint8"), "UInt8", True), ([pd.UInt16Dtype(), "UInt16"], np.dtype("uint16"), "UInt16", True), ([pd.UInt32Dtype(), "UInt32"], np.dtype("uint32"), "UInt32", True), ([pd.UInt64Dtype(), "UInt64"], np.dtype("uint64"), "UInt64", True), ([pd.Int8Dtype(), "Int8"], np.dtype("int8"), "Int8", True), ([pd.Int16Dtype(), "Int16"], np.dtype("int16"), "Int16", True), ([pd.Int32Dtype(), "Int32"], np.dtype("int32"), "Int32", True), ([pd.Int64Dtype(), "Int64"], np.dtype("int64"), "Int64", True), # datetime types (["datetime64[ns]"], np.dtype("