Skip to content

Commit

Permalink
Fixes issues related to gr.Dataframe receiving an empty dataframe (#…
Browse files Browse the repository at this point in the history
…7257)

* fixed

* tested and formatted

* add changeset

* add changeset

* fixes

* revert

* dataframe

* clean

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: pngwn <hello@pngwn.io>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
  • Loading branch information
4 people committed Feb 1, 2024
1 parent 7ea8336 commit daaaf95
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-ghosts-sneeze.md
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Fixes issues related to `gr.Dataframe` receiving an empty dataframe
33 changes: 23 additions & 10 deletions gradio/components/dataframe.py
Expand Up @@ -126,7 +126,6 @@ def __init__(
self.col_count = self.__process_counts(
col_count, len(headers) if headers else 3
)

self.__validate_headers(headers, self.col_count[0])

self.headers = (
Expand Down Expand Up @@ -199,13 +198,18 @@ def preprocess(
"""
if self.type == "pandas":
if payload.headers is not None:
return pd.DataFrame(payload.data, columns=payload.headers)
return pd.DataFrame(
[] if payload.data == [[]] else payload.data,
columns=payload.headers,
)
else:
return pd.DataFrame(payload.data)
if self.type == "polars":
polars = _import_polars()
if payload.headers is not None:
return polars.DataFrame(payload.data, schema=payload.headers)
return polars.DataFrame(
[] if payload.data == [[]] else payload.data, schema=payload.headers
)
else:
return polars.DataFrame(payload.data)
if self.type == "numpy":
Expand Down Expand Up @@ -240,12 +244,19 @@ def postprocess(
if value is None:
return self.postprocess(self.empty_input)
if isinstance(value, dict):
if len(value) == 0:
return DataframeData(headers=self.headers, data=[[]])
return DataframeData(
headers=value.get("headers", []), data=value.get("data", [[]])
)
if isinstance(value, (str, pd.DataFrame)):
if isinstance(value, str):
value = pd.read_csv(value) # type: ignore
if len(value) == 0:
return DataframeData(
headers=list(value.columns), # type: ignore
data=[[]], # type: ignore
)
return DataframeData(
headers=list(value.columns), # type: ignore
data=value.to_dict(orient="split")["data"], # type: ignore
Expand All @@ -262,25 +273,27 @@ def postprocess(
"Cannot display Styler object in interactive mode. Will display as a regular pandas dataframe instead."
)
df: pd.DataFrame = value.data # type: ignore
if len(df) == 0:
return DataframeData(
headers=list(df.columns),
data=[[]],
metadata=self.__extract_metadata(value), # type: ignore
)
return DataframeData(
headers=list(df.columns),
data=df.to_dict(orient="split")["data"], # type: ignore
metadata=self.__extract_metadata(value), # type: ignore
)
elif isinstance(value, (str, pd.DataFrame)):
df = pd.read_csv(value) if isinstance(value, str) else value # type: ignore
return DataframeData(
headers=list(df.columns),
data=df.to_dict(orient="split")["data"], # type: ignore
)
elif _is_polars_available() and isinstance(value, _import_polars().DataFrame):
if len(value) == 0:
return DataframeData(headers=list(value.to_dict().keys()), data=[[]])
df_dict = value.to_dict()
headers = list(df_dict.keys())
data = list(zip(*df_dict.values()))
return DataframeData(headers=headers, data=data)
elif isinstance(value, (np.ndarray, list)):
if len(value) == 0:
return self.postprocess([[]])
return DataframeData(headers=self.headers, data=[[]])
if isinstance(value, np.ndarray):
value = value.tolist()
if not isinstance(value, list):
Expand Down
2 changes: 1 addition & 1 deletion test/test_components.py
Expand Up @@ -1187,7 +1187,7 @@ def test_postprocess(self):
"""
dataframe_output = gr.Dataframe()
output = dataframe_output.postprocess([]).model_dump()
assert output == {"data": [[]], "headers": [], "metadata": None}
assert output == {"data": [[]], "headers": ["1", "2", "3"], "metadata": None}
output = dataframe_output.postprocess(np.zeros((2, 2))).model_dump()
assert output == {
"data": [[0, 0], [0, 0]],
Expand Down

0 comments on commit daaaf95

Please sign in to comment.