|
16 | 16 | from pandas.compat import StringIO, lzip, map, u, zip
|
17 | 17 |
|
18 | 18 | from pandas.core.dtypes.common import (
|
19 |
| - is_categorical_dtype, is_datetime64_dtype, is_datetime64tz_dtype, is_float, |
20 |
| - is_float_dtype, is_integer, is_integer_dtype, is_interval_dtype, |
21 |
| - is_list_like, is_numeric_dtype, is_period_arraylike, is_scalar, |
| 19 | + is_categorical_dtype, is_datetime64_dtype, is_datetime64tz_dtype, |
| 20 | + is_extension_array_dtype, is_float, is_float_dtype, is_integer, |
| 21 | + is_integer_dtype, is_list_like, is_numeric_dtype, is_scalar, |
22 | 22 | is_timedelta64_dtype)
|
23 |
| -from pandas.core.dtypes.generic import ABCMultiIndex, ABCSparseArray |
| 23 | +from pandas.core.dtypes.generic import ( |
| 24 | + ABCIndexClass, ABCMultiIndex, ABCSeries, ABCSparseArray) |
24 | 25 | from pandas.core.dtypes.missing import isna, notna
|
25 | 26 |
|
26 | 27 | from pandas import compat
|
|
29 | 30 | from pandas.core.config import get_option, set_option
|
30 | 31 | from pandas.core.index import Index, ensure_index
|
31 | 32 | from pandas.core.indexes.datetimes import DatetimeIndex
|
32 |
| -from pandas.core.indexes.period import PeriodIndex |
33 | 33 |
|
34 | 34 | from pandas.io.common import _expand_user, _stringify_path
|
35 | 35 | from pandas.io.formats.printing import adjoin, justify, pprint_thing
|
@@ -842,22 +842,18 @@ def _get_column_name_list(self):
|
842 | 842 | def format_array(values, formatter, float_format=None, na_rep='NaN',
|
843 | 843 | digits=None, space=None, justify='right', decimal='.'):
|
844 | 844 |
|
845 |
| - if is_categorical_dtype(values): |
846 |
| - fmt_klass = CategoricalArrayFormatter |
847 |
| - elif is_interval_dtype(values): |
848 |
| - fmt_klass = IntervalArrayFormatter |
| 845 | + if is_datetime64_dtype(values.dtype): |
| 846 | + fmt_klass = Datetime64Formatter |
| 847 | + elif is_timedelta64_dtype(values.dtype): |
| 848 | + fmt_klass = Timedelta64Formatter |
| 849 | + elif is_extension_array_dtype(values.dtype): |
| 850 | + fmt_klass = ExtensionArrayFormatter |
849 | 851 | elif is_float_dtype(values.dtype):
|
850 | 852 | fmt_klass = FloatArrayFormatter
|
851 |
| - elif is_period_arraylike(values): |
852 |
| - fmt_klass = PeriodArrayFormatter |
853 | 853 | elif is_integer_dtype(values.dtype):
|
854 | 854 | fmt_klass = IntArrayFormatter
|
855 | 855 | elif is_datetime64tz_dtype(values):
|
856 | 856 | fmt_klass = Datetime64TZFormatter
|
857 |
| - elif is_datetime64_dtype(values.dtype): |
858 |
| - fmt_klass = Datetime64Formatter |
859 |
| - elif is_timedelta64_dtype(values.dtype): |
860 |
| - fmt_klass = Timedelta64Formatter |
861 | 857 | else:
|
862 | 858 | fmt_klass = GenericArrayFormatter
|
863 | 859 |
|
@@ -1121,39 +1117,22 @@ def _format_strings(self):
|
1121 | 1117 | return fmt_values.tolist()
|
1122 | 1118 |
|
1123 | 1119 |
|
1124 |
| -class IntervalArrayFormatter(GenericArrayFormatter): |
1125 |
| - |
1126 |
| - def __init__(self, values, *args, **kwargs): |
1127 |
| - GenericArrayFormatter.__init__(self, values, *args, **kwargs) |
1128 |
| - |
1129 |
| - def _format_strings(self): |
1130 |
| - formatter = self.formatter or str |
1131 |
| - fmt_values = np.array([formatter(x) for x in self.values]) |
1132 |
| - return fmt_values |
1133 |
| - |
1134 |
| - |
1135 |
| -class PeriodArrayFormatter(IntArrayFormatter): |
1136 |
| - |
| 1120 | +class ExtensionArrayFormatter(GenericArrayFormatter): |
1137 | 1121 | def _format_strings(self):
|
1138 |
| - from pandas.core.indexes.period import IncompatibleFrequency |
1139 |
| - try: |
1140 |
| - values = PeriodIndex(self.values).to_native_types() |
1141 |
| - except IncompatibleFrequency: |
1142 |
| - # periods may contains different freq |
1143 |
| - values = Index(self.values, dtype='object').to_native_types() |
1144 |
| - |
1145 |
| - formatter = self.formatter or (lambda x: '{x}'.format(x=x)) |
1146 |
| - fmt_values = [formatter(x) for x in values] |
1147 |
| - return fmt_values |
1148 |
| - |
| 1122 | + values = self.values |
| 1123 | + if isinstance(values, (ABCIndexClass, ABCSeries)): |
| 1124 | + values = values._values |
1149 | 1125 |
|
1150 |
| -class CategoricalArrayFormatter(GenericArrayFormatter): |
| 1126 | + formatter = values._formatter(boxed=True) |
1151 | 1127 |
|
1152 |
| - def __init__(self, values, *args, **kwargs): |
1153 |
| - GenericArrayFormatter.__init__(self, values, *args, **kwargs) |
| 1128 | + if is_categorical_dtype(values.dtype): |
| 1129 | + # Categorical is special for now, so that we can preserve tzinfo |
| 1130 | + array = values.get_values() |
| 1131 | + else: |
| 1132 | + array = np.asarray(values) |
1154 | 1133 |
|
1155 |
| - def _format_strings(self): |
1156 |
| - fmt_values = format_array(self.values.get_values(), self.formatter, |
| 1134 | + fmt_values = format_array(array, |
| 1135 | + formatter, |
1157 | 1136 | float_format=self.float_format,
|
1158 | 1137 | na_rep=self.na_rep, digits=self.digits,
|
1159 | 1138 | space=self.space, justify=self.justify)
|
|
0 commit comments