Skip to content

Commit

Permalink
feat(python): additional autocomplete affordances for IPython users (
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie committed Nov 11, 2022
1 parent 1f642ee commit 57dd31c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions py-polars/polars/internals/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 +1462,9 @@ def __copy__(self: DF) -> DF:
def __deepcopy__(self: DF, memo: None = None) -> DF:
return self.clone()

def _ipython_key_completions_(self) -> list[str]:
return self.columns

def _repr_html_(self) -> str:
"""
Format output data in HTML for display in Jupyter Notebooks.
Expand Down
3 changes: 3 additions & 0 deletions py-polars/polars/internals/series/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def __getitem__(self, item: int | str) -> pli.Series:
else:
raise ValueError(f"expected type 'int | str', got {type(item)}")

def _ipython_key_completions_(self) -> list[str]:
return self.fields

def to_frame(self) -> pli.DataFrame:
"""Convert this Struct Series to a DataFrame."""
return pli.wrap_df(self._s.struct_to_frame())
Expand Down
1 change: 1 addition & 0 deletions py-polars/tests/unit/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ def test_dataframe_membership_operator() -> None:
df = pl.DataFrame({"name": ["Jane", "John"], "age": [20, 30]})
assert "name" in df
assert "phone" not in df
assert df._ipython_key_completions_() == ["name", "age"]


def test_sort() -> None:
Expand Down
1 change: 1 addition & 0 deletions py-polars/tests/unit/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def test_struct_various() -> None:
assert s.struct.field("int").to_list() == [1, 2]

assert df.to_struct("my_struct").struct.to_frame().frame_equal(df)
assert s.struct._ipython_key_completions_() == s.struct.fields


def test_struct_to_list() -> None:
Expand Down

0 comments on commit 57dd31c

Please sign in to comment.