From a58d1a2413f60a642059939ddb3c4ad781001df3 Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 22 May 2021 20:58:37 -0700 Subject: [PATCH 1/2] REF: share _mpl_repr --- pandas/core/indexes/base.py | 9 +++++++-- pandas/core/indexes/datetimes.py | 5 ----- pandas/core/indexes/period.py | 7 ------- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index b9fd18dfdce73..3f8e427d6f260 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1164,9 +1164,14 @@ def _format_attrs(self): """ return format_object_attrs(self) - def _mpl_repr(self): + @final + def _mpl_repr(self) -> np.ndarray: # how to represent ourselves to matplotlib - return self.values + if isinstance(self.dtype, np.dtype) and self.dtype.kind != "M": + # Incompatible return value type (got "Union[ExtensionArray, ndarray]", + # expected "ndarray") + return self.values # type: ignore[return-value] + return self.astype(object, copy=False)._values def format( self, diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index ac09159c23566..2400e5e749605 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -25,7 +25,6 @@ ) from pandas._libs.tslibs import ( Resolution, - ints_to_pydatetime, parsing, timezones, to_offset, @@ -392,10 +391,6 @@ def _is_comparable_dtype(self, dtype: DtypeObj) -> bool: # -------------------------------------------------------------------- # Rendering Methods - def _mpl_repr(self) -> np.ndarray: - # how to represent ourselves to matplotlib - return ints_to_pydatetime(self.asi8, self.tz) - @property def _formatter_func(self): from pandas.io.formats.format import get_format_datetime64 diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 136843938b683..548032e5e3ee5 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -322,13 +322,6 @@ def _is_comparable_dtype(self, dtype: DtypeObj) -> bool: return False return dtype.freq == self.freq - # ------------------------------------------------------------------------ - # Rendering Methods - - def _mpl_repr(self) -> np.ndarray: - # how to represent ourselves to matplotlib - return self.astype(object)._values - # ------------------------------------------------------------------------ # Indexing From 000007d785b64ebabf336e12e7f2925c4599bed8 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 23 May 2021 10:35:35 -0700 Subject: [PATCH 2/2] ignore->cast --- pandas/core/indexes/base.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index c64c6787c28e1..bd711a2624753 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1171,9 +1171,7 @@ def _format_attrs(self): def _mpl_repr(self) -> np.ndarray: # how to represent ourselves to matplotlib if isinstance(self.dtype, np.dtype) and self.dtype.kind != "M": - # Incompatible return value type (got "Union[ExtensionArray, ndarray]", - # expected "ndarray") - return self.values # type: ignore[return-value] + return cast(np.ndarray, self.values) return self.astype(object, copy=False)._values def format(