Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger committed Nov 9, 2018
1 parent 4e0d91f commit 37638cc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 50 deletions.
6 changes: 3 additions & 3 deletions pandas/core/arrays/base.py
Expand Up @@ -680,8 +680,8 @@ def __repr__(self):
length=len(self),
dtype=self.dtype)

def _formatter(self, boxed=False):
# type: (bool) -> Callable[Any]
def _formatter(self, formatter):
# type: (ExtensionArrayFormatter) -> Callable[Any]
"""Formatting function for scalar values.
This is used in the default '__repr__'. The formatting function
Expand All @@ -693,7 +693,7 @@ def _formatter(self, boxed=False):
Whether the formatter is to be used by pandas inside a Series
or DataFrame repr.
"""
return str
return formatter.formatter or str

def _formatting_values(self):
# type: () -> np.ndarray
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/internals/blocks.py
Expand Up @@ -1963,9 +1963,7 @@ def formatting_values(self):
warnings.warn(msg, FutureWarning, stacklevel=10)
return self.values._formatting_values()

# the future implementation (and current, if not overrode)
formatter = self.values._formatter(boxed=True)
return np.array([formatter(x) for x in self.values], dtype=object)
return self.values

def concat_same_type(self, to_concat, placement=None):
"""
Expand Down
60 changes: 25 additions & 35 deletions pandas/io/formats/format.py
Expand Up @@ -16,11 +16,12 @@
from pandas.compat import StringIO, lzip, map, u, zip

from pandas.core.dtypes.common import (
is_categorical_dtype, is_datetime64_dtype, is_datetimetz, is_float,
is_float_dtype, is_integer, is_integer_dtype, is_interval_dtype,
is_list_like, is_numeric_dtype, is_period_arraylike, is_scalar,
is_timedelta64_dtype)
from pandas.core.dtypes.generic import ABCMultiIndex, ABCSparseArray
is_categorical_dtype, is_datetime64_dtype, is_datetimetz,
is_extension_array_dtype, is_float, is_float_dtype, is_integer,
is_integer_dtype, is_list_like, is_numeric_dtype, is_period_arraylike,
is_scalar, is_timedelta64_dtype)
from pandas.core.dtypes.generic import (
ABCIndex, ABCMultiIndex, ABCSeries, ABCSparseArray)
from pandas.core.dtypes.missing import isna, notna

from pandas import compat
Expand Down Expand Up @@ -849,22 +850,20 @@ def _get_column_name_list(self):
def format_array(values, formatter, float_format=None, na_rep='NaN',
digits=None, space=None, justify='right', decimal='.'):

if is_categorical_dtype(values):
fmt_klass = CategoricalArrayFormatter
elif is_interval_dtype(values):
fmt_klass = IntervalArrayFormatter
if is_period_arraylike(values):
fmt_klass = PeriodArrayFormatter
elif is_datetime64_dtype(values.dtype):
fmt_klass = Datetime64Formatter
elif is_timedelta64_dtype(values.dtype):
fmt_klass = Timedelta64Formatter
elif is_extension_array_dtype(values.dtype):
fmt_klass = ExtensionArrayFormatter
elif is_float_dtype(values.dtype):
fmt_klass = FloatArrayFormatter
elif is_period_arraylike(values):
fmt_klass = PeriodArrayFormatter
elif is_integer_dtype(values.dtype):
fmt_klass = IntArrayFormatter
elif is_datetimetz(values):
fmt_klass = Datetime64TZFormatter
elif is_datetime64_dtype(values.dtype):
fmt_klass = Datetime64Formatter
elif is_timedelta64_dtype(values.dtype):
fmt_klass = Timedelta64Formatter
else:
fmt_klass = GenericArrayFormatter

Expand Down Expand Up @@ -1126,14 +1125,18 @@ def _format_strings(self):
return fmt_values.tolist()


class IntervalArrayFormatter(GenericArrayFormatter):

def __init__(self, values, *args, **kwargs):
GenericArrayFormatter.__init__(self, values, *args, **kwargs)

class ExtensionArrayFormatter(GenericArrayFormatter):
def _format_strings(self):
formatter = self.formatter or str
fmt_values = np.array([formatter(x) for x in self.values])
values = self.values
if isinstance(values, (ABCIndex, ABCSeries)):
values = values._values

formatter = self.values._formatter(self)
fmt_values = format_array(np.asarray(self.values),
formatter,
float_format=self.float_format,
na_rep=self.na_rep, digits=self.digits,
space=self.space, justify=self.justify)
return fmt_values


Expand All @@ -1152,19 +1155,6 @@ def _format_strings(self):
return fmt_values


class CategoricalArrayFormatter(GenericArrayFormatter):

def __init__(self, values, *args, **kwargs):
GenericArrayFormatter.__init__(self, values, *args, **kwargs)

def _format_strings(self):
fmt_values = format_array(self.values.get_values(), self.formatter,
float_format=self.float_format,
na_rep=self.na_rep, digits=self.digits,
space=self.space, justify=self.justify)
return fmt_values


def format_percentiles(percentiles):
"""
Outputs rounded and formatted percentiles.
Expand Down
20 changes: 11 additions & 9 deletions pandas/tests/arrays/test_period.py
Expand Up @@ -215,14 +215,16 @@ def test_repr_large():
arr = period_array(['2000', '2001'] * 500, freq='D')
result = str(arr)
expected = (
'<PeriodArray>\n'
'[2000-01-01, 2001-01-01, 2000-01-01, 2001-01-01, 2000-01-01, '
'2001-01-01,\n' # continuation
' 2000-01-01, 2001-01-01, 2000-01-01, 2001-01-01,\n'
' ...\n'
' 2000-01-01, 2001-01-01, 2000-01-01, 2001-01-01, 2000-01-01, '
'2001-01-01,\n' # continuation
' 2000-01-01, 2001-01-01, 2000-01-01, 2001-01-01]\n'
'Length: 1000, dtype: period[D]'
"<PeriodArray>\n"
"['2000-01-01', '2001-01-01', '2000-01-01', '2001-01-01', "
"'2000-01-01',\n"
" '2001-01-01', '2000-01-01', '2001-01-01', '2000-01-01', "
"'2001-01-01',\n"
" ...\n"
" '2000-01-01', '2001-01-01', '2000-01-01', '2001-01-01', "
"'2000-01-01',\n"
" '2001-01-01', '2000-01-01', '2001-01-01', '2000-01-01', "
"'2001-01-01']\n"
"Length: 1000, dtype: period[D]"
)
assert result == expected

0 comments on commit 37638cc

Please sign in to comment.