Skip to content

Commit e73fe18

Browse files
committed
pandas-dev#25802 Updated type hints to python3 syntax (array files)
1 parent d9c53ab commit e73fe18

File tree

7 files changed

+95
-86
lines changed

7 files changed

+95
-86
lines changed

pandas/core/arrays/array_.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
from pandas._libs import lib, tslibs
66

7+
from pandas.core.arrays.base import ExtensionArray
78
from pandas.core.dtypes.common import (
89
is_datetime64_ns_dtype, is_extension_array_dtype, is_timedelta64_ns_dtype)
910
from pandas.core.dtypes.dtypes import ExtensionDtype, registry
1011

1112
from pandas import compat
1213

1314

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

pandas/core/arrays/base.py

+35-34
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,7 @@ def __getitem__(self, item):
214214
"""
215215
raise AbstractMethodError(self)
216216

217-
def __setitem__(self, key, value):
218-
# type: (Union[int, np.ndarray], Any) -> None
217+
def __setitem__(self, key: Union[int, np.ndarray], value: Any) -> None:
219218
"""
220219
Set one or more values inplace.
221220
@@ -262,8 +261,7 @@ def __setitem__(self, key, value):
262261
type(self), '__setitem__')
263262
)
264263

265-
def __len__(self):
266-
# type: () -> int
264+
def __len__(self) -> int:
267265
"""
268266
Length of this array
269267
@@ -287,32 +285,28 @@ def __iter__(self):
287285
# Required attributes
288286
# ------------------------------------------------------------------------
289287
@property
290-
def dtype(self):
291-
# type: () -> ExtensionDtype
288+
def dtype(self) -> ExtensionDtype:
292289
"""
293290
An instance of 'ExtensionDtype'.
294291
"""
295292
raise AbstractMethodError(self)
296293

297294
@property
298-
def shape(self):
299-
# type: () -> Tuple[int, ...]
295+
def shape(self) -> Tuple[int, ...]:
300296
"""
301297
Return a tuple of the array dimensions.
302298
"""
303299
return (len(self),)
304300

305301
@property
306-
def ndim(self):
307-
# type: () -> int
302+
def ndim(self) -> int:
308303
"""
309304
Extension Arrays are only allowed to be 1-dimensional.
310305
"""
311306
return 1
312307

313308
@property
314-
def nbytes(self):
315-
# type: () -> int
309+
def nbytes(self) -> int:
316310
"""
317311
The number of bytes needed to store this object in memory.
318312
"""
@@ -343,8 +337,7 @@ def astype(self, dtype, copy=True):
343337
"""
344338
return np.array(self, dtype=dtype, copy=copy)
345339

346-
def isna(self):
347-
# type: () -> Union[ExtensionArray, np.ndarray]
340+
def isna(self) -> Union['ExtensionArray', np.ndarray]:
348341
"""
349342
A 1-D array indicating if each value is missing.
350343
@@ -366,8 +359,7 @@ def isna(self):
366359
"""
367360
raise AbstractMethodError(self)
368361

369-
def _values_for_argsort(self):
370-
# type: () -> np.ndarray
362+
def _values_for_argsort(self) -> np.ndarray:
371363
"""
372364
Return values for sorting.
373365
@@ -482,8 +474,11 @@ def dropna(self):
482474
"""
483475
return self[~self.isna()]
484476

485-
def shift(self, periods=1, fill_value=None):
486-
# type: (int, object) -> ExtensionArray
477+
def shift(
478+
self,
479+
periods: int = 1,
480+
fill_value: object = None,
481+
) -> 'ExtensionArray':
487482
"""
488483
Shift values by desired number.
489484
@@ -598,8 +593,7 @@ def searchsorted(self, value, side="left", sorter=None):
598593
arr = self.astype(object)
599594
return arr.searchsorted(value, side=side, sorter=sorter)
600595

601-
def _values_for_factorize(self):
602-
# type: () -> Tuple[np.ndarray, Any]
596+
def _values_for_factorize(self) -> Tuple[np.ndarray, Any]:
603597
"""
604598
Return an array and missing value suitable for factorization.
605599
@@ -623,8 +617,10 @@ def _values_for_factorize(self):
623617
"""
624618
return self.astype(object), np.nan
625619

626-
def factorize(self, na_sentinel=-1):
627-
# type: (int) -> Tuple[np.ndarray, ExtensionArray]
620+
def factorize(
621+
self,
622+
na_sentinel: int = -1,
623+
) -> Tuple[np.ndarray, 'ExtensionArray']:
628624
"""
629625
Encode the extension array as an enumerated type.
630626
@@ -726,8 +722,12 @@ def repeat(self, repeats, axis=None):
726722
# Indexing methods
727723
# ------------------------------------------------------------------------
728724

729-
def take(self, indices, allow_fill=False, fill_value=None):
730-
# type: (Sequence[int], bool, Optional[Any]) -> ExtensionArray
725+
def take(
726+
self,
727+
indices: Sequence[int],
728+
allow_fill: bool = False,
729+
fill_value: Optional[Any] = None
730+
) -> 'ExtensionArray':
731731
"""
732732
Take elements from an array.
733733
@@ -816,8 +816,7 @@ def take(self, indices, allow_fill=False, fill_value=None):
816816
# pandas.api.extensions.take
817817
raise AbstractMethodError(self)
818818

819-
def copy(self, deep=False):
820-
# type: (bool) -> ExtensionArray
819+
def copy(self, deep: bool = False) -> 'ExtensionArray':
821820
"""
822821
Return a copy of the array.
823822
@@ -853,8 +852,10 @@ def __repr__(self):
853852
length=len(self),
854853
dtype=self.dtype)
855854

856-
def _formatter(self, boxed=False):
857-
# type: (bool) -> Callable[[Any], Optional[str]]
855+
def _formatter(
856+
self,
857+
boxed: bool = False,
858+
) -> Callable[[Any], Optional[str]]:
858859
"""Formatting function for scalar values.
859860
860861
This is used in the default '__repr__'. The returned formatting
@@ -881,8 +882,7 @@ def _formatter(self, boxed=False):
881882
return str
882883
return repr
883884

884-
def _formatting_values(self):
885-
# type: () -> np.ndarray
885+
def _formatting_values(self) -> np.ndarray:
886886
# At the moment, this has to be an array since we use result.dtype
887887
"""
888888
An array of values to be printed in, e.g. the Series repr
@@ -898,8 +898,10 @@ def _formatting_values(self):
898898
# ------------------------------------------------------------------------
899899

900900
@classmethod
901-
def _concat_same_type(cls, to_concat):
902-
# type: (Sequence[ExtensionArray]) -> ExtensionArray
901+
def _concat_same_type(
902+
cls,
903+
to_concat: Sequence['ExtensionArray']
904+
) -> 'ExtensionArray':
903905
"""
904906
Concatenate multiple array
905907
@@ -921,8 +923,7 @@ def _concat_same_type(cls, to_concat):
921923
_can_hold_na = True
922924

923925
@property
924-
def _ndarray_values(self):
925-
# type: () -> np.ndarray
926+
def _ndarray_values(self) -> np.ndarray:
926927
"""
927928
Internal pandas method for lossy conversion to a NumPy ndarray.
928929

pandas/core/arrays/datetimelike.py

+18-16
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ def _get_attributes_dict(self):
5858
return {k: getattr(self, k, None) for k in self._attributes}
5959

6060
@property
61-
def _scalar_type(self):
62-
# type: () -> Union[type, Tuple[type]]
61+
def _scalar_type(self) -> Union[type, Tuple[type]]:
6362
"""The scalar associated with this datelike
6463
6564
* PeriodArray : Period
@@ -68,8 +67,10 @@ def _scalar_type(self):
6867
"""
6968
raise AbstractMethodError(self)
7069

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

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

112-
def _check_compatible_with(self, other):
113-
# type: (Union[Period, Timestamp, Timedelta, NaTType]) -> None
115+
def _check_compatible_with(
116+
self,
117+
other: Union[Period, Timestamp, Timedelta, NaTType],
118+
) -> None:
114119
"""
115120
Verify that `self` and `other` are compatible.
116121
@@ -350,8 +355,7 @@ def __iter__(self):
350355
return (self._box_func(v) for v in self.asi8)
351356

352357
@property
353-
def asi8(self):
354-
# type: () -> np.ndarray
358+
def asi8(self) -> np.ndarray:
355359
"""
356360
Integer representation of the values.
357361
@@ -402,8 +406,7 @@ def shape(self):
402406
return (len(self),)
403407

404408
@property
405-
def size(self):
406-
# type: () -> int
409+
def size(self) -> int:
407410
"""The number of elements in this array."""
408411
return np.prod(self.shape)
409412

@@ -461,10 +464,9 @@ def __getitem__(self, key):
461464

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

pandas/core/arrays/datetimes.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ class DatetimeArray(dtl.DatetimeLikeArrayMixin,
275275
# Constructors
276276

277277
_attributes = ["freq", "tz"]
278-
_dtype = None # type: Union[np.dtype, DatetimeTZDtype]
278+
_dtype: Union[np.dtype, DatetimeTZDtype] = None
279279
_freq = None
280280

281281
def __init__(self, values, dtype=_NS_DTYPE, freq=None, copy=False):
@@ -514,8 +514,7 @@ def _box_func(self):
514514
return lambda x: Timestamp(x, freq=self.freq, tz=self.tz)
515515

516516
@property
517-
def dtype(self):
518-
# type: () -> Union[np.dtype, DatetimeTZDtype]
517+
def dtype(self) -> Union[np.dtype, DatetimeTZDtype]:
519518
"""
520519
The dtype for the DatetimeArray.
521520

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

pandas/core/arrays/period.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,12 @@ def _simple_new(cls, values, freq=None, **kwargs):
183183
return cls(values, freq=freq, **kwargs)
184184

185185
@classmethod
186-
def _from_sequence(cls, scalars, dtype=None, copy=False):
187-
# type: (Sequence[Optional[Period]], PeriodDtype, bool) -> PeriodArray
186+
def _from_sequence(
187+
cls,
188+
scalars: Sequence[Optional[Period]],
189+
dtype: PeriodDtype = None,
190+
copy: bool = False,
191+
) -> 'PeriodArray':
188192
if dtype:
189193
freq = dtype.freq
190194
else:
@@ -246,8 +250,7 @@ def _generate_range(cls, start, end, periods, freq, fields):
246250
# -----------------------------------------------------------------
247251
# DatetimeLike Interface
248252

249-
def _unbox_scalar(self, value):
250-
# type: (Union[Period, NaTType]) -> int
253+
def _unbox_scalar(self, value: Union[Period, NaTType]) -> int:
251254
if value is NaT:
252255
return value.value
253256
elif isinstance(value, self._scalar_type):
@@ -258,8 +261,7 @@ def _unbox_scalar(self, value):
258261
raise ValueError("'value' should be a Period. Got '{val}' instead."
259262
.format(val=value))
260263

261-
def _scalar_from_string(self, value):
262-
# type: (str) -> Period
264+
def _scalar_from_string(self, value: str) -> Period:
263265
return Period(value, freq=self.freq)
264266

265267
def _check_compatible_with(self, other):
@@ -540,14 +542,9 @@ def _sub_period(self, other):
540542
@Appender(dtl.DatetimeLikeArrayMixin._addsub_int_array.__doc__)
541543
def _addsub_int_array(
542544
self,
543-
other, # type: Union[ExtensionArray, np.ndarray[int]]
544-
op # type: Callable[Any, Any]
545-
):
546-
# type: (...) -> PeriodArray
547-
548-
# TODO: ABCIndexClass is a valid type for other but had to be excluded
549-
# due to length of Py2 compatability comment; add back in once migrated
550-
# to Py3 syntax
545+
other: Union[ExtensionArray, np.ndarray, ABCIndexClass],
546+
op: Callable[[Any], Any]
547+
) -> 'PeriodArray':
551548
assert op in [operator.add, operator.sub]
552549
if op is operator.sub:
553550
other = -other
@@ -716,8 +713,11 @@ def _raise_on_incompatible(left, right):
716713
# -------------------------------------------------------------------
717714
# Constructor Helpers
718715

719-
def period_array(data, freq=None, copy=False):
720-
# type: (Sequence[Optional[Period]], Optional[Tick], bool) -> PeriodArray
716+
def period_array(
717+
data: Sequence[Optional[Period]],
718+
freq: Optional[Tick] = None,
719+
copy: bool = False,
720+
) -> PeriodArray:
721721
"""
722722
Construct a new PeriodArray from a sequence of Period scalars.
723723

0 commit comments

Comments
 (0)