Skip to content

Commit 27db397

Browse files
committed
simplify formatter
1 parent fc4279d commit 27db397

File tree

5 files changed

+29
-41
lines changed

5 files changed

+29
-41
lines changed

pandas/core/arrays/base.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -675,37 +675,29 @@ def __repr__(self):
675675
length=len(self),
676676
dtype=self.dtype)
677677

678-
def _formatter(self, formatter=None):
679-
# type: (Optional[ExtensionArrayFormatter]) -> Callable[[Any], str]
678+
def _formatter(self, boxed=False):
679+
# type: (bool) -> Callable[[Any], str]
680680
"""Formatting function for scalar values.
681681
682-
This is used in the default '__repr__'. The formatting function
683-
receives instances of your scalar type.
682+
This is used in the default '__repr__'. The returned formatting
683+
function receives instances of your scalar type.
684684
685685
Parameters
686686
----------
687-
formatter: GenericArrayFormatter, optional
688-
The formatter this array is being rendered with. When the array
689-
is being rendered inside an Index, Series, or DataFrame, a
690-
formatter will be provided. So if you want your objects to
691-
render differently inside a Series from on its own, checking
692-
with ``formatter is None`` is an option.
693-
694-
The default behavior depends on whether `formatter` is passed.
695-
696-
* When `formatter` is None, :func:`repr` is returned.
697-
* When `formatter` is passed, ``formatter.formatter`` is used,
698-
which falls back to :func:`repr` if that isn't specified.
699-
700-
In general, just returning :func:`repr` should be fine.
687+
boxed: bool, default False
688+
An indicated for whether or not your array is being printed
689+
within a Series, DataFrame, or Index (True), or just by
690+
itself (False). This may be useful if you want scalar values
691+
to appear differently within a Series versus on its own (e.g.
692+
quoted or not).
701693
702694
Returns
703695
-------
704696
Callable[[Any], str]
705697
A callable that gets instances of the scalar type and
706-
returns a string.
698+
returns a string. By defult, :func:`repr` is used.
707699
"""
708-
return getattr(formatter, 'formatter', None) or repr
700+
return repr
709701

710702
def _formatting_values(self):
711703
# type: () -> np.ndarray

pandas/core/arrays/categorical.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ def _constructor(self):
499499
def _from_sequence(cls, scalars, dtype=None, copy=False):
500500
return Categorical(scalars, dtype=dtype)
501501

502-
def _formatter(self, formatter=None):
502+
def _formatter(self, boxed=False):
503503
# backwards compat with old printing.
504504
return None
505505

pandas/core/arrays/integer.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,12 @@ def _from_sequence(cls, scalars, dtype=None, copy=False):
264264
def _from_factorized(cls, values, original):
265265
return integer_array(values, dtype=original.dtype)
266266

267-
def _formatter(self, formatter=None):
268-
if formatter is None:
269-
def fmt(x):
270-
if isna(x):
271-
return 'NaN'
272-
return str(x)
273-
return fmt
274-
return formatter.formatter
267+
def _formatter(self, boxed=False):
268+
def fmt(x):
269+
if isna(x):
270+
return 'NaN'
271+
return str(x)
272+
return fmt
275273

276274
def __getitem__(self, item):
277275
if is_integer(item):

pandas/core/arrays/period.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,9 @@ def to_timestamp(self, freq=None, how='start'):
350350
# --------------------------------------------------------------------
351351
# Array-like / EA-Interface Methods
352352

353-
def _formatter(self, formatter=None):
354-
if formatter:
355-
return formatter.formatter or str
353+
def _formatter(self, boxed=False):
354+
if boxed:
355+
return str
356356
return "'{}'".format
357357

358358
def __setitem__(

pandas/core/arrays/sparse.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,15 +1738,13 @@ def __unicode__(self):
17381738
fill=printing.pprint_thing(self.fill_value),
17391739
index=printing.pprint_thing(self.sp_index))
17401740

1741-
def _formatter(self, formatter=None):
1742-
if formatter is None:
1743-
def fmt(x):
1744-
if isna(x) and isinstance(x, float):
1745-
return 'NaN'
1746-
return str(x)
1747-
1748-
return fmt
1749-
return formatter.formatter
1741+
def _formatter(self, boxed=False):
1742+
def fmt(x):
1743+
if isna(x) and isinstance(x, float):
1744+
return 'NaN'
1745+
return str(x)
1746+
1747+
return fmt
17501748

17511749

17521750
SparseArray._add_arithmetic_ops()

0 commit comments

Comments
 (0)