Skip to content

Commit aa19aa4

Browse files
committed
pandas-dev#25790 Updated type hints to Python3 style in files outside core/arrays/
1 parent 37d04a3 commit aa19aa4

File tree

10 files changed

+36
-44
lines changed

10 files changed

+36
-44
lines changed

pandas/core/base.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,7 @@ def base(self):
786786
return self.values.base
787787

788788
@property
789-
def array(self):
790-
# type: () -> ExtensionArray
789+
def array(self) -> ExtensionArray:
791790
"""
792791
The ExtensionArray of the data backing this Series or Index.
793792
@@ -962,8 +961,7 @@ def to_numpy(self, dtype=None, copy=False):
962961
return result
963962

964963
@property
965-
def _ndarray_values(self):
966-
# type: () -> np.ndarray
964+
def _ndarray_values(self) -> np.ndarray:
967965
"""
968966
The data as an ndarray, possibly losing information.
969967

pandas/core/common.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ def maybe_box_datetimelike(value):
9393
values_from_object = lib.values_from_object
9494

9595

96-
def is_bool_indexer(key):
97-
# type: (Any) -> bool
96+
def is_bool_indexer(key: Any) -> bool:
9897
"""
9998
Check whether `key` is a valid boolean indexer.
10099

pandas/core/dtypes/base.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Extend pandas with custom array types"""
2-
from typing import List, Optional, Type
2+
from typing import List, Optional
33

44
import numpy as np
55

@@ -65,8 +65,7 @@ def __ne__(self, other):
6565
return not self.__eq__(other)
6666

6767
@property
68-
def names(self):
69-
# type: () -> Optional[List[str]]
68+
def names(self) -> Optional[List[str]]:
7069
"""Ordered list of field names, or None if there are no fields.
7170
7271
This is for compatibility with NumPy arrays, and may be removed in the
@@ -116,8 +115,7 @@ def is_dtype(cls, dtype):
116115
return False
117116

118117
@property
119-
def _is_numeric(self):
120-
# type: () -> bool
118+
def _is_numeric(self) -> bool:
121119
"""
122120
Whether columns with this dtype should be considered numeric.
123121
@@ -128,8 +126,7 @@ def _is_numeric(self):
128126
return False
129127

130128
@property
131-
def _is_boolean(self):
132-
# type: () -> bool
129+
def _is_boolean(self) -> bool:
133130
"""
134131
Whether this dtype should be considered boolean.
135132
@@ -212,8 +209,7 @@ def __str__(self):
212209
return self.name
213210

214211
@property
215-
def type(self):
216-
# type: () -> Type
212+
def type(self) -> bool:
217213
"""
218214
The scalar type for the array, e.g. ``int``
219215
@@ -242,8 +238,7 @@ def kind(self):
242238
return 'O'
243239

244240
@property
245-
def name(self):
246-
# type: () -> str
241+
def name(self) -> str:
247242
"""
248243
A string identifying the data type.
249244

pandas/core/frame.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import sys
2020
import warnings
2121
from textwrap import dedent
22-
from typing import List, Union
22+
from typing import List, TypeVar, Union
2323

2424
import numpy as np
2525
import numpy.ma as ma
@@ -284,6 +284,10 @@
284284
Index(['value'], dtype='object')
285285
"""
286286

287+
288+
DF = TypeVar('DF', bound='DataFrame')
289+
290+
287291
# -----------------------------------------------------------------------
288292
# DataFrame class
289293

@@ -6243,11 +6247,9 @@ def diff(self, periods=1, axis=0):
62436247
# Function application
62446248

62456249
def _gotitem(self,
6246-
key, # type: Union[str, List[str]]
6247-
ndim, # type: int
6248-
subset=None # type: Union[Series, DataFrame, None]
6249-
):
6250-
# type: (...) -> Union[Series, DataFrame]
6250+
key: Union[str, List[str]],
6251+
ndim: int,
6252+
subset: Union[Series, DF, None] = None) -> Union[Series, DF]:
62516253
"""
62526254
Sub-classes to define. Return a sliced object.
62536255

pandas/core/groupby/groupby.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -1040,17 +1040,15 @@ def _bool_agg(self, val_test, skipna):
10401040
Shared func to call any / all Cython GroupBy implementations.
10411041
"""
10421042

1043-
def objs_to_bool(vals):
1044-
# type: (np.ndarray) -> (np.ndarray, Type)
1043+
def objs_to_bool(vals: np.ndarray) -> types.Tuple[np.ndarray, Type]:
10451044
if is_object_dtype(vals):
10461045
vals = np.array([bool(x) for x in vals])
10471046
else:
10481047
vals = vals.astype(np.bool)
10491048

10501049
return vals.view(np.uint8), np.bool
10511050

1052-
def result_to_bool(result, inference):
1053-
# type: (np.ndarray, Type) -> np.ndarray
1051+
def result_to_bool(result: np.ndarray, inference: Type) -> np.ndarray:
10541052
return result.astype(inference, copy=False)
10551053

10561054
return self._get_cythonized_result('group_any_all', self.grouper,
@@ -1738,8 +1736,8 @@ def quantile(self, q=0.5, interpolation='linear'):
17381736
b 3.0
17391737
"""
17401738

1741-
def pre_processor(vals):
1742-
# type: (np.ndarray) -> (np.ndarray, Optional[Type])
1739+
def pre_processor(vals: np.ndarray) -> \
1740+
types.Tuple[np.ndarray, Optional[Type]]:
17431741
if is_object_dtype(vals):
17441742
raise TypeError("'quantile' cannot be performed against "
17451743
"'object' dtypes!")
@@ -1753,8 +1751,8 @@ def pre_processor(vals):
17531751

17541752
return vals, inference
17551753

1756-
def post_processor(vals, inference):
1757-
# type: (np.ndarray, Optional[Type]) -> np.ndarray
1754+
def post_processor(vals: np.ndarray, inference: Optional[Type]) -> \
1755+
np.ndarray:
17581756
if inference:
17591757
# Check for edge case
17601758
if not (is_integer_dtype(inference) and

pandas/core/indexes/base.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3632,8 +3632,7 @@ def values(self):
36323632
return self._data.view(np.ndarray)
36333633

36343634
@property
3635-
def _values(self):
3636-
# type: () -> Union[ExtensionArray, Index, np.ndarray]
3635+
def _values(self) -> Union[ExtensionArray, Index, np.ndarray]:
36373636
# TODO(EA): remove index types as they become extension arrays
36383637
"""
36393638
The best array representation.

pandas/core/indexes/datetimelike.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class DatetimeIndexOpsMixin(ExtensionOpsMixin):
5656
"""
5757
common ops mixin to support a unified interface datetimelike Index
5858
"""
59-
_data = None # type: DatetimeLikeArrayMixin
59+
_data: DatetimeLikeArrayMixin = None
6060

6161
# DatetimeLikeArrayMixin assumes subclasses are mutable, so these are
6262
# properties there. They can be made into cache_readonly for Index
@@ -129,8 +129,7 @@ def _ndarray_values(self):
129129
# Abstract data attributes
130130

131131
@property
132-
def values(self):
133-
# type: () -> np.ndarray
132+
def values(self) -> np.ndarray:
134133
# Note: PeriodArray overrides this to return an ndarray of objects.
135134
return self._data._data
136135

pandas/core/indexes/period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class PeriodIndex(DatetimeIndexOpsMixin, Int64Index, PeriodDelegateMixin):
170170
_is_numeric_dtype = False
171171
_infer_as_myclass = True
172172

173-
_data = None # type: PeriodArray
173+
_data: PeriodArray = None
174174

175175
_engine_type = libindex.PeriodEngine
176176

pandas/core/internals/blocks.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import functools
44
import inspect
55
import re
6-
from typing import Any, List
6+
from typing import Any, List, Optional, TypeVar
77
import warnings
88

99
import numpy as np
@@ -1646,6 +1646,9 @@ def _get_unstack_items(self, unstacker, new_columns):
16461646
return new_placement, new_values, mask
16471647

16481648

1649+
EB = TypeVar('EB', bound='ExtensionBlock')
1650+
1651+
16491652
class ExtensionBlock(NonConsolidatableMixIn, Block):
16501653
"""Block for holding extension types.
16511654
@@ -1828,10 +1831,9 @@ def interpolate(self, method='pad', axis=0, inplace=False, limit=None,
18281831
placement=self.mgr_locs)
18291832

18301833
def shift(self,
1831-
periods, # type: int
1832-
axis=0, # type: libinternals.BlockPlacement
1833-
fill_value=None): # type: Any
1834-
# type: (...) -> List[ExtensionBlock]
1834+
periods: int,
1835+
axis: libinternals.BlockPlacement = 0,
1836+
fill_value: Optional[Any] = None) -> List[EB]:
18351837
"""
18361838
Shift the block by `periods`.
18371839

pandas/core/internals/managers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1863,8 +1863,8 @@ def _shape_compat(x):
18631863
return stacked, placement
18641864

18651865

1866-
def _interleaved_dtype(blocks):
1867-
# type: (List[Block]) -> Optional[Union[np.dtype, ExtensionDtype]]
1866+
def _interleaved_dtype(blocks: List[Block]) \
1867+
-> Optional[Union[np.dtype, ExtensionDtype]]:
18681868
"""Find the common dtype for `blocks`.
18691869
18701870
Parameters

0 commit comments

Comments
 (0)