Skip to content

Commit 3713834

Browse files
authored
REF: rename PandasArray->NumpyExtensionArray (#54101)
* REF: rename PandasArray->NumpyExtensionArray * document PandasDtype->NumpyEADtype
1 parent f77a0e6 commit 3713834

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+293
-252
lines changed

ci/code_checks.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
105105
pandas.api.indexers.VariableOffsetWindowIndexer \
106106
pandas.api.extensions.ExtensionDtype \
107107
pandas.api.extensions.ExtensionArray \
108-
pandas.arrays.PandasArray \
108+
pandas.arrays.NumpyExtensionArray \
109109
pandas.api.extensions.ExtensionArray._accumulate \
110110
pandas.api.extensions.ExtensionArray._concat_same_type \
111111
pandas.api.extensions.ExtensionArray._formatter \

doc/source/development/contributing_codebase.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ be located.
475475

476476
8) Is your test for one of the pandas-provided ExtensionArrays (``Categorical``,
477477
``DatetimeArray``, ``TimedeltaArray``, ``PeriodArray``, ``IntervalArray``,
478-
``PandasArray``, ``FloatArray``, ``BoolArray``, ``StringArray``)?
478+
``NumpyExtensionArray``, ``FloatArray``, ``BoolArray``, ``StringArray``)?
479479
This test likely belongs in one of:
480480

481481
- tests.arrays

doc/source/reference/extensions.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ objects.
2424
:template: autosummary/class_without_autosummary.rst
2525

2626
api.extensions.ExtensionArray
27-
arrays.PandasArray
27+
arrays.NumpyExtensionArray
2828

2929
.. We need this autosummary so that methods and attributes are generated.
3030
.. Separate block, since they aren't classes.

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ See :ref:`install.dependencies` and :ref:`install.optional_dependencies` for mor
286286

287287
Other API changes
288288
^^^^^^^^^^^^^^^^^
289+
- :class:`arrays.PandasArray` has been renamed ``NumpyExtensionArray`` and the attached dtype name changed from ``PandasDtype`` to ``NumpyEADtype``; importing ``PandasArray`` still works until the next major version (:issue:`53694`)
289290
-
290291

291292
.. ---------------------------------------------------------------------------

pandas/_testing/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
from pandas.core.arrays import (
103103
BaseMaskedArray,
104104
ExtensionArray,
105-
PandasArray,
105+
NumpyExtensionArray,
106106
)
107107
from pandas.core.arrays._mixins import NDArrayBackedExtensionArray
108108
from pandas.core.construction import extract_array
@@ -307,7 +307,7 @@ def box_expected(expected, box_cls, transpose: bool = True):
307307
if box_cls is pd.array:
308308
if isinstance(expected, RangeIndex):
309309
# pd.array would return an IntegerArray
310-
expected = PandasArray(np.asarray(expected._values))
310+
expected = NumpyExtensionArray(np.asarray(expected._values))
311311
else:
312312
expected = pd.array(expected, copy=False)
313313
elif box_cls is Index:

pandas/_testing/asserters.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
CategoricalDtype,
2626
DatetimeTZDtype,
2727
ExtensionDtype,
28-
PandasDtype,
28+
NumpyEADtype,
2929
)
3030
from pandas.core.dtypes.missing import array_equivalent
3131

@@ -577,12 +577,12 @@ def raise_assert_detail(
577577

578578
if isinstance(left, np.ndarray):
579579
left = pprint_thing(left)
580-
elif isinstance(left, (CategoricalDtype, PandasDtype, StringDtype)):
580+
elif isinstance(left, (CategoricalDtype, NumpyEADtype, StringDtype)):
581581
left = repr(left)
582582

583583
if isinstance(right, np.ndarray):
584584
right = pprint_thing(right)
585-
elif isinstance(right, (CategoricalDtype, PandasDtype, StringDtype)):
585+
elif isinstance(right, (CategoricalDtype, NumpyEADtype, StringDtype)):
586586
right = repr(right)
587587

588588
msg += f"""

pandas/arrays/__init__.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
FloatingArray,
1313
IntegerArray,
1414
IntervalArray,
15-
PandasArray,
15+
NumpyExtensionArray,
1616
PeriodArray,
1717
SparseArray,
1818
StringArray,
@@ -28,9 +28,26 @@
2828
"FloatingArray",
2929
"IntegerArray",
3030
"IntervalArray",
31-
"PandasArray",
31+
"NumpyExtensionArray",
3232
"PeriodArray",
3333
"SparseArray",
3434
"StringArray",
3535
"TimedeltaArray",
3636
]
37+
38+
39+
def __getattr__(name: str):
40+
if name == "PandasArray":
41+
# GH#53694
42+
import warnings
43+
44+
from pandas.util._exceptions import find_stack_level
45+
46+
warnings.warn(
47+
"PandasArray has been renamed NumpyExtensionArray. Use that "
48+
"instead. This alias will be removed in a future version.",
49+
FutureWarning,
50+
stacklevel=find_stack_level(),
51+
)
52+
return NumpyExtensionArray
53+
raise AttributeError(f"module 'pandas.arrays' has no attribute '{name}'")

pandas/core/algorithms.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
BaseMaskedDtype,
5959
CategoricalDtype,
6060
ExtensionDtype,
61-
PandasDtype,
61+
NumpyEADtype,
6262
)
6363
from pandas.core.dtypes.generic import (
6464
ABCDatetimeArray,
@@ -1439,8 +1439,8 @@ def diff(arr, n: int, axis: AxisInt = 0):
14391439
else:
14401440
op = operator.sub
14411441

1442-
if isinstance(dtype, PandasDtype):
1443-
# PandasArray cannot necessarily hold shifted versions of itself.
1442+
if isinstance(dtype, NumpyEADtype):
1443+
# NumpyExtensionArray cannot necessarily hold shifted versions of itself.
14441444
arr = arr.to_numpy()
14451445
dtype = arr.dtype
14461446

pandas/core/arrays/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pandas.core.arrays.integer import IntegerArray
1212
from pandas.core.arrays.interval import IntervalArray
1313
from pandas.core.arrays.masked import BaseMaskedArray
14-
from pandas.core.arrays.numpy_ import PandasArray
14+
from pandas.core.arrays.numpy_ import NumpyExtensionArray
1515
from pandas.core.arrays.period import (
1616
PeriodArray,
1717
period_array,
@@ -34,7 +34,7 @@
3434
"FloatingArray",
3535
"IntegerArray",
3636
"IntervalArray",
37-
"PandasArray",
37+
"NumpyExtensionArray",
3838
"PeriodArray",
3939
"period_array",
4040
"SparseArray",

pandas/core/arrays/_mixins.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ def fillna(self, value=None, method=None, limit: int | None = None) -> Self:
317317
func(npvalues, limit=limit, mask=mask.T)
318318
npvalues = npvalues.T
319319

320-
# TODO: PandasArray didn't used to copy, need tests for this
320+
# TODO: NumpyExtensionArray didn't used to copy, need tests
321+
# for this
321322
new_values = self._from_backing_data(npvalues)
322323
else:
323324
# fill with value

pandas/core/arrays/categorical.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2645,18 +2645,18 @@ def _str_map(
26452645
# Optimization to apply the callable `f` to the categories once
26462646
# and rebuild the result by `take`ing from the result with the codes.
26472647
# Returns the same type as the object-dtype implementation though.
2648-
from pandas.core.arrays import PandasArray
2648+
from pandas.core.arrays import NumpyExtensionArray
26492649

26502650
categories = self.categories
26512651
codes = self.codes
2652-
result = PandasArray(categories.to_numpy())._str_map(f, na_value, dtype)
2652+
result = NumpyExtensionArray(categories.to_numpy())._str_map(f, na_value, dtype)
26532653
return take_nd(result, codes, fill_value=na_value)
26542654

26552655
def _str_get_dummies(self, sep: str = "|"):
26562656
# sep may not be in categories. Just bail on this.
2657-
from pandas.core.arrays import PandasArray
2657+
from pandas.core.arrays import NumpyExtensionArray
26582658

2659-
return PandasArray(self.astype(str))._str_get_dummies(sep)
2659+
return NumpyExtensionArray(self.astype(str))._str_get_dummies(sep)
26602660

26612661
# ------------------------------------------------------------------------
26622662
# GroupBy Methods

pandas/core/arrays/datetimelike.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,8 @@ def _validate_listlike(self, value, allow_object: bool = False):
652652
msg = self._validation_error_message(value, True)
653653
raise TypeError(msg)
654654

655-
# Do type inference if necessary up front (after unpacking PandasArray)
655+
# Do type inference if necessary up front (after unpacking
656+
# NumpyExtensionArray)
656657
# e.g. we passed PeriodIndex.values and got an ndarray of Periods
657658
value = extract_array(value, extract_numpy=True)
658659
value = pd_array(value)

pandas/core/arrays/numpy_.py

+24-20
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from pandas.core.dtypes.astype import astype_array
1818
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
1919
from pandas.core.dtypes.common import pandas_dtype
20-
from pandas.core.dtypes.dtypes import PandasDtype
20+
from pandas.core.dtypes.dtypes import NumpyEADtype
2121
from pandas.core.dtypes.missing import isna
2222

2323
from pandas.core import (
@@ -48,7 +48,7 @@
4848

4949
# error: Definition of "_concat_same_type" in base class "NDArrayBacked" is
5050
# incompatible with definition in base class "ExtensionArray"
51-
class PandasArray( # type: ignore[misc]
51+
class NumpyExtensionArray( # type: ignore[misc]
5252
OpsMixin,
5353
NDArrayBackedExtensionArray,
5454
ObjectStringArrayMixin,
@@ -76,19 +76,21 @@ class PandasArray( # type: ignore[misc]
7676
"""
7777

7878
# If you're wondering why pd.Series(cls) doesn't put the array in an
79-
# ExtensionBlock, search for `ABCPandasArray`. We check for
79+
# ExtensionBlock, search for `ABCNumpyExtensionArray`. We check for
8080
# that _typ to ensure that users don't unnecessarily use EAs inside
8181
# pandas internals, which turns off things like block consolidation.
8282
_typ = "npy_extension"
8383
__array_priority__ = 1000
8484
_ndarray: np.ndarray
85-
_dtype: PandasDtype
85+
_dtype: NumpyEADtype
8686
_internal_fill_value = np.nan
8787

8888
# ------------------------------------------------------------------------
8989
# Constructors
9090

91-
def __init__(self, values: np.ndarray | PandasArray, copy: bool = False) -> None:
91+
def __init__(
92+
self, values: np.ndarray | NumpyExtensionArray, copy: bool = False
93+
) -> None:
9294
if isinstance(values, type(self)):
9395
values = values._ndarray
9496
if not isinstance(values, np.ndarray):
@@ -98,19 +100,19 @@ def __init__(self, values: np.ndarray | PandasArray, copy: bool = False) -> None
98100

99101
if values.ndim == 0:
100102
# Technically we support 2, but do not advertise that fact.
101-
raise ValueError("PandasArray must be 1-dimensional.")
103+
raise ValueError("NumpyExtensionArray must be 1-dimensional.")
102104

103105
if copy:
104106
values = values.copy()
105107

106-
dtype = PandasDtype(values.dtype)
108+
dtype = NumpyEADtype(values.dtype)
107109
super().__init__(values, dtype)
108110

109111
@classmethod
110112
def _from_sequence(
111113
cls, scalars, *, dtype: Dtype | None = None, copy: bool = False
112-
) -> PandasArray:
113-
if isinstance(dtype, PandasDtype):
114+
) -> NumpyExtensionArray:
115+
if isinstance(dtype, NumpyEADtype):
114116
dtype = dtype._dtype
115117

116118
# error: Argument "dtype" to "asarray" has incompatible type
@@ -131,14 +133,14 @@ def _from_sequence(
131133
result = result.copy()
132134
return cls(result)
133135

134-
def _from_backing_data(self, arr: np.ndarray) -> PandasArray:
136+
def _from_backing_data(self, arr: np.ndarray) -> NumpyExtensionArray:
135137
return type(self)(arr)
136138

137139
# ------------------------------------------------------------------------
138140
# Data
139141

140142
@property
141-
def dtype(self) -> PandasDtype:
143+
def dtype(self) -> NumpyEADtype:
142144
return self._dtype
143145

144146
# ------------------------------------------------------------------------
@@ -151,7 +153,7 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
151153
# Lightly modified version of
152154
# https://numpy.org/doc/stable/reference/generated/numpy.lib.mixins.NDArrayOperatorsMixin.html
153155
# The primary modification is not boxing scalar return values
154-
# in PandasArray, since pandas' ExtensionArrays are 1-d.
156+
# in NumpyExtensionArray, since pandas' ExtensionArrays are 1-d.
155157
out = kwargs.get("out", ())
156158

157159
result = arraylike.maybe_dispatch_ufunc_to_dunder_op(
@@ -175,10 +177,12 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
175177
return result
176178

177179
# Defer to the implementation of the ufunc on unwrapped values.
178-
inputs = tuple(x._ndarray if isinstance(x, PandasArray) else x for x in inputs)
180+
inputs = tuple(
181+
x._ndarray if isinstance(x, NumpyExtensionArray) else x for x in inputs
182+
)
179183
if out:
180184
kwargs["out"] = tuple(
181-
x._ndarray if isinstance(x, PandasArray) else x for x in out
185+
x._ndarray if isinstance(x, NumpyExtensionArray) else x for x in out
182186
)
183187
result = getattr(ufunc, method)(*inputs, **kwargs)
184188

@@ -499,20 +503,20 @@ def to_numpy(
499503
# ------------------------------------------------------------------------
500504
# Ops
501505

502-
def __invert__(self) -> PandasArray:
506+
def __invert__(self) -> NumpyExtensionArray:
503507
return type(self)(~self._ndarray)
504508

505-
def __neg__(self) -> PandasArray:
509+
def __neg__(self) -> NumpyExtensionArray:
506510
return type(self)(-self._ndarray)
507511

508-
def __pos__(self) -> PandasArray:
512+
def __pos__(self) -> NumpyExtensionArray:
509513
return type(self)(+self._ndarray)
510514

511-
def __abs__(self) -> PandasArray:
515+
def __abs__(self) -> NumpyExtensionArray:
512516
return type(self)(abs(self._ndarray))
513517

514518
def _cmp_method(self, other, op):
515-
if isinstance(other, PandasArray):
519+
if isinstance(other, NumpyExtensionArray):
516520
other = other._ndarray
517521

518522
other = ops.maybe_prepare_scalar_for_op(other, (len(self),))
@@ -538,7 +542,7 @@ def _cmp_method(self, other, op):
538542

539543
def _wrap_ndarray_result(self, result: np.ndarray):
540544
# If we have timedelta64[ns] result, return a TimedeltaArray instead
541-
# of a PandasArray
545+
# of a NumpyExtensionArray
542546
if result.dtype.kind == "m" and is_supported_unit(
543547
get_unit_from_dtype(result.dtype)
544548
):

0 commit comments

Comments
 (0)