Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docarray/array/doc_list/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,12 @@ def to_csv(
`'unix'` (for csv file generated on UNIX systems).

"""
if self.doc_type == AnyDoc:
raise TypeError(
'DocList must be homogeneous to be converted to a csv.'
'There is no document schema defined. '
'Please specify the DocList\'s Document type using `DocList[MyDoc]`.'
)
fields = self.doc_type._get_access_paths()

with open(file_path, 'w') as csv_file:
Expand Down Expand Up @@ -532,6 +538,13 @@ def to_dataframe(self) -> 'pd.DataFrame':
else:
pd = import_library('pandas', raise_error=True)

if self.doc_type == AnyDoc:
raise TypeError(
'DocList must be homogeneous to be converted to a DataFrame.'
'There is no document schema defined. '
'Please specify the DocList\'s Document type using `DocList[MyDoc]`.'
)

fields = self.doc_type._get_access_paths()
df = pd.DataFrame(columns=fields)

Expand Down
10 changes: 10 additions & 0 deletions tests/units/array/test_array_from_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,13 @@ class Book(BaseDoc):
books = DocList[Book].from_csv(file_path=remote_url)

assert len(books) == 3


def test_doc_list_error(tmpdir):
class Book(BaseDoc):
title: str

docs = DocList([Book(title='hello'), Book(title='world')])
tmp_file = str(tmpdir / 'tmp.csv')
with pytest.raises(TypeError):
docs.to_csv(tmp_file)
9 changes: 9 additions & 0 deletions tests/units/array/test_array_from_to_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,12 @@ def test_from_pandas_with_wrong_schema_raise_exception(nested_doc):
columns=['title', 'count'], data=[['title 0', 0], ['title 1', 1]]
)
DocList[nested_doc.__class__].from_dataframe(df=df)


def test_doc_list_error():
class Book(BaseDoc):
title: str

docs = DocList([Book(title='hello'), Book(title='world')])
with pytest.raises(TypeError):
docs.to_dataframe()