Skip to content

Commit

Permalink
REF: move values_for_json to EA (pandas-dev#53680)
Browse files Browse the repository at this point in the history
* REF: move values_for_json to EA

* docstring
  • Loading branch information
jbrockmendel authored and jamie-harness committed Jul 26, 2023
1 parent d134414 commit 2ceb8df
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
13 changes: 13 additions & 0 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 0 additions & 16 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"""
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2ceb8df

Please sign in to comment.