Skip to content

Commit 402c5cd

Browse files
authored
REF: avoid _internal_get_values in json (#32754)
1 parent 5081748 commit 402c5cd

File tree

5 files changed

+8
-96
lines changed

5 files changed

+8
-96
lines changed

pandas/_libs/src/ujson/python/objToJSON.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,19 @@ static PyObject *get_values(PyObject *obj) {
222222

223223
PRINTMARK();
224224

225-
if (PyObject_HasAttrString(obj, "_internal_get_values")) {
225+
if (PyObject_TypeCheck(obj, cls_index) || PyObject_TypeCheck(obj, cls_series)) {
226+
// The special cases to worry about are dt64tz and category[dt64tz].
227+
// In both cases we want the UTC-localized datetime64 ndarray,
228+
// without going through and object array of Timestamps.
226229
PRINTMARK();
227-
values = PyObject_CallMethod(obj, "_internal_get_values", NULL);
230+
values = PyObject_GetAttrString(obj, "values");
228231

229232
if (values == NULL) {
230233
// Clear so we can subsequently try another method
231234
PyErr_Clear();
235+
} else if (PyObject_HasAttrString(values, "__array__")) {
236+
// We may have gotten a Categorical or Sparse array so call np.array
237+
values = PyObject_CallMethod(values, "__array__", NULL);
232238
} else if (!PyArray_CheckExact(values)) {
233239
// Didn't get a numpy array, so keep trying
234240
PRINTMARK();

pandas/core/generic.py

-20
Original file line numberDiff line numberDiff line change
@@ -5382,26 +5382,6 @@ def _values(self) -> np.ndarray:
53825382
"""internal implementation"""
53835383
return self.values
53845384

5385-
def _internal_get_values(self) -> np.ndarray:
5386-
"""
5387-
Return an ndarray after converting sparse values to dense.
5388-
5389-
This is the same as ``.values`` for non-sparse data. For sparse
5390-
data contained in a `SparseArray`, the data are first
5391-
converted to a dense representation.
5392-
5393-
Returns
5394-
-------
5395-
numpy.ndarray
5396-
Numpy representation of DataFrame.
5397-
5398-
See Also
5399-
--------
5400-
values : Numpy representation of DataFrame.
5401-
SparseArray : Container for sparse data.
5402-
"""
5403-
return self.values
5404-
54055385
@property
54065386
def dtypes(self):
54075387
"""

pandas/core/indexes/base.py

-44
Original file line numberDiff line numberDiff line change
@@ -3872,50 +3872,6 @@ def _values(self) -> Union[ExtensionArray, np.ndarray]:
38723872
"""
38733873
return self._data
38743874

3875-
def _internal_get_values(self) -> np.ndarray:
3876-
"""
3877-
Return `Index` data as an `numpy.ndarray`.
3878-
3879-
Returns
3880-
-------
3881-
numpy.ndarray
3882-
A one-dimensional numpy array of the `Index` values.
3883-
3884-
See Also
3885-
--------
3886-
Index.values : The attribute that _internal_get_values wraps.
3887-
3888-
Examples
3889-
--------
3890-
Getting the `Index` values of a `DataFrame`:
3891-
3892-
>>> df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
3893-
... index=['a', 'b', 'c'], columns=['A', 'B', 'C'])
3894-
>>> df
3895-
A B C
3896-
a 1 2 3
3897-
b 4 5 6
3898-
c 7 8 9
3899-
>>> df.index._internal_get_values()
3900-
array(['a', 'b', 'c'], dtype=object)
3901-
3902-
Standalone `Index` values:
3903-
3904-
>>> idx = pd.Index(['1', '2', '3'])
3905-
>>> idx._internal_get_values()
3906-
array(['1', '2', '3'], dtype=object)
3907-
3908-
`MultiIndex` arrays also have only one dimension:
3909-
3910-
>>> midx = pd.MultiIndex.from_arrays([[1, 2, 3], ['a', 'b', 'c']],
3911-
... names=('number', 'letter'))
3912-
>>> midx._internal_get_values()
3913-
array([(1, 'a'), (2, 'b'), (3, 'c')], dtype=object)
3914-
>>> midx._internal_get_values().ndim
3915-
1
3916-
"""
3917-
return self.values
3918-
39193875
def _get_engine_target(self) -> np.ndarray:
39203876
"""
39213877
Get the ndarray that we can pass to the IndexEngine constructor.

pandas/core/internals/blocks.py

-18
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,6 @@ def get_block_values_for_json(self) -> np.ndarray:
237237
# TODO(2DEA): reshape will be unnecessary with 2D EAs
238238
return np.asarray(self.values).reshape(self.shape)
239239

240-
def to_dense(self):
241-
return self.values.view()
242-
243240
@property
244241
def fill_value(self):
245242
return np.nan
@@ -1820,9 +1817,6 @@ def get_values(self, dtype=None):
18201817
def array_values(self) -> ExtensionArray:
18211818
return self.values
18221819

1823-
def to_dense(self):
1824-
return np.asarray(self.values)
1825-
18261820
def to_native_types(self, slicer=None, na_rep="nan", quoting=None, **kwargs):
18271821
"""override to use ExtensionArray astype for the conversion"""
18281822
values = self.values
@@ -2378,12 +2372,6 @@ def get_values(self, dtype=None):
23782372
values = values.reshape(1, -1)
23792373
return values
23802374

2381-
def to_dense(self):
2382-
# we request M8[ns] dtype here, even though it discards tzinfo,
2383-
# as lots of code (e.g. anything using values_from_object)
2384-
# expects that behavior.
2385-
return np.asarray(self.values, dtype=_NS_DTYPE)
2386-
23872375
def _slice(self, slicer):
23882376
""" return a slice of my values """
23892377
if isinstance(slicer, tuple):
@@ -2911,12 +2899,6 @@ def __init__(self, values, placement, ndim=None):
29112899
def _holder(self):
29122900
return Categorical
29132901

2914-
def to_dense(self):
2915-
# Categorical.get_values returns a DatetimeIndex for datetime
2916-
# categories, so we can't simply use `np.asarray(self.values)` like
2917-
# other types.
2918-
return self.values._internal_get_values()
2919-
29202902
def to_native_types(self, slicer=None, na_rep="", quoting=None, **kwargs):
29212903
""" convert to our native types format, slicing if desired """
29222904
values = self.values

pandas/core/series.py

-12
Original file line numberDiff line numberDiff line change
@@ -574,18 +574,6 @@ def _values(self):
574574
def array(self) -> ExtensionArray:
575575
return self._data._block.array_values()
576576

577-
def _internal_get_values(self):
578-
"""
579-
Same as values (but handles sparseness conversions); is a view.
580-
581-
Returns
582-
-------
583-
numpy.ndarray
584-
Data of the Series.
585-
"""
586-
blk = self._data._block
587-
return np.array(blk.to_dense(), copy=False)
588-
589577
# ops
590578
def ravel(self, order="C"):
591579
"""

0 commit comments

Comments
 (0)