Skip to content

Commit f0ba498

Browse files
gwromeWillAyd
authored andcommitted
#25790 Updating type hints to Python3 syntax in pandas/core/array (#25829)
1 parent 3de9137 commit f0ba498

File tree

7 files changed

+101
-90
lines changed

7 files changed

+101
-90
lines changed

pandas/core/arrays/array_.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
from pandas.core.dtypes.common import (
88
is_datetime64_ns_dtype, is_extension_array_dtype, is_timedelta64_ns_dtype)
99
from pandas.core.dtypes.dtypes import ExtensionDtype, registry
10+
from pandas.core.dtypes.generic import ABCExtensionArray
1011

1112

12-
def array(data, # type: Sequence[object]
13-
dtype=None, # type: Optional[Union[str, np.dtype, ExtensionDtype]]
14-
copy=True, # type: bool
15-
):
16-
# type: (...) -> ExtensionArray
13+
def array(data: Sequence[object],
14+
dtype: Optional[Union[str, np.dtype, ExtensionDtype]] = None,
15+
copy: bool = True,
16+
) -> ABCExtensionArray:
1717
"""
1818
Create an array.
1919

pandas/core/arrays/base.py

+37-35
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
from pandas.core.dtypes.common import is_list_like
1919
from pandas.core.dtypes.dtypes import ExtensionDtype
20-
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
20+
from pandas.core.dtypes.generic import (
21+
ABCExtensionArray, ABCIndexClass, ABCSeries)
2122
from pandas.core.dtypes.missing import isna
2223

2324
from pandas.core import ops
@@ -214,8 +215,7 @@ def __getitem__(self, item):
214215
"""
215216
raise AbstractMethodError(self)
216217

217-
def __setitem__(self, key, value):
218-
# type: (Union[int, np.ndarray], Any) -> None
218+
def __setitem__(self, key: Union[int, np.ndarray], value: Any) -> None:
219219
"""
220220
Set one or more values inplace.
221221
@@ -262,8 +262,7 @@ def __setitem__(self, key, value):
262262
type(self), '__setitem__')
263263
)
264264

265-
def __len__(self):
266-
# type: () -> int
265+
def __len__(self) -> int:
267266
"""
268267
Length of this array
269268
@@ -287,32 +286,28 @@ def __iter__(self):
287286
# Required attributes
288287
# ------------------------------------------------------------------------
289288
@property
290-
def dtype(self):
291-
# type: () -> ExtensionDtype
289+
def dtype(self) -> ExtensionDtype:
292290
"""
293291
An instance of 'ExtensionDtype'.
294292
"""
295293
raise AbstractMethodError(self)
296294

297295
@property
298-
def shape(self):
299-
# type: () -> Tuple[int, ...]
296+
def shape(self) -> Tuple[int, ...]:
300297
"""
301298
Return a tuple of the array dimensions.
302299
"""
303300
return (len(self),)
304301

305302
@property
306-
def ndim(self):
307-
# type: () -> int
303+
def ndim(self) -> int:
308304
"""
309305
Extension Arrays are only allowed to be 1-dimensional.
310306
"""
311307
return 1
312308

313309
@property
314-
def nbytes(self):
315-
# type: () -> int
310+
def nbytes(self) -> int:
316311
"""
317312
The number of bytes needed to store this object in memory.
318313
"""
@@ -343,8 +338,7 @@ def astype(self, dtype, copy=True):
343338
"""
344339
return np.array(self, dtype=dtype, copy=copy)
345340

346-
def isna(self):
347-
# type: () -> Union[ExtensionArray, np.ndarray]
341+
def isna(self) -> Union[ABCExtensionArray, np.ndarray]:
348342
"""
349343
A 1-D array indicating if each value is missing.
350344
@@ -366,8 +360,7 @@ def isna(self):
366360
"""
367361
raise AbstractMethodError(self)
368362

369-
def _values_for_argsort(self):
370-
# type: () -> np.ndarray
363+
def _values_for_argsort(self) -> np.ndarray:
371364
"""
372365
Return values for sorting.
373366
@@ -482,8 +475,11 @@ def dropna(self):
482475
"""
483476
return self[~self.isna()]
484477

485-
def shift(self, periods=1, fill_value=None):
486-
# type: (int, object) -> ExtensionArray
478+
def shift(
479+
self,
480+
periods: int = 1,
481+
fill_value: object = None,
482+
) -> ABCExtensionArray:
487483
"""
488484
Shift values by desired number.
489485
@@ -598,8 +594,7 @@ def searchsorted(self, value, side="left", sorter=None):
598594
arr = self.astype(object)
599595
return arr.searchsorted(value, side=side, sorter=sorter)
600596

601-
def _values_for_factorize(self):
602-
# type: () -> Tuple[np.ndarray, Any]
597+
def _values_for_factorize(self) -> Tuple[np.ndarray, Any]:
603598
"""
604599
Return an array and missing value suitable for factorization.
605600
@@ -623,8 +618,10 @@ def _values_for_factorize(self):
623618
"""
624619
return self.astype(object), np.nan
625620

626-
def factorize(self, na_sentinel=-1):
627-
# type: (int) -> Tuple[np.ndarray, ExtensionArray]
621+
def factorize(
622+
self,
623+
na_sentinel: int = -1,
624+
) -> Tuple[np.ndarray, ABCExtensionArray]:
628625
"""
629626
Encode the extension array as an enumerated type.
630627
@@ -726,8 +723,12 @@ def repeat(self, repeats, axis=None):
726723
# Indexing methods
727724
# ------------------------------------------------------------------------
728725

729-
def take(self, indices, allow_fill=False, fill_value=None):
730-
# type: (Sequence[int], bool, Optional[Any]) -> ExtensionArray
726+
def take(
727+
self,
728+
indices: Sequence[int],
729+
allow_fill: bool = False,
730+
fill_value: Any = None
731+
) -> ABCExtensionArray:
731732
"""
732733
Take elements from an array.
733734
@@ -816,8 +817,7 @@ def take(self, indices, allow_fill=False, fill_value=None):
816817
# pandas.api.extensions.take
817818
raise AbstractMethodError(self)
818819

819-
def copy(self, deep=False):
820-
# type: (bool) -> ExtensionArray
820+
def copy(self, deep: bool = False) -> ABCExtensionArray:
821821
"""
822822
Return a copy of the array.
823823
@@ -853,8 +853,10 @@ def __repr__(self):
853853
length=len(self),
854854
dtype=self.dtype)
855855

856-
def _formatter(self, boxed=False):
857-
# type: (bool) -> Callable[[Any], Optional[str]]
856+
def _formatter(
857+
self,
858+
boxed: bool = False,
859+
) -> Callable[[Any], Optional[str]]:
858860
"""Formatting function for scalar values.
859861
860862
This is used in the default '__repr__'. The returned formatting
@@ -881,8 +883,7 @@ def _formatter(self, boxed=False):
881883
return str
882884
return repr
883885

884-
def _formatting_values(self):
885-
# type: () -> np.ndarray
886+
def _formatting_values(self) -> np.ndarray:
886887
# At the moment, this has to be an array since we use result.dtype
887888
"""
888889
An array of values to be printed in, e.g. the Series repr
@@ -898,8 +899,10 @@ def _formatting_values(self):
898899
# ------------------------------------------------------------------------
899900

900901
@classmethod
901-
def _concat_same_type(cls, to_concat):
902-
# type: (Sequence[ExtensionArray]) -> ExtensionArray
902+
def _concat_same_type(
903+
cls,
904+
to_concat: Sequence[ABCExtensionArray]
905+
) -> ABCExtensionArray:
903906
"""
904907
Concatenate multiple array
905908
@@ -921,8 +924,7 @@ def _concat_same_type(cls, to_concat):
921924
_can_hold_na = True
922925

923926
@property
924-
def _ndarray_values(self):
925-
# type: () -> np.ndarray
927+
def _ndarray_values(self) -> np.ndarray:
926928
"""
927929
Internal pandas method for lossy conversion to a NumPy ndarray.
928930

pandas/core/arrays/datetimelike.py

+19-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from datetime import datetime, timedelta
33
import operator
4-
from typing import Any, Sequence, Tuple, Union
4+
from typing import Any, Sequence, Tuple, Type, Union
55
import warnings
66

77
import numpy as np
@@ -57,8 +57,7 @@ def _get_attributes_dict(self):
5757
return {k: getattr(self, k, None) for k in self._attributes}
5858

5959
@property
60-
def _scalar_type(self):
61-
# type: () -> Union[type, Tuple[type]]
60+
def _scalar_type(self) -> Union[Type, Tuple[Type]]:
6261
"""The scalar associated with this datelike
6362
6463
* PeriodArray : Period
@@ -67,8 +66,10 @@ def _scalar_type(self):
6766
"""
6867
raise AbstractMethodError(self)
6968

70-
def _scalar_from_string(self, value):
71-
# type: (str) -> Union[Period, Timestamp, Timedelta, NaTType]
69+
def _scalar_from_string(
70+
self,
71+
value: str,
72+
) -> Union[Period, Timestamp, Timedelta, NaTType]:
7273
"""
7374
Construct a scalar type from a string.
7475
@@ -88,8 +89,10 @@ def _scalar_from_string(self, value):
8889
"""
8990
raise AbstractMethodError(self)
9091

91-
def _unbox_scalar(self, value):
92-
# type: (Union[Period, Timestamp, Timedelta, NaTType]) -> int
92+
def _unbox_scalar(
93+
self,
94+
value: Union[Period, Timestamp, Timedelta, NaTType],
95+
) -> int:
9396
"""
9497
Unbox the integer value of a scalar `value`.
9598
@@ -108,8 +111,10 @@ def _unbox_scalar(self, value):
108111
"""
109112
raise AbstractMethodError(self)
110113

111-
def _check_compatible_with(self, other):
112-
# type: (Union[Period, Timestamp, Timedelta, NaTType]) -> None
114+
def _check_compatible_with(
115+
self,
116+
other: Union[Period, Timestamp, Timedelta, NaTType],
117+
) -> None:
113118
"""
114119
Verify that `self` and `other` are compatible.
115120
@@ -349,8 +354,7 @@ def __iter__(self):
349354
return (self._box_func(v) for v in self.asi8)
350355

351356
@property
352-
def asi8(self):
353-
# type: () -> np.ndarray
357+
def asi8(self) -> np.ndarray:
354358
"""
355359
Integer representation of the values.
356360
@@ -401,8 +405,7 @@ def shape(self):
401405
return (len(self),)
402406

403407
@property
404-
def size(self):
405-
# type: () -> int
408+
def size(self) -> int:
406409
"""The number of elements in this array."""
407410
return np.prod(self.shape)
408411

@@ -460,10 +463,9 @@ def __getitem__(self, key):
460463

461464
def __setitem__(
462465
self,
463-
key, # type: Union[int, Sequence[int], Sequence[bool], slice]
464-
value, # type: Union[NaTType, Any, Sequence[Any]]
465-
):
466-
# type: (...) -> None
466+
key: Union[int, Sequence[int], Sequence[bool], slice],
467+
value: Union[NaTType, Any, Sequence[Any]]
468+
) -> None:
467469
# I'm fudging the types a bit here. "Any" above really depends
468470
# on type(self). For PeriodArray, it's Period (or stuff coercible
469471
# to a period in from_sequence). For DatetimeArray, it's Timestamp...

pandas/core/arrays/datetimes.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,7 @@ def _box_func(self):
511511
return lambda x: Timestamp(x, freq=self.freq, tz=self.tz)
512512

513513
@property
514-
def dtype(self):
515-
# type: () -> Union[np.dtype, DatetimeTZDtype]
514+
def dtype(self) -> Union[np.dtype, DatetimeTZDtype]:
516515
"""
517516
The dtype for the DatetimeArray.
518517

pandas/core/arrays/integer.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,7 @@ def astype(self, dtype, copy=True):
452452
return astype_nansafe(data, dtype, copy=None)
453453

454454
@property
455-
def _ndarray_values(self):
456-
# type: () -> np.ndarray
455+
def _ndarray_values(self) -> np.ndarray:
457456
"""Internal pandas method for lossy conversion to a NumPy ndarray.
458457
459458
This method is not part of the pandas interface.
@@ -509,8 +508,7 @@ def value_counts(self, dropna=True):
509508

510509
return Series(array, index=index)
511510

512-
def _values_for_argsort(self):
513-
# type: () -> np.ndarray
511+
def _values_for_argsort(self) -> np.ndarray:
514512
"""Return values for sorting.
515513
516514
Returns

0 commit comments

Comments
 (0)