diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 8fb88e625d948..bd711a2624753 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1167,9 +1167,12 @@ 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": + return cast(np.ndarray, self.values) + 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