From 193747e9c3b5d09315e8848495e9e6f91437c733 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Fri, 9 Nov 2018 14:56:09 -0600 Subject: [PATCH] update docs, type --- pandas/core/arrays/base.py | 16 ++++++++++++---- pandas/core/arrays/integer.py | 14 ++++++++------ pandas/core/arrays/period.py | 4 ++-- pandas/tests/arrays/test_integer.py | 5 +++++ 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index d4a8da03913c3..be0b272f2816f 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -681,7 +681,7 @@ 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 @@ -689,9 +689,17 @@ def _formatter(self, formatter=None): 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 diff --git a/pandas/core/arrays/integer.py b/pandas/core/arrays/integer.py index 984ef4c8c673c..98eb9df98328f 100644 --- a/pandas/core/arrays/integer.py +++ b/pandas/core/arrays/integer.py @@ -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): diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 4f2848b3f5591..36a00db7625ce 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -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 diff --git a/pandas/tests/arrays/test_integer.py b/pandas/tests/arrays/test_integer.py index 6b99ede3436ce..94bf3dfd938a1 100644 --- a/pandas/tests/arrays/test_integer.py +++ b/pandas/tests/arrays/test_integer.py @@ -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):