From 2ceb8df62a746d2503f4a650b56aeb505ceca78e Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 25 Jul 2023 09:53:20 -0700 Subject: [PATCH] REF: move values_for_json to EA (#53680) * REF: move values_for_json to EA * docstring --- pandas/core/arrays/base.py | 13 +++++++++++++ pandas/core/arrays/datetimelike.py | 6 ++++++ pandas/core/internals/blocks.py | 16 ---------------- pandas/core/internals/managers.py | 2 +- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 2e409463ecb81..5ca9716f5c8d6 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -1678,6 +1678,19 @@ def _reduce( # Non-Optimized Default Methods; in the case of the private methods here, # these are not guaranteed to be stable across pandas versions. + def _values_for_json(self) -> np.ndarray: + """ + Specify how to render our entries in to_json. + + Notes + ----- + The dtype on the returned ndarray is not restricted, but for non-native + types that are not specifically handled in objToJSON.c, to_json is + liable to raise. In these cases, it may be safer to return an ndarray + of strings. + """ + return np.asarray(self) + def _hash_pandas_object( self, *, encoding: str, hash_key: str, categorize: bool ) -> npt.NDArray[np.uint64]: diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index e11878dace88e..55eea72262170 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -2202,6 +2202,12 @@ def _with_freq(self, freq) -> Self: # -------------------------------------------------------------- # ExtensionArray Interface + def _values_for_json(self) -> np.ndarray: + # Small performance bump vs the base class which calls np.asarray(self) + if isinstance(self.dtype, np.dtype): + return self._ndarray + return super()._values_for_json() + def factorize( self, use_na_sentinel: bool = True, diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 2d102de879df0..64906d8792a8a 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -1638,9 +1638,6 @@ def get_values(self, dtype: DtypeObj | None = None) -> np.ndarray: """ raise AbstractMethodError(self) - def values_for_json(self) -> np.ndarray: - raise AbstractMethodError(self) - class EABackedBlock(Block): """ @@ -1885,9 +1882,6 @@ def get_values(self, dtype: DtypeObj | None = None) -> np.ndarray: # TODO(EA2D): reshape not needed with 2D EAs return np.asarray(values).reshape(self.shape) - def values_for_json(self) -> np.ndarray: - return np.asarray(self.values) - @final def pad_or_backfill( self, @@ -2174,9 +2168,6 @@ def get_values(self, dtype: DtypeObj | None = None) -> np.ndarray: return self.values.astype(_dtype_obj) return self.values - def values_for_json(self) -> np.ndarray: - return self.values - @cache_readonly def is_numeric(self) -> bool: # type: ignore[override] dtype = self.values.dtype @@ -2231,9 +2222,6 @@ class DatetimeLikeBlock(NDArrayBackedExtensionBlock): is_numeric = False values: DatetimeArray | TimedeltaArray - def values_for_json(self) -> np.ndarray: - return self.values._ndarray - class DatetimeTZBlock(DatetimeLikeBlock): """implement a datetime64 block with a tz attribute""" @@ -2242,10 +2230,6 @@ class DatetimeTZBlock(DatetimeLikeBlock): __slots__ = () - # Don't use values_for_json from DatetimeLikeBlock since it is - # an invalid optimization here(drop the tz) - values_for_json = NDArrayBackedExtensionBlock.values_for_json - # ----------------------------------------------------------------- # Constructor Helpers diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index c19300a8f3d9c..557137d5eb2f1 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -1008,7 +1008,7 @@ def column_arrays(self) -> list[np.ndarray]: for blk in self.blocks: mgr_locs = blk._mgr_locs - values = blk.values_for_json() + values = blk.array_values._values_for_json() if values.ndim == 1: # TODO(EA2D): special casing not needed with 2D EAs result[mgr_locs[0]] = values