Skip to content

Commit 77a0de8

Browse files
zbarryericmjl
authored andcommitted
[ENH] Datadesc repr (#352)
* add a repr to DataDesc * remove todo, fix docstring, move away from assertionerror
1 parent 3674346 commit 77a0de8

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

janitor/functions.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2764,8 +2764,6 @@ class DataDescription:
27642764
High-level description of data present in this DataFrame.
27652765
27662766
This is a custom data accessor.
2767-
2768-
TODO: add repr, fail on type error in desc
27692767
"""
27702768

27712769
def __init__(self, data):
@@ -2790,9 +2788,13 @@ def df(self) -> pd.DataFrame:
27902788
"""Get a table of descriptive information in a DataFrame format."""
27912789
return self._get_data_df()
27922790

2791+
def __repr__(self):
2792+
"""Human-readable representation of the `DataDescription` object."""
2793+
return str(self._get_data_df())
2794+
27932795
def display(self):
27942796
"""Print the table of descriptive information about this DataFrame."""
2795-
print(self._get_data_df())
2797+
print(self)
27962798

27972799
def set_description(self, desc: Union[List, Dict]):
27982800
"""
@@ -2801,7 +2803,13 @@ def set_description(self, desc: Union[List, Dict]):
28012803
:param desc: The structure containing the descriptions to update
28022804
"""
28032805
if isinstance(desc, list):
2804-
assert len(desc) == len(self._data.columns)
2806+
if len(desc) != len(self._data.columns):
2807+
raise ValueError(
2808+
f"Length of description list "
2809+
f"({len(desc)}) does not match number of columns in "
2810+
f"DataFrame ({len(self._data.columns)})"
2811+
)
2812+
28052813
self._desc = dict(zip(self._data.columns, desc))
28062814

28072815
elif isinstance(desc, dict):

tests/functions/test_data_description.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ def test_description_list(dataframe):
2121

2222
assert (df["description"] == desc).all()
2323

24-
with pytest.raises(AssertionError):
24+
with pytest.raises(ValueError):
2525
dataframe.data_description.set_description([])
2626

27-
with pytest.raises(AssertionError):
27+
with pytest.raises(ValueError):
2828
dataframe.data_description.set_description(desc[0:3])
2929

3030

0 commit comments

Comments
 (0)