Skip to content

Commit 8eb512c

Browse files
authored
Merge pull request #2699 from activeloopai/fy_fix_data
Fix `.data()` for empty tensors
2 parents e4ab101 + 30ec665 commit 8eb512c

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

deeplake/api/tests/test_json.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,10 @@ def upload(stuff, ds):
245245
assert ds.list.data()["value"] == items
246246
assert ds.list[0].list() == items[0]
247247
assert ds.list.list() == items
248+
249+
250+
def test_empty_json(memory_ds):
251+
with memory_ds as ds:
252+
ds.create_tensor("json", htype="json")
253+
254+
assert ds.json.data()["value"] == []

deeplake/api/tests/test_text.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,10 @@ def test_text_tensor_append(memory_ds):
9595
for i in range(3):
9696
assert ds.x[i].data() == ds2.x[i].data()
9797
assert ds.y[i].data() == ds2.y[i].data()
98+
99+
100+
def test_empty_text(memory_ds):
101+
with memory_ds as ds:
102+
ds.create_tensor("text", htype="text")
103+
104+
assert ds.text.data()["value"] == []

deeplake/core/tensor.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,9 +1437,17 @@ def _extract_value(self, htype: str, fetch_chunks: bool = False):
14371437
raise Exception(f"Only supported for {htype} tensors.")
14381438

14391439
if self.ndim == 1:
1440-
return self.numpy(fetch_chunks=fetch_chunks)[0]
1440+
data = self.numpy(fetch_chunks=fetch_chunks)
1441+
if len(data) == 0:
1442+
return []
1443+
else:
1444+
return data[0]
14411445
else:
1442-
return [sample[0] for sample in self.numpy(aslist=True)]
1446+
data = self.numpy(aslist=True, fetch_chunks=fetch_chunks)
1447+
if len(data) == 0:
1448+
return []
1449+
else:
1450+
return [sample[0] for sample in data]
14431451

14441452
def text(self, fetch_chunks: bool = False):
14451453
"""Return text data. Only applicable for tensors with 'text' base htype."""

0 commit comments

Comments
 (0)