Skip to content

Commit

Permalink
Raise ImportError in to_pandas() and from_pandas() if pyarrow is not …
Browse files Browse the repository at this point in the history
…installed. (#3348)
  • Loading branch information
ghuls committed May 10, 2022
1 parent eb42b99 commit 2f3bb06
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
7 changes: 6 additions & 1 deletion py-polars/polars/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def from_pandas(
"""
Construct a Polars DataFrame or Series from a pandas DataFrame or Series.
Requires the pandas package to be installed.
This requires that pandas and pyarrow are installed.
Parameters
----------
Expand Down Expand Up @@ -297,6 +297,11 @@ def from_pandas(
]
"""
if not _PYARROW_AVAILABLE:
raise ImportError( # pragma: no cover
"'pyarrow' is required when using from_pandas()."
)

try:
import pandas as pd
except ImportError as e: # pragma: no cover
Expand Down
7 changes: 6 additions & 1 deletion py-polars/polars/internals/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,8 @@ def to_pandas(
self, *args: Any, date_as_object: bool = False, **kwargs: Any
) -> "pd.DataFrame": # noqa: F821
"""
Cast to a Pandas DataFrame. This requires that Pandas is installed.
Cast to a pandas DataFrame.
This requires that pandas and pyarrow are installed.
This operation clones data.
Parameters
Expand Down Expand Up @@ -1068,6 +1069,10 @@ def to_pandas(
<class 'pandas.core.frame.DataFrame'>
"""
if not _PYARROW_AVAILABLE:
raise ImportError( # pragma: no cover
"'pyarrow' is required when using to_pandas()."
)
record_batches = self._df.to_pandas()
tbl = pa.Table.from_batches(record_batches)
return tbl.to_pandas(*args, date_as_object=date_as_object, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/test_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def test_from_pandas_dataframe() -> None:
df = pl.from_pandas(pd_df)
assert df.shape == (2, 3)

# if not a Pandas dataframe, raise a ValueError
# if not a pandas dataframe, raise a ValueError
with pytest.raises(ValueError):
_ = pl.from_pandas([1, 2]) # type: ignore

Expand Down

0 comments on commit 2f3bb06

Please sign in to comment.