Skip to content

Commit 10c7b04

Browse files
committed
TYP: annotate (pandas-dev#32730)
1 parent 192d736 commit 10c7b04

File tree

5 files changed

+36
-46
lines changed

5 files changed

+36
-46
lines changed

pandas/core/arrays/base.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from pandas.core.dtypes.common import is_array_like, is_list_like
2323
from pandas.core.dtypes.dtypes import ExtensionDtype
24-
from pandas.core.dtypes.generic import ABCExtensionArray, ABCIndexClass, ABCSeries
24+
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
2525
from pandas.core.dtypes.missing import isna
2626

2727
from pandas.core import ops
@@ -591,7 +591,7 @@ def dropna(self):
591591
"""
592592
return self[~self.isna()]
593593

594-
def shift(self, periods: int = 1, fill_value: object = None) -> ABCExtensionArray:
594+
def shift(self, periods: int = 1, fill_value: object = None) -> "ExtensionArray":
595595
"""
596596
Shift values by desired number.
597597
@@ -728,7 +728,7 @@ def _values_for_factorize(self) -> Tuple[np.ndarray, Any]:
728728
"""
729729
return self.astype(object), np.nan
730730

731-
def factorize(self, na_sentinel: int = -1) -> Tuple[np.ndarray, ABCExtensionArray]:
731+
def factorize(self, na_sentinel: int = -1) -> Tuple[np.ndarray, "ExtensionArray"]:
732732
"""
733733
Encode the extension array as an enumerated type.
734734
@@ -833,7 +833,7 @@ def repeat(self, repeats, axis=None):
833833

834834
def take(
835835
self, indices: Sequence[int], allow_fill: bool = False, fill_value: Any = None
836-
) -> ABCExtensionArray:
836+
) -> "ExtensionArray":
837837
"""
838838
Take elements from an array.
839839
@@ -922,7 +922,7 @@ def take(self, indices, allow_fill=False, fill_value=None):
922922
# pandas.api.extensions.take
923923
raise AbstractMethodError(self)
924924

925-
def copy(self) -> ABCExtensionArray:
925+
def copy(self) -> "ExtensionArray":
926926
"""
927927
Return a copy of the array.
928928
@@ -932,7 +932,7 @@ def copy(self) -> ABCExtensionArray:
932932
"""
933933
raise AbstractMethodError(self)
934934

935-
def view(self, dtype=None) -> Union[ABCExtensionArray, np.ndarray]:
935+
def view(self, dtype=None) -> ArrayLike:
936936
"""
937937
Return a view on the array.
938938
@@ -943,8 +943,8 @@ def view(self, dtype=None) -> Union[ABCExtensionArray, np.ndarray]:
943943
944944
Returns
945945
-------
946-
ExtensionArray
947-
A view of the :class:`ExtensionArray`.
946+
ExtensionArray or np.ndarray
947+
A view on the :class:`ExtensionArray`'s data.
948948
"""
949949
# NB:
950950
# - This must return a *new* object referencing the same data, not self.
@@ -1002,7 +1002,7 @@ def _formatter(self, boxed: bool = False) -> Callable[[Any], Optional[str]]:
10021002
# Reshaping
10031003
# ------------------------------------------------------------------------
10041004

1005-
def ravel(self, order="C") -> ABCExtensionArray:
1005+
def ravel(self, order="C") -> "ExtensionArray":
10061006
"""
10071007
Return a flattened view on this array.
10081008
@@ -1023,8 +1023,8 @@ def ravel(self, order="C") -> ABCExtensionArray:
10231023

10241024
@classmethod
10251025
def _concat_same_type(
1026-
cls, to_concat: Sequence[ABCExtensionArray]
1027-
) -> ABCExtensionArray:
1026+
cls, to_concat: Sequence["ExtensionArray"]
1027+
) -> "ExtensionArray":
10281028
"""
10291029
Concatenate multiple array.
10301030

pandas/core/arrays/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class AttributesMixin:
131131
_data: np.ndarray
132132

133133
@classmethod
134-
def _simple_new(cls, values, **kwargs):
134+
def _simple_new(cls, values: np.ndarray, **kwargs):
135135
raise AbstractMethodError(cls)
136136

137137
@property

pandas/core/arrays/period.py

+16-26
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@
3131
pandas_dtype,
3232
)
3333
from pandas.core.dtypes.dtypes import PeriodDtype
34-
from pandas.core.dtypes.generic import (
35-
ABCIndexClass,
36-
ABCPeriod,
37-
ABCPeriodArray,
38-
ABCPeriodIndex,
39-
ABCSeries,
40-
)
34+
from pandas.core.dtypes.generic import ABCIndexClass, ABCPeriodIndex, ABCSeries
4135
from pandas.core.dtypes.missing import isna, notna
4236

4337
import pandas.core.algorithms as algos
@@ -48,7 +42,7 @@
4842
from pandas.tseries.offsets import DateOffset, Tick, _delta_to_tick
4943

5044

51-
def _field_accessor(name, alias, docstring=None):
45+
def _field_accessor(name: str, alias: int, docstring=None):
5246
def f(self):
5347
base, mult = libfrequencies.get_freq_code(self.freq)
5448
result = get_period_field_arr(alias, self.asi8, base)
@@ -170,7 +164,7 @@ def __init__(self, values, freq=None, dtype=None, copy=False):
170164
self._dtype = PeriodDtype(freq)
171165

172166
@classmethod
173-
def _simple_new(cls, values: np.ndarray, freq=None, **kwargs):
167+
def _simple_new(cls, values: np.ndarray, freq=None, **kwargs) -> "PeriodArray":
174168
# alias for PeriodArray.__init__
175169
assert isinstance(values, np.ndarray) and values.dtype == "i8"
176170
return cls(values, freq=freq, **kwargs)
@@ -181,7 +175,7 @@ def _from_sequence(
181175
scalars: Sequence[Optional[Period]],
182176
dtype: Optional[PeriodDtype] = None,
183177
copy: bool = False,
184-
) -> ABCPeriodArray:
178+
) -> "PeriodArray":
185179
if dtype:
186180
freq = dtype.freq
187181
else:
@@ -202,11 +196,13 @@ def _from_sequence(
202196
return cls(ordinals, freq=freq)
203197

204198
@classmethod
205-
def _from_sequence_of_strings(cls, strings, dtype=None, copy=False):
199+
def _from_sequence_of_strings(
200+
cls, strings, dtype=None, copy=False
201+
) -> "PeriodArray":
206202
return cls._from_sequence(strings, dtype, copy)
207203

208204
@classmethod
209-
def _from_datetime64(cls, data, freq, tz=None):
205+
def _from_datetime64(cls, data, freq, tz=None) -> "PeriodArray":
210206
"""
211207
Construct a PeriodArray from a datetime64 array
212208
@@ -270,12 +266,12 @@ def _check_compatible_with(self, other, setitem: bool = False):
270266
# Data / Attributes
271267

272268
@cache_readonly
273-
def dtype(self):
269+
def dtype(self) -> PeriodDtype:
274270
return self._dtype
275271

276272
# error: Read-only property cannot override read-write property [misc]
277273
@property # type: ignore
278-
def freq(self):
274+
def freq(self) -> DateOffset:
279275
"""
280276
Return the frequency object for this PeriodArray.
281277
"""
@@ -402,7 +398,7 @@ def __arrow_array__(self, type=None):
402398
daysinmonth = days_in_month
403399

404400
@property
405-
def is_leap_year(self):
401+
def is_leap_year(self) -> np.ndarray:
406402
"""
407403
Logical indicating if the date belongs to a leap year.
408404
"""
@@ -458,12 +454,6 @@ def to_timestamp(self, freq=None, how="start"):
458454
new_data = libperiod.periodarr_to_dt64arr(new_data.asi8, base)
459455
return DatetimeArray._from_sequence(new_data, freq="infer")
460456

461-
# --------------------------------------------------------------------
462-
# Array-like / EA-Interface Methods
463-
464-
def _values_for_argsort(self):
465-
return self._data
466-
467457
# --------------------------------------------------------------------
468458

469459
def _time_shift(self, periods, freq=None):
@@ -495,7 +485,7 @@ def _time_shift(self, periods, freq=None):
495485
def _box_func(self):
496486
return lambda x: Period._from_ordinal(ordinal=x, freq=self.freq)
497487

498-
def asfreq(self, freq=None, how="E"):
488+
def asfreq(self, freq=None, how="E") -> "PeriodArray":
499489
"""
500490
Convert the Period Array/Index to the specified frequency `freq`.
501491
@@ -557,7 +547,7 @@ def asfreq(self, freq=None, how="E"):
557547
# ------------------------------------------------------------------
558548
# Rendering Methods
559549

560-
def _formatter(self, boxed=False):
550+
def _formatter(self, boxed: bool = False):
561551
if boxed:
562552
return str
563553
return "'{}'".format
@@ -584,7 +574,7 @@ def _format_native_types(self, na_rep="NaT", date_format=None, **kwargs):
584574

585575
# ------------------------------------------------------------------
586576

587-
def astype(self, dtype, copy=True):
577+
def astype(self, dtype, copy: bool = True):
588578
# We handle Period[T] -> Period[U]
589579
# Our parent handles everything else.
590580
dtype = pandas_dtype(dtype)
@@ -965,8 +955,8 @@ def _get_ordinal_range(start, end, periods, freq, mult=1):
965955
if end is not None:
966956
end = Period(end, freq)
967957

968-
is_start_per = isinstance(start, ABCPeriod)
969-
is_end_per = isinstance(end, ABCPeriod)
958+
is_start_per = isinstance(start, Period)
959+
is_end_per = isinstance(end, Period)
970960

971961
if is_start_per and is_end_per and start.freq != end.freq:
972962
raise ValueError("start and end must have same freq")

pandas/core/ops/dispatch.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"""
22
Functions for defining unary operations.
33
"""
4-
from typing import Any, Union
4+
from typing import Any
55

66
import numpy as np
77

8+
from pandas._typing import ArrayLike
9+
810
from pandas.core.dtypes.common import (
911
is_datetime64_dtype,
1012
is_extension_array_dtype,
@@ -13,7 +15,7 @@
1315
is_scalar,
1416
is_timedelta64_dtype,
1517
)
16-
from pandas.core.dtypes.generic import ABCExtensionArray, ABCSeries
18+
from pandas.core.dtypes.generic import ABCSeries
1719

1820
from pandas.core.construction import array
1921

@@ -93,9 +95,7 @@ def should_series_dispatch(left, right, op):
9395
return False
9496

9597

96-
def dispatch_to_extension_op(
97-
op, left: Union[ABCExtensionArray, np.ndarray], right: Any,
98-
):
98+
def dispatch_to_extension_op(op, left: ArrayLike, right: Any):
9999
"""
100100
Assume that left or right is a Series backed by an ExtensionArray,
101101
apply the operator defined by op.

pandas/io/pytables.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2202,7 +2202,7 @@ def __eq__(self, other: Any) -> bool:
22022202
for a in ["name", "cname", "dtype", "pos"]
22032203
)
22042204

2205-
def set_data(self, data: Union[np.ndarray, ABCExtensionArray]):
2205+
def set_data(self, data: ArrayLike):
22062206
assert data is not None
22072207
assert self.dtype is None
22082208

@@ -4959,11 +4959,11 @@ def _dtype_to_kind(dtype_str: str) -> str:
49594959
return kind
49604960

49614961

4962-
def _get_data_and_dtype_name(data: Union[np.ndarray, ABCExtensionArray]):
4962+
def _get_data_and_dtype_name(data: ArrayLike):
49634963
"""
49644964
Convert the passed data into a storable form and a dtype string.
49654965
"""
4966-
if is_categorical_dtype(data.dtype):
4966+
if isinstance(data, Categorical):
49674967
data = data.codes
49684968

49694969
# For datetime64tz we need to drop the TZ in tests TODO: why?

0 commit comments

Comments
 (0)