Skip to content

Added extra info section to EA repr #24279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ExtensionArray(object):

* __repr__ : A default repr for the ExtensionArray.
* _formatter : Print scalars inside a Series or DataFrame.
* _repr_extra : Extra information to be included in the default repr.

Some methods require casting the ExtensionArray to an ndarray of Python
objects with ``self.astype(object)``, which may be expensive. When
Expand Down Expand Up @@ -699,17 +700,21 @@ def __repr__(self):
template = (
u'{class_name}'
u'{data}\n'
u'Length: {length}, dtype: {dtype}'
u'Length: {length}, dtype: {dtype}{extra}'
)
# the short repr has no trailing newline, while the truncated
# repr does. So we include a newline in our template, and strip
# any trailing newlines from format_object_summary
data = format_object_summary(self, self._formatter(),
indent_for_name=False).rstrip(', \n')
class_name = u'<{}>\n'.format(self.__class__.__name__)
extra = self._repr_extra()
if extra:
extra = ', {}'.format(extra)
return template.format(class_name=class_name, data=data,
length=len(self),
dtype=self.dtype)
dtype=self.dtype,
extra=extra)

def _formatter(self, boxed=False):
# type: (bool) -> Callable[[Any], Optional[str]]
Expand Down Expand Up @@ -739,6 +744,20 @@ def _formatter(self, boxed=False):
return str
return repr

def _repr_extra(self):
"""
Additional text to be placed into the default repr.

This text is placed after the ``dtype: <dtype>``, section
of the repr, and is separated by a comma and a space.

Returns
-------
str:
The additional text to be included in the repr.
"""
return ''

def _formatting_values(self):
# type: () -> np.ndarray
# At the moment, this has to be an array since we use result.dtype
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/extension/decimal/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ def _reduce(self, name, skipna=True, **kwargs):
"the {} operation".format(name))
return op(axis=0)

def _repr_extra(self):
return 'prec: {}'.format(self.dtype.context.prec)


def to_decimal(values, context=None):
return DecimalArray([decimal.Decimal(x) for x in values], context=context)
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/extension/decimal/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,14 @@ def _formatting_values(self):
with tm.assert_produces_warning(DeprecationWarning,
check_stacklevel=check_stacklevel):
repr(ser)


def test_repr():
arr = to_decimal(([1, 2, 3]))
result = repr(arr)
expected = (
"<DecimalArray>\n"
"[Decimal('1'), Decimal('2'), Decimal('3')]\n"
"Length: 3, dtype: decimal, prec: 28"
)
assert result == expected