8000 #25790 Updating type hints to Python3 syntax in pandas/core/array by gwrome · Pull Request #25829 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

#25790 Updating type hints to Python3 syntax in pandas/core/array #25829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Mar 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
#25790 Updated type hints to Python3 style in files outside core/arrays/
  • Loading branch information
gwrome committed Mar 22, 2019
commit a0b8c4cf205623a76f0e9e37bc20c73ee49e96af
6 changes: 2 additions & 4 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,8 +786,7 @@ def base(self):
return self.values.base

@property
def array(self):
# type: () -> ExtensionArray
def array(self) -> ExtensionArray:
"""
The ExtensionArray of the data backing this Series or Index.

Expand Down Expand Up @@ -962,8 +961,7 @@ def to_numpy(self, dtype=None, copy=False):
return result

@property
def _ndarray_values(self):
# type: () -> np.ndarray
def _ndarray_values(self) -> np.ndarray:
"""
The data as an ndarray, possibly losing information.

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ def maybe_box_datetimelike(value):
values_from_object = lib.values_from_object


def is_bool_indexer(key):
# type: (Any) -> bool
def is_bool_indexer(key: Any) -> bool:
"""
Check whether `key` is a valid boolean indexer.

Expand Down
17 changes: 6 additions & 11 deletions pandas/core/dtypes/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Extend pandas with custom array types"""
from typing import List, Optional, Type
from typing import List, Optional

import numpy as np

Expand Down Expand Up @@ -65,8 +65,7 @@ def __ne__(self, other):
return not self.__eq__(other)

@property
def names(self):
# type: () -> Optional[List[str]]
def names(self) -> Optional[List[str]]:
"""Ordered list of field names, or None if there are no fields.

This is for compatibility with NumPy arrays, and may be removed in the
Expand Down Expand Up @@ -116,8 +115,7 @@ def is_dtype(cls, dtype):
return False

@property
def _is_numeric(self):
# type: () -> bool
def _is_numeric(self) -> bool:
"""
Whether columns with this dtype should be considered numeric.

Expand All @@ -128,8 +126,7 @@ def _is_numeric(self):
return False

@property
def _is_boolean(self):
# type: () -> bool
def _is_boolean(self) -> bool:
"""
Whether this dtype should be considered boolean.

Expand Down Expand Up @@ -212,8 +209,7 @@ def __str__(self):
return self.name

@property
def type(self):
# type: () -> Type
def type(self) -> bool:
"""
The scalar type for the array, e.g. ``int``

Expand Down Expand Up @@ -242,8 +238,7 @@ def kind(self):
return 'O'

@property
def name(self):
# type: () -> str
def name(self) -> str:
"""
A string identifying the data type.

Expand Down
14 changes: 8 additions & 6 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import sys
import warnings
from textwrap import dedent
from typing import List, Union
from typing import List, TypeVar, Union

import numpy as np
import numpy.ma as ma
Expand Down Expand Up @@ -284,6 +284,10 @@
Index(['value'], dtype='object')
"""


DF = TypeVar('DF', bound='DataFrame')


# -----------------------------------------------------------------------
# DataFrame class

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

def _gotitem(self,
key, # type: Union[str, List[str]]
ndim, # type: int
subset=None # type: Union[Series, DataFrame, None]
):
# type: (...) -> Union[Series, DataFrame]
key: Union[str, List[str]],
ndim: int,
subset: Union[Series, DF, None] = None) -> Union[Series, DF]:
"""
Sub-classes to define. Return a sliced object.

Expand Down
14 changes: 6 additions & 8 deletions pandas/core/groupby/groupby.py
< 2364 td class="blob-code blob-code-context js-file-line"> return vals.view(np.uint8), np.bool
Original file line number Diff line number Diff line change
Expand Up @@ -1040,17 +1040,15 @@ def _bool_agg(self, val_test, skipna):
Shared func to call any / all Cython GroupBy implementations.
"""

def objs_to_bool(vals):
# type: (np.ndarray) -> (np.ndarray, Type)
def objs_to_bool(vals: np.ndarray) -> types.Tuple[np.ndarray, Type]:
if is_object_dtype(vals):
vals = np.array([bool(x) for x in vals])
else:
vals = vals.astype(np.bool)


def result_to_bool(result, inference):
# type: (np.ndarray, Type) -> np.ndarray
def result_to_bool(result: np.ndarray, inference: Type) -> np.ndarray:
return result.astype(inference, copy=False)

return self._get_cythonized_result('group_any_all', self.grouper,
Expand Down Expand Up @@ -1738,8 +1736,8 @@ def quantile(self, q=0.5, interpolation='linear'):
b 3.0
"""

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

return vals, inference

def post_processor(vals, inference):
# type: (np.ndarray, Optional[Type]) -> np.ndarray
def post_processor(vals: np.ndarray, inference: Optional[Type]) -> \
np.ndarray:
if inference:
# Check for edge case
if not (is_integer_dtype(inference) and
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3632,8 +3632,7 @@ def values(self):
return self._data.view(np.ndarray)

@property
def _values(self):
# type: () -> Union[ExtensionArray, Index, np.ndarray]
def _values(self) -> Union[ExtensionArray, Index, np.ndarray]:
# TODO(EA): remove index types as they become extension arrays
"""
The best array representation.
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class DatetimeIndexOpsMixin(ExtensionOpsMixin):
"""
common ops mixin to support a unified interface datetimelike Index
"""
_data = None # type: DatetimeLikeArrayMixin
_data: DatetimeLikeArrayMixin = None

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

@property
def values(self):
# type: () -> np.ndarray
def values(self) -> np.ndarray:
# Note: PeriodArray overrides this to return an ndarray of objects.
return self._data._data

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class PeriodIndex(DatetimeIndexOpsMixin, Int64Index, PeriodDelegateMixin):
_is_numeric_dtype = False
_infer_as_myclass = True

_data = None # type: PeriodArray
_data: PeriodArray = None

_engine_type = libindex.PeriodEngine

Expand Down
12 changes: 7 additions & 5 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import functools
import inspect
import re
from typing import Any, List
from typing import Any, List, Optional, TypeVar
import warnings

import numpy as np
Expand Down Expand Up @@ -1646,6 +1646,9 @@ def _get_unstack_items(self, unstacker, new_columns):
return new_placement, new_values, mask


EB = TypeVar('EB', bound='ExtensionBlock')


class ExtensionBlock(NonConsolidatableMixIn, Block):
"""Block for holding extension types.

Expand Down Expand Up @@ -1828,10 +1831,9 @@ def interpolate(self, method='pad', axis=0, inplace=False, limit=None,
placement=self.mgr_locs)

def shift(self,
periods, # type: int
axis=0, # type: libinternals.BlockPlacement
fill_value=None): # type: Any
# type: (...) -> List[ExtensionBlock]
periods: int,
axis: libinternals.BlockPlacement = 0,
fill_value: Optional[Any] = None) -> List[EB]:
"""
Shift the block by `periods`.

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1863,8 +1863,8 @@ def _shape_compat(x):
return stacked, placement


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

Parameters
Expand Down
0