Skip to content

Commit

Permalink
update docs, type
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger committed Nov 9, 2018
1 parent 6e64b7b commit 193747e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
16 changes: 12 additions & 4 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,17 +681,25 @@ def __repr__(self):
dtype=self.dtype)

def _formatter(self, formatter=None):
# type: (ExtensionArrayFormatter) -> Callable[Any]
# type: (Optional[ExtensionArrayFormatter]) -> Callable[[Any], str]
"""Formatting function for scalar values.
This is used in the default '__repr__'. The formatting function
receives instances of your scalar type.
Parameters
----------
boxed: bool, default False
Whether the formatter is to be used by pandas inside a Series
or DataFrame repr.
formatter: GenericArrayFormatter, optional
The formatter this array is being rendered with. The formatter
may have a `.formatter` method already defined. By default, this
will be used if a `formatter` is passed, otherwise the formatter
is ``str``.
Returns
-------
Callable[[Any], str]
A callable that gets instances of the scalar type and
returns a string.
"""
return getattr(formatter, 'formatter', None) or str

Expand Down
14 changes: 8 additions & 6 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,14 @@ def _from_sequence(cls, scalars, dtype=None, copy=False):
def _from_factorized(cls, values, original):
return integer_array(values, dtype=original.dtype)

def _formatter(self, boxed=False):
def fmt(x):
if isna(x):
return 'NaN'
return str(x)
return fmt
def _formatter(self, formatter=None):
if formatter is None:
def fmt(x):
if isna(x):
return 'NaN'
return str(x)
return fmt
return formatter.formatter

def __getitem__(self, item):
if is_integer(item):
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ def start_time(self):
def end_time(self):
return self.to_timestamp(how='end')

def _formatter(self, boxed=False):
if boxed:
def _formatter(self, formatter=None):
if formatter:
return str
return "'{}'".format

Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/arrays/test_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ def test_repr_array(data):
assert 'dtype: ' in result


def test_na_repr(data):
result = repr(integer_array([1, None]))
assert 'NaN' in result


def test_repr_array_long(data):
# some arrays may be able to assert a ... in the repr
with pd.option_context('display.max_seq_items', 1):
Expand Down

0 comments on commit 193747e

Please sign in to comment.