Skip to content

Commit c1a8ad6

Browse files
committed
annotations
1 parent 6620dc6 commit c1a8ad6

File tree

6 files changed

+37
-45
lines changed

6 files changed

+37
-45
lines changed

pandas/core/arrays/base.py

Lines changed: 9 additions & 9 deletions
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
@@ -590,7 +590,7 @@ def dropna(self):
590590
"""
591591
return self[~self.isna()]
592592

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

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

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

924-
def copy(self) -> ABCExtensionArray:
924+
def copy(self) -> "ExtensionArray":
925925
"""
926926
Return a copy of the array.
927927
@@ -931,7 +931,7 @@ def copy(self) -> ABCExtensionArray:
931931
"""
932932
raise AbstractMethodError(self)
933933

934-
def view(self, dtype=None) -> Union[ABCExtensionArray, np.ndarray]:
934+
def view(self, dtype=None) -> ArrayLike:
935935
"""
936936
Return a view on the array.
937937
@@ -1001,7 +1001,7 @@ def _formatter(self, boxed: bool = False) -> Callable[[Any], Optional[str]]:
10011001
# Reshaping
10021002
# ------------------------------------------------------------------------
10031003

1004-
def ravel(self, order="C") -> ABCExtensionArray:
1004+
def ravel(self, order="C") -> "ExtensionArray":
10051005
"""
10061006
Return a flattened view on this array.
10071007
@@ -1022,8 +1022,8 @@ def ravel(self, order="C") -> ABCExtensionArray:
10221022

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

pandas/core/arrays/datetimelike.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class AttributesMixin:
130130
_data: np.ndarray
131131

132132
@classmethod
133-
def _simple_new(cls, values, **kwargs):
133+
def _simple_new(cls, values: np.ndarray, **kwargs):
134134
raise AbstractMethodError(cls)
135135

136136
@property

pandas/core/arrays/period.py

Lines changed: 16 additions & 26 deletions
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/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import builtins
66
import textwrap
7-
from typing import Dict, FrozenSet, List, Optional, Union
7+
from typing import Dict, FrozenSet, List, Optional
88

99
import numpy as np
1010

@@ -34,6 +34,7 @@
3434
from pandas.core.arrays import ExtensionArray
3535
from pandas.core.construction import create_series_with_explicit_dtype
3636
import pandas.core.nanops as nanops
37+
from pandas.typing import ArrayLike
3738

3839
_shared_docs: Dict[str, str] = dict()
3940
_indexops_doc_kwargs = dict(
@@ -598,7 +599,7 @@ class IndexOpsMixin:
598599
)
599600

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

pandas/core/ops/dispatch.py

Lines changed: 5 additions & 5 deletions
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

Lines changed: 3 additions & 2 deletions
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,12 @@ 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
"""
49664966
if is_categorical_dtype(data.dtype):
4967+
assert isinstance(data, Categorical) # for mypy
49674968
data = data.codes
49684969

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

0 commit comments

Comments
 (0)