Skip to content

TYP: Arraylike #31518

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

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 3 additions & 3 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ def isna(self) -> ArrayLike:

Returns
-------
na_values : Union[np.ndarray, ExtensionArray]
na_values : ArrayLike
In most cases, this should return a NumPy ndarray. For
exceptional cases like ``SparseArray``, where returning
an ndarray would be expensive, an ExtensionArray may be
Expand Down Expand Up @@ -922,7 +922,7 @@ def copy(self) -> ABCExtensionArray:
"""
raise AbstractMethodError(self)

def view(self, dtype=None) -> Union[ABCExtensionArray, np.ndarray]:
def view(self, dtype=None) -> ArrayLike:
"""
Return a view on the array.

Expand Down Expand Up @@ -1175,7 +1175,7 @@ def _create_method(cls, op, coerce_to_dtype=True):

Returns
-------
Callable[[Any, Any], Union[ndarray, ExtensionArray]]
Callable[[Any, Any], ArrayLike]
A method that can be bound to a class. When used, the method
receives the two arguments, one of which is the instance of
this class, and should return an ExtensionArray or an ndarray.
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import builtins
import textwrap
from typing import Dict, FrozenSet, List, Optional, Union
from typing import Dict, FrozenSet, List, Optional

import numpy as np

import pandas._libs.lib as lib
from pandas._typing import T
from pandas._typing import ArrayLike, T
from pandas.compat import PYPY
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
Expand Down Expand Up @@ -606,7 +606,7 @@ class IndexOpsMixin:
)

@property
def _values(self) -> Union[ExtensionArray, np.ndarray]:
def _values(self) -> ArrayLike:
# must be defined here as a property for mypy
raise AbstractMethodError(self)

Expand Down
8 changes: 4 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
import operator
from textwrap import dedent
from typing import Any, FrozenSet, Hashable, Optional, Union
from typing import Any, FrozenSet, Hashable, Optional
import warnings

import numpy as np
Expand All @@ -12,7 +12,7 @@
from pandas._libs.tslibs import OutOfBoundsDatetime, Timestamp
from pandas._libs.tslibs.period import IncompatibleFrequency
from pandas._libs.tslibs.timezones import tz_compare
from pandas._typing import Label
from pandas._typing import ArrayLike, Label
from pandas.compat import set_function_name
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender, Substitution, cache_readonly
Expand Down Expand Up @@ -243,7 +243,7 @@ def _outer_indexer(self, left, right):
return libjoin.outer_join_indexer(left, right)

_typ = "index"
_data: Union[ExtensionArray, np.ndarray]
_data: ArrayLike
_id = None
_name: Label = None
# MultiIndex.levels previously allowed setting the index name. We
Expand Down Expand Up @@ -3891,7 +3891,7 @@ def array(self) -> ExtensionArray:
return array

@property
def _values(self) -> Union[ExtensionArray, np.ndarray]:
def _values(self) -> ArrayLike:
"""
The best array representation.

Expand Down
21 changes: 7 additions & 14 deletions pandas/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,17 @@
"""
import datetime
import operator
from typing import Optional, Set, Tuple, Union
from typing import TYPE_CHECKING, Optional, Set, Tuple

import numpy as np

from pandas._libs import Timedelta, Timestamp, lib
from pandas._libs.ops_dispatch import maybe_dispatch_ufunc_to_dunder_op # noqa:F401
from pandas._typing import Level
from pandas._typing import ArrayLike, Level
from pandas.util._decorators import Appender

from pandas.core.dtypes.common import is_list_like, is_timedelta64_dtype
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCExtensionArray,
ABCIndexClass,
ABCSeries,
)
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
from pandas.core.dtypes.missing import isna

from pandas.core.construction import extract_array
Expand Down Expand Up @@ -61,6 +56,9 @@
rxor,
)

if TYPE_CHECKING:
from pandas import Index, Series # noqa: F401

# -----------------------------------------------------------------------------
# constants
ARITHMETIC_BINOPS: Set[str] = {
Expand Down Expand Up @@ -449,12 +447,7 @@ def _align_method_SERIES(left, right, align_asobject=False):
return left, right


def _construct_result(
left: ABCSeries,
result: Union[np.ndarray, ABCExtensionArray],
index: ABCIndexClass,
name,
):
def _construct_result(left: "Series", result: ArrayLike, index: "Index", name):
"""
Construct an appropriately-labelled Series from the result of an op.

Expand Down
15 changes: 5 additions & 10 deletions pandas/core/ops/array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"""
from functools import partial
import operator
from typing import Any, Optional, Union
from typing import Any, Optional

import numpy as np

from pandas._libs import Timestamp, lib, ops as libops
from pandas._typing import ArrayLike

from pandas.core.dtypes.cast import (
construct_1d_object_array_from_listlike,
Expand Down Expand Up @@ -155,9 +156,7 @@ def na_arithmetic_op(left, right, op, str_rep: str):
return missing.dispatch_fill_zeros(op, left, right, result)


def arithmetic_op(
left: Union[np.ndarray, ABCExtensionArray], right: Any, op, str_rep: str
):
def arithmetic_op(left: ArrayLike, right: Any, op, str_rep: str):
"""
Evaluate an arithmetic operation `+`, `-`, `*`, `/`, `//`, `%`, `**`, ...

Expand Down Expand Up @@ -200,9 +199,7 @@ def arithmetic_op(
return res_values


def comparison_op(
left: Union[np.ndarray, ABCExtensionArray], right: Any, op
) -> Union[np.ndarray, ABCExtensionArray]:
def comparison_op(left: ArrayLike, right: Any, op) -> ArrayLike:
"""
Evaluate a comparison operation `=`, `!=`, `>=`, `>`, `<=`, or `<`.

Expand Down Expand Up @@ -303,9 +300,7 @@ def na_logical_op(x: np.ndarray, y, op):
return result.reshape(x.shape)


def logical_op(
left: Union[np.ndarray, ABCExtensionArray], right: Any, op
) -> Union[np.ndarray, ABCExtensionArray]:
def logical_op(left: ArrayLike, right: Any, op) -> ArrayLike:
"""
Evaluate a logical operation `|`, `&`, or `^`.

Expand Down
10 changes: 5 additions & 5 deletions pandas/core/ops/dispatch.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""
Functions for defining unary operations.
"""
from typing import Any, Union
from typing import Any

import numpy as np

from pandas._typing import ArrayLike

from pandas.core.dtypes.common import (
is_datetime64_dtype,
is_extension_array_dtype,
Expand All @@ -13,7 +15,7 @@
is_scalar,
is_timedelta64_dtype,
)
from pandas.core.dtypes.generic import ABCExtensionArray, ABCSeries
from pandas.core.dtypes.generic import ABCSeries

from pandas.core.construction import array

Expand Down Expand Up @@ -93,9 +95,7 @@ def should_series_dispatch(left, right, op):
return False


def dispatch_to_extension_op(
op, left: Union[ABCExtensionArray, np.ndarray], right: Any,
):
def dispatch_to_extension_op(op, left: ArrayLike, right: Any):
"""
Assume that left or right is a Series backed by an ExtensionArray,
apply the operator defined by op.
Expand Down
6 changes: 3 additions & 3 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2216,7 +2216,7 @@ def __eq__(self, other: Any) -> bool:
for a in ["name", "cname", "dtype", "pos"]
)

def set_data(self, data: Union[np.ndarray, ABCExtensionArray]):
def set_data(self, data: ArrayLike):
assert data is not None
assert self.dtype is None

Expand All @@ -2231,7 +2231,7 @@ def take_data(self):
return self.data

@classmethod
def _get_atom(cls, values: Union[np.ndarray, ABCExtensionArray]) -> "Col":
def _get_atom(cls, values: ArrayLike) -> "Col":
"""
Get an appropriately typed and shaped pytables.Col object for values.
"""
Expand Down Expand Up @@ -4977,7 +4977,7 @@ def _dtype_to_kind(dtype_str: str) -> str:
return kind


def _get_data_and_dtype_name(data: Union[np.ndarray, ABCExtensionArray]):
def _get_data_and_dtype_name(data: ArrayLike):
"""
Convert the passed data into a storable form and a dtype string.
"""
Expand Down