Skip to content

Commit

Permalink
IPython fix html rendering for DataFrame (#2837)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Mar 6, 2022
1 parent 7e85a3d commit ee5c8f3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions py-polars/polars/internals/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,10 +1242,19 @@ def __getattr__(self, item: Any) -> "PySeries":
"""
Access columns as attribute.
"""
# it is important that we return an AttributeError here
# this is used by ipython to check some private
# `_ipython_canary_method_should_not_exist_`
# if we return any other error than AttributeError pretty printing
# will not work in notebooks.
# See: https://github.com/jupyter/notebook/issues/2014
if item.startswith("_"):
raise AttributeError(item)
try:
warnings.warn("accessing series as Attribute of a DataFrame is deprecated")
return pli.wrap_s(self._df.column(item))
except RuntimeError:
raise AttributeError(f"{item} not found")
except Exception:
raise AttributeError(item)

def __iter__(self) -> Iterator[Any]:
return self.get_columns().__iter__()
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -1732,7 +1732,7 @@ def test_getattr() -> None:
df = pl.DataFrame({"a": [1.0, 2.0]})
testing.assert_series_equal(df.a, pl.Series("a", [1.0, 2.0]))

with pytest.raises(pl.NotFoundError):
with pytest.raises(AttributeError):
_ = df.b


Expand Down

0 comments on commit ee5c8f3

Please sign in to comment.