Skip to content

TYP: __array__ #30839

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

Merged
merged 1 commit into from
Jan 9, 2020
Merged
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
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ def shift(self, periods, fill_value=None):

return self.from_codes(codes, dtype=self.dtype)

def __array__(self, dtype=None):
def __array__(self, dtype=None) -> np.ndarray:
"""
The numpy array interface.

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ def _formatter(self, boxed=False):
def nbytes(self):
return self._data.nbytes

def __array__(self, dtype=None):
def __array__(self, dtype=None) -> np.ndarray:
# used for Timedelta/DatetimeArray, overwritten by PeriodArray
if is_object_dtype(dtype):
return np.array(list(self), dtype=object)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ def _resolution(self):
# ----------------------------------------------------------------
# Array-Like / EA-Interface Methods

def __array__(self, dtype=None):
def __array__(self, dtype=None) -> np.ndarray:
if dtype is None and self.tz:
# The default for tz-aware is object, to preserve tz info
dtype = object
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ def is_non_overlapping_monotonic(self):
)

# Conversion
def __array__(self, dtype=None):
def __array__(self, dtype=None) -> np.ndarray:
"""
Return the IntervalArray's data as a numpy array of Interval
objects (with dtype='object')
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def to_numpy(

__array_priority__ = 1000 # higher than ndarray so ops dispatch to us

def __array__(self, dtype=None):
def __array__(self, dtype=None) -> np.ndarray:
"""
the array interface, return my values
We return an object array here to preserve our scalar values
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def dtype(self):
# ------------------------------------------------------------------------
# NumPy Array Interface

def __array__(self, dtype=None):
def __array__(self, dtype=None) -> np.ndarray:
return np.asarray(self._ndarray, dtype=dtype)

_HANDLED_TYPES = (np.ndarray, numbers.Number)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def freq(self):
"""
return self.dtype.freq

def __array__(self, dtype=None):
def __array__(self, dtype=None) -> np.ndarray:
# overriding DatetimelikeArray
return np.array(list(self), dtype=object)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ def from_spmatrix(cls, data):

return cls._simple_new(arr, index, dtype)

def __array__(self, dtype=None, copy=True):
def __array__(self, dtype=None, copy=True) -> np.ndarray:
fill_value = self.fill_value

if self.sp_index.ngaps == 0:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1882,7 +1882,7 @@ def empty(self) -> bool_t:
# GH#23114 Ensure ndarray.__op__(DataFrame) returns NotImplemented
__array_priority__ = 1000

def __array__(self, dtype=None):
def __array__(self, dtype=None) -> np.ndarray:
return com.values_from_object(self)

def __array_wrap__(self, result, context=None):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ def __len__(self) -> int:
"""
return len(self._data)

def __array__(self, dtype=None):
def __array__(self, dtype=None) -> np.ndarray:
"""
The array interface, return my values.
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def __contains__(self, key) -> bool:

return contains(self, key, container=self._engine)

def __array__(self, dtype=None):
def __array__(self, dtype=None) -> np.ndarray:
""" the array interface, return my values """
return np.array(self._data, dtype=dtype)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def _simple_new(cls, values, name=None, freq=None, tz=None, dtype=None):

# --------------------------------------------------------------------

def __array__(self, dtype=None):
def __array__(self, dtype=None) -> np.ndarray:
return np.asarray(self._data, dtype=dtype)

@cache_readonly
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ def copy(
_set_identity=_set_identity,
)

def __array__(self, dtype=None):
def __array__(self, dtype=None) -> np.ndarray:
""" the array interface, return my values """
return self.values

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def _int64index(self):
# ------------------------------------------------------------------------
# Index Methods

def __array__(self, dtype=None):
def __array__(self, dtype=None) -> np.ndarray:
if is_integer_dtype(dtype):
return self.asi8
else:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ def construct_return(result):
else:
return construct_return(result)

def __array__(self, dtype=None):
def __array__(self, dtype=None) -> np.ndarray:
"""
Return the values as a NumPy array.

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class ArrayLike:
def __init__(self, array):
self.array = array

def __array__(self, dtype=None):
def __array__(self, dtype=None) -> np.ndarray:
return self.array

expected = pd.Index(array)
Expand Down