Skip to content

Commit 4661928

Browse files
authored
CLN: remove _ndarray_values (#32768)
1 parent cfbd7f6 commit 4661928

File tree

18 files changed

+37
-154
lines changed

18 files changed

+37
-154
lines changed

doc/source/development/internals.rst

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,10 @@ pandas extends NumPy's type system with custom types, like ``Categorical`` or
8989
datetimes with a timezone, so we have multiple notions of "values". For 1-D
9090
containers (``Index`` classes and ``Series``) we have the following convention:
9191

92-
* ``cls._ndarray_values`` is *always* a NumPy ``ndarray``. Ideally,
93-
``_ndarray_values`` is cheap to compute. For example, for a ``Categorical``,
94-
this returns the codes, not the array of objects.
9592
* ``cls._values`` refers is the "best possible" array. This could be an
96-
``ndarray``, ``ExtensionArray``, or in ``Index`` subclass (note: we're in the
97-
process of removing the index subclasses here so that it's always an
98-
``ndarray`` or ``ExtensionArray``).
93+
``ndarray`` or ``ExtensionArray``.
9994

100-
So, for example, ``Series[category]._values`` is a ``Categorical``, while
101-
``Series[category]._ndarray_values`` is the underlying codes.
95+
So, for example, ``Series[category]._values`` is a ``Categorical``.
10296

10397
.. _ref-subclassing-pandas:
10498

doc/source/reference/extensions.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ objects.
3737
api.extensions.ExtensionArray._from_factorized
3838
api.extensions.ExtensionArray._from_sequence
3939
api.extensions.ExtensionArray._from_sequence_of_strings
40-
api.extensions.ExtensionArray._ndarray_values
4140
api.extensions.ExtensionArray._reduce
4241
api.extensions.ExtensionArray._values_for_argsort
4342
api.extensions.ExtensionArray._values_for_factorize

pandas/core/arrays/base.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ class ExtensionArray:
9393
_from_factorized
9494
_from_sequence
9595
_from_sequence_of_strings
96-
_ndarray_values
9796
_reduce
9897
_values_for_argsort
9998
_values_for_factorize
@@ -1046,22 +1045,6 @@ def _concat_same_type(
10461045
# of objects
10471046
_can_hold_na = True
10481047

1049-
@property
1050-
def _ndarray_values(self) -> np.ndarray:
1051-
"""
1052-
Internal pandas method for lossy conversion to a NumPy ndarray.
1053-
1054-
This method is not part of the pandas interface.
1055-
1056-
The expectation is that this is cheap to compute, and is primarily
1057-
used for interacting with our indexers.
1058-
1059-
Returns
1060-
-------
1061-
array : ndarray
1062-
"""
1063-
return np.array(self)
1064-
10651048
def _reduce(self, name, skipna=True, **kwargs):
10661049
"""
10671050
Return a scalar result of performing the reduction operation.

pandas/core/arrays/categorical.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,6 @@ def dtype(self) -> CategoricalDtype:
451451
"""
452452
return self._dtype
453453

454-
@property
455-
def _ndarray_values(self) -> np.ndarray:
456-
return self.codes
457-
458454
@property
459455
def _constructor(self) -> Type["Categorical"]:
460456
return Categorical
@@ -2567,12 +2563,7 @@ def _get_codes_for_values(values, categories):
25672563
"""
25682564
dtype_equal = is_dtype_equal(values.dtype, categories.dtype)
25692565

2570-
if dtype_equal:
2571-
# To prevent erroneous dtype coercion in _get_data_algo, retrieve
2572-
# the underlying numpy array. gh-22702
2573-
values = getattr(values, "_ndarray_values", values)
2574-
categories = getattr(categories, "_ndarray_values", categories)
2575-
elif is_extension_array_dtype(categories.dtype) and is_object_dtype(values):
2566+
if is_extension_array_dtype(categories.dtype) and is_object_dtype(values):
25762567
# Support inferring the correct extension dtype from an array of
25772568
# scalar objects. e.g.
25782569
# Categorical(array[Period, Period], categories=PeriodIndex(...))
@@ -2582,7 +2573,7 @@ def _get_codes_for_values(values, categories):
25822573
# exception raised in _from_sequence
25832574
values = ensure_object(values)
25842575
categories = ensure_object(categories)
2585-
else:
2576+
elif not dtype_equal:
25862577
values = ensure_object(values)
25872578
categories = ensure_object(categories)
25882579

pandas/core/arrays/datetimelike.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,6 @@ def asi8(self) -> np.ndarray:
456456
# do not cache or you'll create a memory leak
457457
return self._data.view("i8")
458458

459-
@property
460-
def _ndarray_values(self):
461-
return self._data
462-
463459
# ----------------------------------------------------------------
464460
# Rendering Methods
465461

pandas/core/arrays/integer.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -478,18 +478,6 @@ def astype(self, dtype, copy: bool = True) -> ArrayLike:
478478
data = self.to_numpy(dtype=dtype, **kwargs)
479479
return astype_nansafe(data, dtype, copy=False)
480480

481-
@property
482-
def _ndarray_values(self) -> np.ndarray:
483-
"""
484-
Internal pandas method for lossy conversion to a NumPy ndarray.
485-
486-
This method is not part of the pandas interface.
487-
488-
The expectation is that this is cheap to compute, and is primarily
489-
used for interacting with our indexers.
490-
"""
491-
return self._data
492-
493481
def _values_for_factorize(self) -> Tuple[np.ndarray, float]:
494482
# TODO: https://github.com/pandas-dev/pandas/issues/30037
495483
# use masked algorithms, rather than object-dtype / np.nan.

pandas/core/base.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -855,23 +855,6 @@ def to_numpy(self, dtype=None, copy=False, na_value=lib.no_default, **kwargs):
855855
result[self.isna()] = na_value
856856
return result
857857

858-
@property
859-
def _ndarray_values(self) -> np.ndarray:
860-
"""
861-
The data as an ndarray, possibly losing information.
862-
863-
The expectation is that this is cheap to compute, and is primarily
864-
used for interacting with our indexers.
865-
866-
- categorical -> codes
867-
"""
868-
if is_extension_array_dtype(self):
869-
return self.array._ndarray_values
870-
# As a mixin, we depend on the mixing class having values.
871-
# Special mixin syntax may be developed in the future:
872-
# https://github.com/python/typing/issues/246
873-
return self.values # type: ignore
874-
875858
@property
876859
def empty(self):
877860
return not self.size

pandas/core/indexes/base.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,7 @@ def _simple_new(cls, values, name: Label = None):
464464
# _index_data is a (temporary?) fix to ensure that the direct data
465465
# manipulation we do in `_libs/reduction.pyx` continues to work.
466466
# We need access to the actual ndarray, since we're messing with
467-
# data buffers and strides. We don't re-use `_ndarray_values`, since
468-
# we actually set this value too.
467+
# data buffers and strides.
469468
result._index_data = values
470469
result._name = name
471470
result._cache = {}
@@ -625,7 +624,8 @@ def ravel(self, order="C"):
625624
--------
626625
numpy.ndarray.ravel
627626
"""
628-
return self._ndarray_values.ravel(order=order)
627+
values = self._get_engine_target()
628+
return values.ravel(order=order)
629629

630630
def view(self, cls=None):
631631

@@ -3846,29 +3846,24 @@ def _values(self) -> Union[ExtensionArray, np.ndarray]:
38463846
"""
38473847
The best array representation.
38483848
3849-
This is an ndarray or ExtensionArray. This differs from
3850-
``_ndarray_values``, which always returns an ndarray.
3849+
This is an ndarray or ExtensionArray.
38513850
3852-
Both ``_values`` and ``_ndarray_values`` are consistent between
3853-
``Series`` and ``Index`` (except for datetime64[ns], which returns
3854-
a DatetimeArray for _values on the Index, but ndarray[M8ns] on the
3855-
Series).
3851+
``_values`` are consistent between``Series`` and ``Index``.
38563852
38573853
It may differ from the public '.values' method.
38583854
3859-
index | values | _values | _ndarray_values |
3860-
----------------- | --------------- | ------------- | --------------- |
3861-
Index | ndarray | ndarray | ndarray |
3862-
CategoricalIndex | Categorical | Categorical | ndarray[int] |
3863-
DatetimeIndex | ndarray[M8ns] | DatetimeArray | ndarray[M8ns] |
3864-
DatetimeIndex[tz] | ndarray[M8ns] | DatetimeArray | ndarray[M8ns] |
3865-
PeriodIndex | ndarray[object] | PeriodArray | ndarray[int] |
3866-
IntervalIndex | IntervalArray | IntervalArray | ndarray[object] |
3855+
index | values | _values |
3856+
----------------- | --------------- | ------------- |
3857+
Index | ndarray | ndarray |
3858+
CategoricalIndex | Categorical | Categorical |
3859+
DatetimeIndex | ndarray[M8ns] | DatetimeArray |
3860+
DatetimeIndex[tz] | ndarray[M8ns] | DatetimeArray |
3861+
PeriodIndex | ndarray[object] | PeriodArray |
3862+
IntervalIndex | IntervalArray | IntervalArray |
38673863
38683864
See Also
38693865
--------
38703866
values
3871-
_ndarray_values
38723867
"""
38733868
return self._data
38743869

pandas/core/indexes/datetimelike.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def sort_values(self, return_indexer=False, ascending=True):
179179
sorted_index = self.take(_as)
180180
return sorted_index, _as
181181
else:
182-
# NB: using asi8 instead of _ndarray_values matters in numpy 1.18
182+
# NB: using asi8 instead of _data matters in numpy 1.18
183183
# because the treatment of NaT has been changed to put NaT last
184184
# instead of first.
185185
sorted_values = np.sort(self.asi8)

pandas/core/indexes/extension.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,6 @@ def __iter__(self):
228228
def __array__(self, dtype=None) -> np.ndarray:
229229
return np.asarray(self._data, dtype=dtype)
230230

231-
@property
232-
def _ndarray_values(self) -> np.ndarray:
233-
return self._data._ndarray_values
234-
235231
def _get_engine_target(self) -> np.ndarray:
236232
return self._data._values_for_argsort()
237233

0 commit comments

Comments
 (0)