Skip to content

Commit 92f5d1f

Browse files
REF: move values_for_json to EA (pandas-dev#53680) (#32)
* REF: move values_for_json to EA * docstring Co-authored-by: jbrockmendel <[email protected]>
1 parent d134414 commit 92f5d1f

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

pandas/core/arrays/base.py

+13
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,19 @@ def _reduce(
16781678
# Non-Optimized Default Methods; in the case of the private methods here,
16791679
# these are not guaranteed to be stable across pandas versions.
16801680

1681+
def _values_for_json(self) -> np.ndarray:
1682+
"""
1683+
Specify how to render our entries in to_json.
1684+
1685+
Notes
1686+
-----
1687+
The dtype on the returned ndarray is not restricted, but for non-native
1688+
types that are not specifically handled in objToJSON.c, to_json is
1689+
liable to raise. In these cases, it may be safer to return an ndarray
1690+
of strings.
1691+
"""
1692+
return np.asarray(self)
1693+
16811694
def _hash_pandas_object(
16821695
self, *, encoding: str, hash_key: str, categorize: bool
16831696
) -> npt.NDArray[np.uint64]:

pandas/core/arrays/datetimelike.py

+6
Original file line numberDiff line numberDiff line change
@@ -2202,6 +2202,12 @@ def _with_freq(self, freq) -> Self:
22022202
# --------------------------------------------------------------
22032203
# ExtensionArray Interface
22042204

2205+
def _values_for_json(self) -> np.ndarray:
2206+
# Small performance bump vs the base class which calls np.asarray(self)
2207+
if isinstance(self.dtype, np.dtype):
2208+
return self._ndarray
2209+
return super()._values_for_json()
2210+
22052211
def factorize(
22062212
self,
22072213
use_na_sentinel: bool = True,

pandas/core/internals/blocks.py

-16
Original file line numberDiff line numberDiff line change
@@ -1638,9 +1638,6 @@ def get_values(self, dtype: DtypeObj | None = None) -> np.ndarray:
16381638
"""
16391639
raise AbstractMethodError(self)
16401640

1641-
def values_for_json(self) -> np.ndarray:
1642-
raise AbstractMethodError(self)
1643-
16441641

16451642
class EABackedBlock(Block):
16461643
"""
@@ -1885,9 +1882,6 @@ def get_values(self, dtype: DtypeObj | None = None) -> np.ndarray:
18851882
# TODO(EA2D): reshape not needed with 2D EAs
18861883
return np.asarray(values).reshape(self.shape)
18871884

1888-
def values_for_json(self) -> np.ndarray:
1889-
return np.asarray(self.values)
1890-
18911885
@final
18921886
def pad_or_backfill(
18931887
self,
@@ -2174,9 +2168,6 @@ def get_values(self, dtype: DtypeObj | None = None) -> np.ndarray:
21742168
return self.values.astype(_dtype_obj)
21752169
return self.values
21762170

2177-
def values_for_json(self) -> np.ndarray:
2178-
return self.values
2179-
21802171
@cache_readonly
21812172
def is_numeric(self) -> bool: # type: ignore[override]
21822173
dtype = self.values.dtype
@@ -2231,9 +2222,6 @@ class DatetimeLikeBlock(NDArrayBackedExtensionBlock):
22312222
is_numeric = False
22322223
values: DatetimeArray | TimedeltaArray
22332224

2234-
def values_for_json(self) -> np.ndarray:
2235-
return self.values._ndarray
2236-
22372225

22382226
class DatetimeTZBlock(DatetimeLikeBlock):
22392227
"""implement a datetime64 block with a tz attribute"""
@@ -2242,10 +2230,6 @@ class DatetimeTZBlock(DatetimeLikeBlock):
22422230

22432231
__slots__ = ()
22442232

2245-
# Don't use values_for_json from DatetimeLikeBlock since it is
2246-
# an invalid optimization here(drop the tz)
2247-
values_for_json = NDArrayBackedExtensionBlock.values_for_json
2248-
22492233

22502234
# -----------------------------------------------------------------
22512235
# Constructor Helpers

pandas/core/internals/managers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ def column_arrays(self) -> list[np.ndarray]:
10081008

10091009
for blk in self.blocks:
10101010
mgr_locs = blk._mgr_locs
1011-
values = blk.values_for_json()
1011+
values = blk.array_values._values_for_json()
10121012
if values.ndim == 1:
10131013
# TODO(EA2D): special casing not needed with 2D EAs
10141014
result[mgr_locs[0]] = values

0 commit comments

Comments
 (0)