Skip to content

Commit ace62aa

Browse files
committed
Deprecate formatting_values
1 parent 0fdbfd3 commit ace62aa

File tree

10 files changed

+42
-26
lines changed

10 files changed

+42
-26
lines changed

doc/source/whatsnew/v0.24.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ update the ``ExtensionDtype._metadata`` tuple to match the signature of your
857857
- :meth:`DataFrame.stack` no longer converts to object dtype for DataFrames where each column has the same extension dtype. The output Series will have the same dtype as the columns (:issue:`23077`).
858858
- :meth:`Series.unstack` and :meth:`DataFrame.unstack` no longer convert extension arrays to object-dtype ndarrays. Each column in the output ``DataFrame`` will now have the same dtype as the input (:issue:`23077`).
859859
- Bug when grouping :meth:`Dataframe.groupby()` and aggregating on ``ExtensionArray`` it was not returning the actual ``ExtensionArray`` dtype (:issue:`23227`).
860-
- A default repr is now provided.
860+
- A default repr for ExtensionArrays is now provided (:issue:`23601`).
861861

862862
.. _whatsnew_0240.api.incompatibilities:
863863

@@ -967,6 +967,7 @@ Deprecations
967967
- The class ``FrozenNDArray`` has been deprecated. When unpickling, ``FrozenNDArray`` will be unpickled to ``np.ndarray`` once this class is removed (:issue:`9031`)
968968
- Deprecated the `nthreads` keyword of :func:`pandas.read_feather` in favor of
969969
`use_threads` to reflect the changes in pyarrow 0.11.0. (:issue:`23053`)
970+
- :meth:`ExtensionArray._formatting_values` is deprecated. Use `ExtensionArray._formatter` instead. (:issue:`23601`)
970971

971972
.. _whatsnew_0240.deprecations.datetimelike_int_ops:
972973

pandas/core/arrays/base.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -673,20 +673,25 @@ def __repr__(self):
673673
# the short repr has no trailing newline, while the truncated
674674
# repr does. So we include a newline in our template, and strip
675675
# any trailing newlines from format_object_summary
676-
data = format_object_summary(self, self._formatter, name=False,
676+
data = format_object_summary(self, self._formatter(), name=False,
677677
trailing_comma=False).rstrip('\n')
678678
name = self.__class__.__name__
679679
return template.format(class_name=name, data=data,
680680
length=len(self),
681681
dtype=self.dtype)
682682

683-
@property
684-
def _formatter(self):
685-
# type: () -> Callable[Any]
683+
def _formatter(self, boxed=False):
684+
# type: (bool) -> Callable[Any]
686685
"""Formatting function for scalar values.
687686
688687
This is used in the default '__repr__'. The formatting function
689688
receives instances of your scalar type.
689+
690+
Parameters
691+
----------
692+
boxed: bool, default False
693+
Whether the formatter is to be used by pandas inside a Series
694+
or DataFrame repr.
690695
"""
691696
return str
692697

pandas/core/arrays/categorical.py

-3
Original file line numberDiff line numberDiff line change
@@ -2344,9 +2344,6 @@ def _concat_same_type(self, to_concat):
23442344

23452345
return _concat_categorical(to_concat)
23462346

2347-
def _formatting_values(self):
2348-
return self
2349-
23502347
def isin(self, values):
23512348
"""
23522349
Check whether `values` are contained in Categorical.

pandas/core/arrays/integer.py

-4
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,6 @@ def __iter__(self):
297297
else:
298298
yield self._data[i]
299299

300-
def _formatting_values(self):
301-
# type: () -> np.ndarray
302-
return self._coerce_to_ndarray()
303-
304300
def take(self, indexer, allow_fill=False, fill_value=None):
305301
from pandas.api.extensions import take
306302

pandas/core/arrays/interval.py

-3
Original file line numberDiff line numberDiff line change
@@ -689,9 +689,6 @@ def copy(self, deep=False):
689689
# TODO: Could skip verify_integrity here.
690690
return type(self).from_arrays(left, right, closed=closed)
691691

692-
def _formatting_values(self):
693-
return np.asarray(self)
694-
695692
def isna(self):
696693
return isna(self.left)
697694

pandas/core/arrays/period.py

+5
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,11 @@ def start_time(self):
330330
def end_time(self):
331331
return self.to_timestamp(how='end')
332332

333+
def _formatter(self, boxed=False):
334+
if boxed:
335+
return str
336+
return "'{}'".format
337+
333338
def __setitem__(
334339
self,
335340
key, # type: Union[int, Sequence[int], Sequence[bool]]

pandas/core/internals/blocks.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
import pandas.core.missing as missing
6767
from pandas.core.base import PandasObject
6868

69-
from pandas.core.arrays import Categorical
69+
from pandas.core.arrays import Categorical, ExtensionArray
7070

7171
from pandas.core.indexes.datetimes import DatetimeIndex
7272
from pandas.core.indexes.timedeltas import TimedeltaIndex
@@ -1951,7 +1951,21 @@ def _slice(self, slicer):
19511951
return self.values[slicer]
19521952

19531953
def formatting_values(self):
1954-
return self.values._formatting_values()
1954+
# Deprecating the ability to override _formatting_values.
1955+
# Do the warning here, it's only user in pandas, since we
1956+
# have to check if the subclass overrode it.
1957+
fv = getattr(type(self.values), '_formatting_values', None)
1958+
if fv is not ExtensionArray._formatting_values:
1959+
msg = (
1960+
"'ExtensionArray._formatting_values' is deprecated. "
1961+
"Specify 'ExtensionArray._formatter' instead."
1962+
)
1963+
warnings.warn(msg, FutureWarning)
1964+
return self.values._formatting_values()
1965+
1966+
# the future implementation (and current, if not overrode)
1967+
formatter = self.values._formatter(boxed=True)
1968+
return np.array([formatter(x) for x in self.values], dtype=object)
19551969

19561970
def concat_same_type(self, to_concat, placement=None):
19571971
"""

pandas/io/formats/printing.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -341,19 +341,19 @@ def best_len(values):
341341
return 0
342342

343343
if trailing_comma:
344-
close = ', '
344+
close = u', '
345345
else:
346-
close = ''
346+
close = u''
347347

348348
if n == 0:
349-
summary = '[]{}'.format(close)
349+
summary = u'[]{}'.format(close)
350350
elif n == 1:
351351
first = formatter(obj[0])
352-
summary = '[{}]{}'.format(first, close)
352+
summary = u'[{}]{}'.format(first, close)
353353
elif n == 2:
354354
first = formatter(obj[0])
355355
last = formatter(obj[-1])
356-
summary = '[{}, {}]{}'.format(first, last, close)
356+
summary = u'[{}, {}]{}'.format(first, last, close)
357357
else:
358358

359359
if n > max_seq_items:

pandas/tests/arrays/test_period.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ def test_repr_small():
204204
arr = period_array(['2000', '2001'], freq='D')
205205
result = str(arr)
206206
expected = (
207-
'<PeriodArray>\n'
208-
'[2000-01-01, 2001-01-01]\n'
209-
'Length: 2, dtype: period[D]'
207+
"<PeriodArray>\n"
208+
"['2000-01-01', '2001-01-01']\n"
209+
"Length: 2, dtype: period[D]"
210210
)
211211
assert result == expected
212212

pandas/tests/extension/decimal/test_decimal.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ def test_value_counts(self, all_data, dropna):
188188

189189

190190
class TestCasting(BaseDecimal, base.BaseCastingTests):
191-
pass
191+
pytestmark = pytest.mark.skipif(compat.PY2,
192+
reason="Unhashble dtype in Py2.")
192193

193194

194195
class TestGroupby(BaseDecimal, base.BaseGroupbyTests):

0 commit comments

Comments
 (0)