Skip to content

Commit

Permalink
Auto-convert numbers to strings when appending to a text dtype tensor (
Browse files Browse the repository at this point in the history
…#2782)

Auto-convert numbers to strings when appending to a text dtype tensor
  • Loading branch information
nvoxland-al committed Feb 27, 2024
1 parent aacf27a commit 9fa795d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions deeplake/core/chunk/base_chunk.py
@@ -1,3 +1,4 @@
import numbers
from abc import abstractmethod
import struct
import numpy as np
Expand Down Expand Up @@ -605,6 +606,9 @@ def is_empty_tensor(self):
)

def _text_sample_to_byte_string(self, sample):
if isinstance(sample, numbers.Number):
sample = str(sample)

try:
return str(sample.numpy().reshape(())).encode("utf-8")
except AttributeError:
Expand Down
19 changes: 19 additions & 0 deletions deeplake/core/chunk/tests/test_base_chunk.py
@@ -0,0 +1,19 @@
import pytest

from deeplake.core.chunk.uncompressed_chunk import UncompressedChunk
from deeplake.core.meta import TensorMeta


def test_text_sample_to_byte_string():
chunk = UncompressedChunk(
min_chunk_size=10,
max_chunk_size=1000,
tiling_threshold=1000,
tensor_meta=TensorMeta(),
)

assert chunk._text_sample_to_byte_string("test") == b"test"
assert chunk._text_sample_to_byte_string(3) == b"3"
assert chunk._text_sample_to_byte_string(3.5) == b"3.5"
assert chunk._text_sample_to_byte_string(None) == b""
assert chunk._text_sample_to_byte_string([]) == b""

0 comments on commit 9fa795d

Please sign in to comment.