Skip to content

Datatypes

tablite.datatypes

Attributes

tablite.datatypes.matched_types = {int: DataTypes._infer_int, str: DataTypes._infer_str, float: DataTypes._infer_float, bool: DataTypes._infer_bool, date: DataTypes._infer_date, datetime: DataTypes._infer_datetime, time: DataTypes._infer_time} module-attribute

Classes

tablite.datatypes.DataTypes

Bases: object

DataTypes is the conversion library for all datatypes.

It supports any / all python datatypes.

Attributes
tablite.datatypes.DataTypes.int = int class-attribute instance-attribute
tablite.datatypes.DataTypes.str = str class-attribute instance-attribute
tablite.datatypes.DataTypes.float = float class-attribute instance-attribute
tablite.datatypes.DataTypes.bool = bool class-attribute instance-attribute
tablite.datatypes.DataTypes.date = date class-attribute instance-attribute
tablite.datatypes.DataTypes.datetime = datetime class-attribute instance-attribute
tablite.datatypes.DataTypes.time = time class-attribute instance-attribute
tablite.datatypes.DataTypes.timedelta = timedelta class-attribute instance-attribute
tablite.datatypes.DataTypes.numeric_types = {int, float, date, time, datetime} class-attribute instance-attribute
tablite.datatypes.DataTypes.epoch = datetime(2000, 1, 1, 0, 0, 0, 0, timezone.utc) class-attribute instance-attribute
tablite.datatypes.DataTypes.epoch_no_tz = datetime(2000, 1, 1, 0, 0, 0, 0) class-attribute instance-attribute
tablite.datatypes.DataTypes.digits = '1234567890' class-attribute instance-attribute
tablite.datatypes.DataTypes.decimals = set('1234567890-+eE.') class-attribute instance-attribute
tablite.datatypes.DataTypes.integers = set('1234567890-+') class-attribute instance-attribute
tablite.datatypes.DataTypes.nones = {'null', 'Null', 'NULL', '#N/A', '#n/a', '', 'None', None, np.nan} class-attribute instance-attribute
tablite.datatypes.DataTypes.none_type = type(None) class-attribute instance-attribute
tablite.datatypes.DataTypes.bytes_functions = {type(None): b_none, bool: b_bool, int: b_int, float: b_float, str: b_str, bytes: b_bytes, datetime: b_datetime, date: b_date, time: b_time, timedelta: b_timedelta} class-attribute instance-attribute
tablite.datatypes.DataTypes.type_code_functions = {1: _none, 2: _bool, 3: _int, 4: _float, 5: _str, 6: _bytes, 7: _datetime, 8: _date, 9: _time, 10: _timedelta, 11: _unpickle} class-attribute instance-attribute
tablite.datatypes.DataTypes.pytype_from_type_code = {1: type(None), 2: bool, 3: int, 4: float, 5: str, 6: bytes, 7: datetime, 8: date, 9: time, 10: timedelta, 11: 'pickled object'} class-attribute instance-attribute
tablite.datatypes.DataTypes.date_formats = {'NNNN-NN-NN': lambda x: date(*int(i) for i in x.split('-')), 'NNNN-N-NN': lambda x: date(*int(i) for i in x.split('-')), 'NNNN-NN-N': lambda x: date(*int(i) for i in x.split('-')), 'NNNN-N-N': lambda x: date(*int(i) for i in x.split('-')), 'NN-NN-NNNN': lambda x: date(*[int(i) for i in x.split('-')][::-1]), 'N-NN-NNNN': lambda x: date(*[int(i) for i in x.split('-')][::-1]), 'NN-N-NNNN': lambda x: date(*[int(i) for i in x.split('-')][::-1]), 'N-N-NNNN': lambda x: date(*[int(i) for i in x.split('-')][::-1]), 'NNNN.NN.NN': lambda x: date(*int(i) for i in x.split('.')), 'NNNN.N.NN': lambda x: date(*int(i) for i in x.split('.')), 'NNNN.NN.N': lambda x: date(*int(i) for i in x.split('.')), 'NNNN.N.N': lambda x: date(*int(i) for i in x.split('.')), 'NN.NN.NNNN': lambda x: date(*[int(i) for i in x.split('.')][::-1]), 'N.NN.NNNN': lambda x: date(*[int(i) for i in x.split('.')][::-1]), 'NN.N.NNNN': lambda x: date(*[int(i) for i in x.split('.')][::-1]), 'N.N.NNNN': lambda x: date(*[int(i) for i in x.split('.')][::-1]), 'NNNN/NN/NN': lambda x: date(*int(i) for i in x.split('/')), 'NNNN/N/NN': lambda x: date(*int(i) for i in x.split('/')), 'NNNN/NN/N': lambda x: date(*int(i) for i in x.split('/')), 'NNNN/N/N': lambda x: date(*int(i) for i in x.split('/')), 'NN/NN/NNNN': lambda x: date(*[int(i) for i in x.split('/')][::-1]), 'N/NN/NNNN': lambda x: date(*[int(i) for i in x.split('/')][::-1]), 'NN/N/NNNN': lambda x: date(*[int(i) for i in x.split('/')][::-1]), 'N/N/NNNN': lambda x: date(*[int(i) for i in x.split('/')][::-1]), 'NNNN NN NN': lambda x: date(*int(i) for i in x.split(' ')), 'NNNN N NN': lambda x: date(*int(i) for i in x.split(' ')), 'NNNN NN N': lambda x: date(*int(i) for i in x.split(' ')), 'NNNN N N': lambda x: date(*int(i) for i in x.split(' ')), 'NN NN NNNN': lambda x: date(*[int(i) for i in x.split(' ')][::-1]), 'N N NNNN': lambda x: date(*[int(i) for i in x.split(' ')][::-1]), 'NN N NNNN': lambda x: date(*[int(i) for i in x.split(' ')][::-1]), 'N NN NNNN': lambda x: date(*[int(i) for i in x.split(' ')][::-1]), 'NNNNNNNN': lambda x: date(*(int(x[:4]), int(x[4:6]), int(x[6:])))} class-attribute instance-attribute
tablite.datatypes.DataTypes.datetime_formats = {'NNNN-NN-NNTNN:NN:NN': lambda x: DataTypes.pattern_to_datetime(x), 'NNNN-NN-NNTNN:NN': lambda x: DataTypes.pattern_to_datetime(x), 'NNNN-NN-NN NN:NN:NN': lambda x: DataTypes.pattern_to_datetime(x, T=' '), 'NNNN-NN-NN NN:NN': lambda x: DataTypes.pattern_to_datetime(x, T=' '), 'NNNN/NN/NNTNN:NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='/'), 'NNNN/NN/NNTNN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='/'), 'NNNN/NN/NN NN:NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='/', T=' '), 'NNNN/NN/NN NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='/', T=' '), 'NNNN NN NNTNN:NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd=' '), 'NNNN NN NNTNN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd=' '), 'NNNN NN NN NN:NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd=' ', T=' '), 'NNNN NN NN NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd=' ', T=' '), 'NNNN.NN.NNTNN:NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='.'), 'NNNN.NN.NNTNN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='.'), 'NNNN.NN.NN NN:NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='.', T=' '), 'NNNN.NN.NN NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='.', T=' '), 'NN-NN-NNNNTNN:NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='-', T=' ', day_first=True), 'NN-NN-NNNNTNN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='-', T=' ', day_first=True), 'NN-NN-NNNN NN:NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='-', T=' ', day_first=True), 'NN-NN-NNNN NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='-', T=' ', day_first=True), 'NN/NN/NNNNTNN:NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='/', day_first=True), 'NN/NN/NNNNTNN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='/', day_first=True), 'NN/NN/NNNN NN:NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='/', T=' ', day_first=True), 'NN/NN/NNNN NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='/', T=' ', day_first=True), 'NN NN NNNNTNN:NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='/', day_first=True), 'NN NN NNNNTNN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='/', day_first=True), 'NN NN NNNN NN:NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='/', day_first=True), 'NN NN NNNN NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='/', day_first=True), 'NN.NN.NNNNTNN:NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='.', day_first=True), 'NN.NN.NNNNTNN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='.', day_first=True), 'NN.NN.NNNN NN:NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='.', day_first=True), 'NN.NN.NNNN NN:NN': lambda x: DataTypes.pattern_to_datetime(x, ymd='.', day_first=True), 'NNNNNNNNTNNNNNN': lambda x: DataTypes.pattern_to_datetime(x, compact=1), 'NNNNNNNNTNNNN': lambda x: DataTypes.pattern_to_datetime(x, compact=1), 'NNNNNNNNTNN': lambda x: DataTypes.pattern_to_datetime(x, compact=1), 'NNNNNNNNNN': lambda x: DataTypes.pattern_to_datetime(x, compact=2), 'NNNNNNNNNNNN': lambda x: DataTypes.pattern_to_datetime(x, compact=2), 'NNNNNNNNNNNNNN': lambda x: DataTypes.pattern_to_datetime(x, compact=2), 'NNNNNNNNTNN:NN:NN': lambda x: DataTypes.pattern_to_datetime(x, compact=3)} class-attribute instance-attribute
tablite.datatypes.DataTypes.types = [datetime, date, time, int, bool, float, str] class-attribute instance-attribute
Functions
tablite.datatypes.DataTypes.type_code(value) classmethod
Source code in tablite/datatypes.py
47
48
49
50
51
52
53
54
55
@classmethod
def type_code(cls, value):
    if type(value) in cls._type_codes:
        return cls._type_codes[type(value)]
    elif hasattr(value, "dtype"):
        dtype = pytype(value)
        return cls._type_codes[dtype]
    else:
        return cls._type_codes["pickle"]
tablite.datatypes.DataTypes.b_none(v)
Source code in tablite/datatypes.py
57
58
def b_none(v):
    return b"None"
tablite.datatypes.DataTypes.b_bool(v)
Source code in tablite/datatypes.py
60
61
def b_bool(v):
    return bytes(str(v), encoding="utf-8")
tablite.datatypes.DataTypes.b_int(v)
Source code in tablite/datatypes.py
63
64
def b_int(v):
    return bytes(str(v), encoding="utf-8")
tablite.datatypes.DataTypes.b_float(v)
Source code in tablite/datatypes.py
66
67
def b_float(v):
    return bytes(str(v), encoding="utf-8")
tablite.datatypes.DataTypes.b_str(v)
Source code in tablite/datatypes.py
69
70
def b_str(v):
    return v.encode("utf-8")
tablite.datatypes.DataTypes.b_bytes(v)
Source code in tablite/datatypes.py
72
73
def b_bytes(v):
    return v
tablite.datatypes.DataTypes.b_datetime(v)
Source code in tablite/datatypes.py
75
76
def b_datetime(v):
    return bytes(v.isoformat(), encoding="utf-8")
tablite.datatypes.DataTypes.b_date(v)
Source code in tablite/datatypes.py
78
79
def b_date(v):
    return bytes(v.isoformat(), encoding="utf-8")
tablite.datatypes.DataTypes.b_time(v)
Source code in tablite/datatypes.py
81
82
def b_time(v):
    return bytes(v.isoformat(), encoding="utf-8")
tablite.datatypes.DataTypes.b_timedelta(v)
Source code in tablite/datatypes.py
84
85
def b_timedelta(v):
    return bytes(str(float(v.days + (v.seconds / (24 * 60 * 60)))), "utf-8")
tablite.datatypes.DataTypes.b_pickle(v)
Source code in tablite/datatypes.py
87
88
def b_pickle(v):
    return pickle.dumps(v, protocol=0)
tablite.datatypes.DataTypes.to_bytes(v) classmethod
Source code in tablite/datatypes.py
103
104
105
106
107
108
109
110
111
112
@classmethod
def to_bytes(cls, v):
    if type(v) in cls.bytes_functions:  # it's a python native type
        f = cls.bytes_functions[type(v)]
    elif hasattr(v, "dtype"):  # it's a numpy/c type.
        dtype = pytype(v)
        f = cls.bytes_functions[dtype]
    else:
        f = cls.b_pickle
    return f(v)
tablite.datatypes.DataTypes.from_type_code(value, code) classmethod
Source code in tablite/datatypes.py
177
178
179
180
@classmethod
def from_type_code(cls, value, code):
    f = cls.type_code_functions[code]
    return f(value)
tablite.datatypes.DataTypes.pattern_to_datetime(iso_string, ymd=None, T=None, compact=0, day_first=False) staticmethod
Source code in tablite/datatypes.py
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
@staticmethod
def pattern_to_datetime(iso_string, ymd=None, T=None, compact=0, day_first=False):
    assert isinstance(iso_string, str)
    if compact:
        s = iso_string
        if compact == 1:  # has T
            slices = [
                (0, 4, "-"),
                (4, 6, "-"),
                (6, 8, "T"),
                (9, 11, ":"),
                (11, 13, ":"),
                (13, len(s), ""),
            ]
        elif compact == 2:  # has no T.
            slices = [
                (0, 4, "-"),
                (4, 6, "-"),
                (6, 8, "T"),
                (8, 10, ":"),
                (10, 12, ":"),
                (12, len(s), ""),
            ]
        elif compact == 3:  # has T and :
            slices = [
                (0, 4, "-"),
                (4, 6, "-"),
                (6, 8, "T"),
                (9, 11, ":"),
                (12, 14, ":"),
                (15, len(s), ""),
            ]
        else:
            raise TypeError
        iso_string = "".join([s[a:b] + c for a, b, c in slices if b <= len(s)])
        iso_string = iso_string.rstrip(":")

    if day_first:
        s = iso_string
        iso_string = "".join((s[6:10], "-", s[3:5], "-", s[0:2], s[10:]))

    if "," in iso_string:
        iso_string = iso_string.replace(",", ".")

    dot = iso_string[::-1].find(".")
    if 0 < dot < 10:
        ix = len(iso_string) - dot
        microsecond = int(float(f"0{iso_string[ix - 1:]}") * 10**6)
        # fmt:off
        iso_string = iso_string[: len(iso_string) - dot] + str(microsecond).rjust(6, "0")
        # fmt:on
    if ymd:
        iso_string = iso_string.replace(ymd, "-", 2)
    if T:
        iso_string = iso_string.replace(T, "T")
    return datetime.fromisoformat(iso_string)
tablite.datatypes.DataTypes.round(value, multiple, up=None) classmethod

a nicer way to round numbers.

PARAMETER DESCRIPTION
value

value to be rounded

TYPE: (float, integer, datetime)

multiple

value to be used as the based of rounding. 1) multiple = 1 is the same as rounding to whole integers. 2) multiple = 0.001 is the same as rounding to 3 digits precision. 3) mulitple = 3.1415 is rounding to nearest multiplier of 3.1415 4) value = datetime(2022,8,18,11,14,53,440) 5) multiple = timedelta(hours=0.5) 6) xround(value,multiple) is datetime(2022,8,18,11,0)

TYPE: (float, integer, timedelta)

up

None (default) or boolean rounds half, up or down. round(1.6, 1) rounds to 2. round(1.4, 1) rounds to 1. round(1.5, 1, up=True) rounds to 2. round(1.5, 1, up=False) rounds to 1.

TYPE: (None, bool) DEFAULT: None

RETURNS DESCRIPTION

float,integer,datetime: rounded value in same type as input.

Source code in tablite/datatypes.py
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
@classmethod
def round(cls, value, multiple, up=None):
    """a nicer way to round numbers.

    Args:
        value (float,integer,datetime): value to be rounded

        multiple (float,integer,timedelta): value to be used as the based of rounding.
            1) multiple = 1 is the same as rounding to whole integers.
            2) multiple = 0.001 is the same as rounding to 3 digits precision.
            3) mulitple = 3.1415 is rounding to nearest multiplier of 3.1415
            4) value = datetime(2022,8,18,11,14,53,440)
            5) multiple = timedelta(hours=0.5)
            6) xround(value,multiple) is datetime(2022,8,18,11,0)

        up (None, bool, optional):
            None (default) or boolean rounds half, up or down.
            round(1.6, 1) rounds to 2.
            round(1.4, 1) rounds to 1.
            round(1.5, 1, up=True) rounds to 2.
            round(1.5, 1, up=False) rounds to 1.

    Returns:
        float,integer,datetime: rounded value in same type as input.
    """
    epoch = 0
    if isinstance(value, (datetime)) and isinstance(multiple, timedelta):
        if value.tzinfo is None:
            epoch = cls.epoch_no_tz
        else:
            epoch = cls.epoch

    value2 = value - epoch
    if value2 == 0:
        return value2

    low = (value2 // multiple) * multiple
    high = low + multiple
    if up is True:
        return high + epoch
    elif up is False:
        return low + epoch
    else:
        if abs((high + epoch) - value) < abs(value - (low + epoch)):
            return high + epoch
        else:
            return low + epoch
tablite.datatypes.DataTypes.to_json(v) staticmethod

converts any python type to json.

PARAMETER DESCRIPTION
v

value to convert to json

TYPE: any

RETURNS DESCRIPTION

json compatible value from v

Source code in tablite/datatypes.py
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
407
@staticmethod
def to_json(v):
    """converts any python type to json.

    Args:
        v (any): value to convert to json

    Returns:
        json compatible value from v
    """
    if hasattr(v, "dtype"):
        v = numpy_to_python(v)
    if v is None:
        return v
    elif v is False:
        # using isinstance(v, bool): won't work as False also is int of zero.
        return str(v)
    elif v is True:
        return str(v)
    elif isinstance(v, int):
        return v
    elif isinstance(v, str):
        return v
    elif isinstance(v, float):
        return v
    elif isinstance(v, datetime):
        return v.isoformat()
    elif isinstance(v, time):
        return v.isoformat()
    elif isinstance(v, date):
        return v.isoformat()
    elif isinstance(v, timedelta):
        return f"P{v.days}DT{v.seconds + (v.microseconds / 1e6)}S"
    else:
        raise TypeError(f"The datatype {type(v)} is not supported.")
tablite.datatypes.DataTypes.from_json(v, dtype) staticmethod

converts json to python datatype

PARAMETER DESCRIPTION
v

value

TYPE: any

dtype

any python type

TYPE: python type

RETURNS DESCRIPTION

python type of value v

Source code in tablite/datatypes.py
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
@staticmethod
def from_json(v, dtype):
    """converts json to python datatype

    Args:
        v (any): value
        dtype (python type): any python type

    Returns:
        python type of value v
    """
    if v in DataTypes.nones:
        if dtype is str and v == "":
            return ""
        else:
            return None
    if dtype is int:
        return int(v)
    elif dtype is str:
        return str(v)
    elif dtype is float:
        return float(v)
    elif dtype is bool:
        if v == "False":
            return False
        elif v == "True":
            return True
        else:
            raise ValueError(v)
    elif dtype is date:
        return date.fromisoformat(v)
    elif dtype is datetime:
        return datetime.fromisoformat(v)
    elif dtype is time:
        return time.fromisoformat(v)
    elif dtype is timedelta:
        L = v.split("DT")
        days = int(L[0].lstrip("P"))
        seconds = float(L[1].rstrip("S"))
        return timedelta(days, seconds)
    else:
        raise TypeError(f"The datatype {str(dtype)} is not supported.")
tablite.datatypes.DataTypes.guess_types(*values) staticmethod

Attempts to guess the datatype for *values returns dict with matching datatypes and probabilities

RETURNS DESCRIPTION
dict

{key: type, value: probability}

Source code in tablite/datatypes.py
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
@staticmethod
def guess_types(*values):
    """Attempts to guess the datatype for *values
    returns dict with matching datatypes and probabilities

    Returns:
        dict: {key: type, value: probability}
    """
    d = defaultdict(int)
    probability = Rank(DataTypes.types[:])

    for value in values:
        if hasattr(value, "dtype"):
            value = numpy_to_python(value)

        for dtype in probability:
            try:
                _ = DataTypes.infer(value, dtype)
                d[dtype] += 1
                probability.match(dtype)
                break
            except (ValueError, TypeError):
                pass
    if not d:
        d[str] = len(values)
    return {k: round(v / len(values), 3) for k, v in d.items()}
tablite.datatypes.DataTypes.guess(*values) staticmethod

Makes a best guess the datatype for *values returns list of native python values

RETURNS DESCRIPTION
list

list of native python values

Source code in tablite/datatypes.py
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
@staticmethod
def guess(*values):
    """Makes a best guess the datatype for *values
    returns list of native python values

    Returns:
        list: list of native python values
    """
    probability = Rank(*DataTypes.types[:])
    matches = [None for _ in values[0]]

    for ix, value in enumerate(values[0]):
        if hasattr(value, "dtype"):
            value = numpy_to_python(value)
        for dtype in probability:
            try:
                matches[ix] = DataTypes.infer(value, dtype)
                probability.match(dtype)
                break
            except (ValueError, TypeError):
                pass
    return matches
tablite.datatypes.DataTypes.infer(v, dtype) classmethod
Source code in tablite/datatypes.py
505
506
507
508
509
510
511
512
513
514
515
516
517
@classmethod
def infer(cls, v, dtype):
    if isinstance(v, str) and dtype == str:
        # we got a string, we're trying to infer it to string, we shouldn't check for None-ness
        return v

    if v in DataTypes.nones:
        return None

    if dtype not in matched_types:
        raise TypeError(f"The datatype {str(dtype)} is not supported.")

    return matched_types[dtype](v)

tablite.datatypes.Rank(*items)

Bases: object

Source code in tablite/datatypes.py
730
731
732
733
def __init__(self, *items):
    self.items = {i: ix for i, ix in zip(items, range(len(items)))}
    self.ranks = [0 for _ in items]
    self.items_list = [i for i in items]
Attributes
tablite.datatypes.Rank.items = {i: ixfor (i, ix) in zip(items, range(len(items)))} instance-attribute
tablite.datatypes.Rank.ranks = [0 for _ in items] instance-attribute
tablite.datatypes.Rank.items_list = [i for i in items] instance-attribute
Functions
tablite.datatypes.Rank.match(k)
Source code in tablite/datatypes.py
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
def match(self, k):  # k+=1
    ix = self.items[k]
    r = self.ranks
    r[ix] += 1

    if ix > 0:
        p = self.items_list
        while (
            r[ix] > r[ix - 1] and ix > 0
        ):  # use a simple bubble sort to maintain rank
            r[ix], r[ix - 1] = r[ix - 1], r[ix]
            p[ix], p[ix - 1] = p[ix - 1], p[ix]
            old = p[ix]
            self.items[old] = ix
            self.items[k] = ix - 1
            ix -= 1
tablite.datatypes.Rank.__iter__()
Source code in tablite/datatypes.py
752
753
def __iter__(self):
    return iter(self.items_list)

tablite.datatypes.MetaArray

Bases: ndarray

Array with metadata.

Functions
tablite.datatypes.MetaArray.__new__(array, dtype=None, order=None, **kwargs)
Source code in tablite/datatypes.py
808
809
810
811
def __new__(cls, array, dtype=None, order=None, **kwargs):
    obj = np.asarray(array, dtype=dtype, order=order).view(cls)
    obj.metadata = kwargs
    return obj
tablite.datatypes.MetaArray.__array_finalize__(obj)
Source code in tablite/datatypes.py
813
814
815
816
def __array_finalize__(self, obj):
    if obj is None:
        return
    self.metadata = getattr(obj, "metadata", None)

Functions

tablite.datatypes.numpy_to_python(obj: Any) -> Any

Converts numpy types to python types.

See https://numpy.org/doc/stable/reference/arrays.scalars.html

PARAMETER DESCRIPTION
obj

A numpy object

TYPE: Any

RETURNS DESCRIPTION
Any

python object: A python object

Source code in tablite/datatypes.py
699
700
701
702
703
704
705
706
707
708
709
710
711
712
def numpy_to_python(obj: Any) -> Any:
    """Converts numpy types to python types.

    See https://numpy.org/doc/stable/reference/arrays.scalars.html

    Args:
        obj (Any): A numpy object

    Returns:
        python object: A python object
    """
    if isinstance(obj, np.generic):
        return obj.item()
    return obj

tablite.datatypes.pytype(obj)

Returns the python type of any object

PARAMETER DESCRIPTION
obj

any numpy or python object

TYPE: Any

RETURNS DESCRIPTION
type

type of obj

Source code in tablite/datatypes.py
715
716
717
718
719
720
721
722
723
724
725
726
def pytype(obj):
    """Returns the python type of any object

    Args:
        obj (Any): any numpy or python object

    Returns:
        type: type of obj
    """
    if isinstance(obj, np.generic):
        return type(obj.item())
    return type(obj)

tablite.datatypes.pytype_from_iterable(iterable: {tuple, list}) -> {np.dtype, dict}

helper to make correct np array from python types.

PARAMETER DESCRIPTION
iterable

values to be converted to numpy array.

TYPE: (tuple, list)

RAISES DESCRIPTION
NotImplementedError

if datatype is not supported.

RETURNS DESCRIPTION
{dtype, dict}

np.dtype: python type of the iterable.

Source code in tablite/datatypes.py
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
def pytype_from_iterable(iterable: {tuple, list}) -> {np.dtype, dict}:
    """helper to make correct np array from python types.

    Args:
        iterable (tuple,list): values to be converted to numpy array.

    Raises:
        NotImplementedError: if datatype is not supported.

    Returns:
        np.dtype: python type of the iterable.
    """
    py_types = {}
    if isinstance(iterable, (tuple, list)):
        type_counter = Counter((pytype(v) for v in iterable))

        for k, v in type_counter.items():
            py_types[k] = v

        if len(py_types) == 0:
            np_dtype, py_dtype = object, bool
        elif len(py_types) == 1:
            py_dtype = list(py_types.keys())[0]
            if py_dtype == datetime:
                np_dtype = np.datetime64
            elif py_dtype == date:
                np_dtype = np.datetime64
            elif py_dtype == timedelta:
                np_dtype = np.timedelta64
            else:
                np_dtype = None
        else:
            np_dtype = object
    elif isinstance(iterable, np.ndarray):
        if iterable.dtype == object:
            np_dtype = object
            py_types = dict(Counter((pytype(v) for v in iterable)))
        else:
            np_dtype = iterable.dtype
            if len(iterable) > 0:
                py_types = {pytype(iterable[0]): len(iterable)}
            else:
                py_types = {pytype(np_dtype.type()): len(iterable)}
    else:
        raise NotImplementedError(f"No handler for {type(iterable)}")

    return np_dtype, py_types

tablite.datatypes.list_to_np_array(iterable)

helper to make correct np array from python types. Example of problem where numpy turns mixed types into strings.

np.array([4, '5']) np.ndarray(['4', '5'])

RETURNS DESCRIPTION

np.array

datatypes

Source code in tablite/datatypes.py
819
820
821
822
823
824
825
826
827
828
829
830
831
832
def list_to_np_array(iterable):
    """helper to make correct np array from python types.
    Example of problem where numpy turns mixed types into strings.
    >>> np.array([4, '5'])
    np.ndarray(['4', '5'])

    returns:
        np.array
        datatypes
    """
    np_dtype, py_dtype = pytype_from_iterable(iterable)

    value = MetaArray(iterable, dtype=np_dtype, py_dtype=py_dtype)
    return value

tablite.datatypes.np_type_unify(arrays)

unifies numpy types.

PARAMETER DESCRIPTION
arrays

List of numpy arrays

TYPE: list

RETURNS DESCRIPTION

np.ndarray: numpy array of a single type.

Source code in tablite/datatypes.py
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
def np_type_unify(arrays):
    """unifies numpy types.

    Args:
        arrays (list): List of numpy arrays

    Returns:
        np.ndarray: numpy array of a single type.
    """
    dtypes = {arr.dtype: len(arr) for arr in arrays}
    if len(dtypes) == 1:
        dtype, _ = dtypes.popitem()
    else:
        for ix, arr in enumerate(arrays):
            arrays[ix] = np.array(arr, dtype=object)
        dtype = object
    return np.concatenate(arrays, dtype=dtype)

tablite.datatypes.multitype_set(arr)

prevents loss of True, False when calling sets.

python looses values when called returning a set. Example:

{1, True, 0, False}

PARAMETER DESCRIPTION
arr

iterable of mixed types.

TYPE: Iterable

RETURNS DESCRIPTION

np.array: with unique values.

Source code in tablite/datatypes.py
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
def multitype_set(arr):
    """prevents loss of True, False when calling sets.

    python looses values when called returning a set. Example:
    >>> {1, True, 0, False}
    {0,1}

    Args:
        arr (Iterable): iterable of mixed types.

    Returns:
        np.array: with unique values.
    """
    L = [(type(v), v) for v in arr]
    L = list(set(L))
    L = [v for _, v in L]
    return np.array(L, dtype=object)