Skip to content

Commit

Permalink
feat[python]: minor repr enhancement for Series (#4571)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie committed Aug 26, 2022
1 parent a77665a commit 73e7a8e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
.mypy_cache/
.pytest_cache/
.python-version
.yarn/
.vscode/
__pycache__/
AUTO_CHANGELOG.md
Expand Down
3 changes: 2 additions & 1 deletion py-polars/polars/internals/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ def __setstate__(self, state: Any) -> None:
self._s.__setstate__(state)

def __str__(self) -> str:
return self._s.as_str()
s_repr: str = self._s.as_str()
return s_repr.replace("Series", f"{self.__class__.__name__}", 1)

def __repr__(self) -> str:
return self.__str__()
Expand Down
23 changes: 23 additions & 0 deletions py-polars/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1902,3 +1902,26 @@ def test_set_at_idx() -> None:
# expected error condition
with pytest.raises(TypeError):
s.set_at_idx([0, 2], 0.12345)


def test_repr() -> None:
s = pl.Series("ints", [1001, 2002, 3003])
s_repr = repr(s)

assert "shape: (3,)" in s_repr
assert "Series: 'ints' [i64]" in s_repr
for n in s.to_list():
assert str(n) in s_repr

class XSeries(pl.Series):
"""Custom Series class"""

# check custom class name reflected in repr ouput
x = XSeries("ints", [1001, 2002, 3003])
x_repr = repr(x)

assert "shape: (3,)" in x_repr
assert "XSeries: 'ints' [i64]" in x_repr
assert "1001" in x_repr
for n in x.to_list():
assert str(n) in x_repr

0 comments on commit 73e7a8e

Please sign in to comment.