GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — docarray/tests/units/array/test_array_from_to_pandas.py at main · docarray/docarray

This repository was archived by the owner on Sep 9, 2026. It is now read-only.

Expand file tree

/

test_array_from_to_pandas.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
162 lines (130 loc) · 4.45 KB
/

test_array_from_to_pandas.py

Copy path

File metadata and controls

162 lines (130 loc) · 4.45 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from typing import List, Optional
import pandas as pd
import pytest
from docarray import BaseDoc, DocList, DocVec
from docarray.documents import ImageDoc
from docarray.typing import NdArray, TorchTensor
@pytest.fixture()
def nested_doc_cls():
class MyDoc(BaseDoc):
count: Optional[int] = None
text: str
class MyDocNested(MyDoc):
image: ImageDoc
lst: List[str]
return MyDocNested
@pytest.mark.parametrize('doc_vec', [False, True])
def test_to_from_pandas_df(nested_doc_cls, doc_vec):
da = DocList[nested_doc_cls](
[
nested_doc_cls(
count=0,
text='hello',
image=ImageDoc(url='aux.png'),
lst=["hello", "world"],
),
nested_doc_cls(
text='hello world', image=ImageDoc(), lst=["hello", "world"]
),
]
)
if doc_vec:
da = da.to_doc_vec()
df = da.to_dataframe()
assert isinstance(df, pd.DataFrame)
assert len(df) == 2
assert (
df.columns
== [
'id',
'count',
'text',
'image__id',
'image__url',
'image__tensor',
'image__embedding',
'image__bytes_',
'lst',
]
).all()
if doc_vec:
da_from_df = DocVec[nested_doc_cls].from_dataframe(df)
assert isinstance(da_from_df, DocVec)
else:
da_from_df = DocList[nested_doc_cls].from_dataframe(df)
assert isinstance(da_from_df, DocList)
for doc1, doc2 in zip(da, da_from_df):
assert doc1 == doc2
@pytest.fixture()
def nested_doc():
class Inner(BaseDoc):
img: Optional[ImageDoc] = None
class Middle(BaseDoc):
img: Optional[ImageDoc] = None
inner: Optional[Inner] = None
class Outer(BaseDoc):
img: Optional[ImageDoc] = None
middle: Optional[Middle] = None
doc = Outer(
img=ImageDoc(), middle=Middle(img=ImageDoc(), inner=Inner(img=ImageDoc()))
)
return doc
@pytest.mark.parametrize('array_cls', [DocList, DocVec])
def test_from_pandas_without_schema_raise_exception(array_cls):
with pytest.raises(TypeError, match='no document schema defined'):
df = pd.DataFrame(
columns=['title', 'count'], data=[['title 0', 0], ['title 1', 1]]
)
array_cls.from_dataframe(df=df)
@pytest.mark.parametrize('array_cls', [DocList, DocVec])
def test_from_pandas_with_wrong_schema_raise_exception(nested_doc, array_cls):
with pytest.raises(ValueError, match='Column names do not match the schema'):
df = pd.DataFrame(
columns=['title', 'count'], data=[['title 0', 0], ['title 1', 1]]
)
array_cls[nested_doc.__class__].from_dataframe(df=df)
def test_doc_list_error():
class Book(BaseDoc):
title: str
# not testing DocVec bc it already fails here (as it should!)
docs = DocList([Book(title='hello'), Book(title='world')])
with pytest.raises(TypeError):
docs.to_dataframe()
@pytest.mark.proto
def test_union_type_error():
from typing import Union
from docarray.documents import TextDoc
class CustomDoc(BaseDoc):
ud: Union[TextDoc, ImageDoc] = TextDoc(text='union type')
docs = DocList[CustomDoc]([CustomDoc(ud=TextDoc(text='union type'))])
with pytest.raises(ValueError):
DocList[CustomDoc].from_dataframe(docs.to_dataframe())
class BasisUnion(BaseDoc):
ud: Union[int, str]
docs_basic = DocList[BasisUnion]([BasisUnion(ud="hello")])
docs_copy = DocList[BasisUnion].from_dataframe(docs_basic.to_dataframe())
assert docs_copy == docs_basic
@pytest.mark.parametrize('tensor_type', [NdArray, TorchTensor])
@pytest.mark.parametrize('tensor_len', [0, 5])
def test_from_to_pandas_tensor_type(tensor_type, tensor_len):
class MyDoc(BaseDoc):
embedding: tensor_type
text: str
image: ImageDoc
da = DocVec[MyDoc](
[
MyDoc(
embedding=list(range(tensor_len)),
text='hello',
image=ImageDoc(url='aux.png'),
),
MyDoc(
embedding=list(range(tensor_len)), text='hello world', image=ImageDoc()
),
],
tensor_type=tensor_type,
)
df_da = da.to_dataframe()
da2 = DocVec[MyDoc].from_dataframe(df_da, tensor_type=tensor_type)
assert da2.tensor_type == tensor_type
assert isinstance(da2.embedding, tensor_type)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.