Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REF: move values_for_json to EA #53680

Merged
merged 5 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,19 @@ def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
# 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 @@ -2198,6 +2198,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 @@ -1627,9 +1627,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 @@ -1877,9 +1874,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 @@ -2170,9 +2164,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 @@ -2237,9 +2228,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 @@ -2251,10 +2239,6 @@ class DatetimeTZBlock(DatetimeLikeBlock):
_validate_ndim = True
_can_consolidate = False

# 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