Skip to content
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

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

Merged
merged 16 commits into from
Mar 30, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
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
10 changes: 5 additions & 5 deletions pandas/core/arrays/array_.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
from pandas.core.dtypes.common import (
is_datetime64_ns_dtype, is_extension_array_dtype, is_timedelta64_ns_dtype)
from pandas.core.dtypes.dtypes import ExtensionDtype, registry
from pandas.core.dtypes.generic import ABCExtensionArray

from pandas import compat


def array(data, # type: Sequence[object]
dtype=None, # type: Optional[Union[str, np.dtype, ExtensionDtype]]
copy=True, # type: bool
):
# type: (...) -> ExtensionArray
def array(data: Sequence[object],
dtype: Optional[Union[str, np.dtype, ExtensionDtype]] = None,
copy: bool = True,
) -> ABCExtensionArray:
"""
Create an array.

Expand Down
72 changes: 37 additions & 35 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

from pandas.core.dtypes.common import is_list_like
from pandas.core.dtypes.dtypes import ExtensionDtype
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
from pandas.core.dtypes.generic import (
ABCExtensionArray, ABCIndexClass, ABCSeries)
from pandas.core.dtypes.missing import isna

from pandas.core import ops
Expand Down Expand Up @@ -214,8 +215,7 @@ def __getitem__(self, item):
"""
raise AbstractMethodError(self)

def __setitem__(self, key, value):
# type: (Union[int, np.ndarray], Any) -> None
def __setitem__(self, key: Union[int, np.ndarray], value: Any) -> None:
"""
Set one or more values inplace.

Expand Down Expand Up @@ -262,8 +262,7 @@ def __setitem__(self, key, value):
type(self), '__setitem__')
)

def __len__(self):
# type: () -> int
def __len__(self) -> int:
"""
Length of this array

Expand All @@ -287,32 +286,28 @@ def __iter__(self):
# Required attributes
# ------------------------------------------------------------------------
@property
def dtype(self):
# type: () -> ExtensionDtype
def dtype(self) -> ExtensionDtype:
"""
An instance of 'ExtensionDtype'.
"""
raise AbstractMethodError(self)

@property
def shape(self):
# type: () -> Tuple[int, ...]
def shape(self) -> Tuple[int, ...]:
"""
Return a tuple of the array dimensions.
"""
return (len(self),)

@property
def ndim(self):
# type: () -> int
def ndim(self) -> int:
"""
Extension Arrays are only allowed to be 1-dimensional.
"""
return 1

@property
def nbytes(self):
# type: () -> int
def nbytes(self) -> int:
"""
The number of bytes needed to store this object in memory.
"""
Expand Down Expand Up @@ -343,8 +338,7 @@ def astype(self, dtype, copy=True):
"""
return np.array(self, dtype=dtype, copy=copy)

def isna(self):
# type: () -> Union[ExtensionArray, np.ndarray]
def isna(self) -> Union[ABCExtensionArray, np.ndarray]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is a the first stab at ArrayLike (e.g. to put into pandas.typing)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a reasonable place to start.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are ok here for now, mainly adding things that we should add

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed this would be great @gwrome do you mind opening up an issue as a follow up?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. I should be able to do that this weekend.

"""
A 1-D array indicating if each value is missing.

Expand All @@ -366,8 +360,7 @@ def isna(self):
"""
raise AbstractMethodError(self)

def _values_for_argsort(self):
# type: () -> np.ndarray
def _values_for_argsort(self) -> np.ndarray:
"""
Return values for sorting.

Expand Down Expand Up @@ -482,8 +475,11 @@ def dropna(self):
"""
return self[~self.isna()]

def shift(self, periods=1, fill_value=None):
# type: (int, object) -> ExtensionArray
def shift(
self,
periods: int = 1,
fill_value: object = None,
) -> 'ExtensionArray':
"""
Shift values by desired number.

Expand Down Expand Up @@ -598,8 +594,7 @@ def searchsorted(self, value, side="left", sorter=None):
arr = self.astype(object)
return arr.searchsorted(value, side=side, sorter=sorter)

def _values_for_factorize(self):
# type: () -> Tuple[np.ndarray, Any]
def _values_for_factorize(self) -> Tuple[np.ndarray, Any]:
"""
Return an array and missing value suitable for factorization.

Expand All @@ -623,8 +618,10 @@ def _values_for_factorize(self):
"""
return self.astype(object), np.nan

def factorize(self, na_sentinel=-1):
# type: (int) -> Tuple[np.ndarray, ExtensionArray]
def factorize(
self,
na_sentinel: int = -1,
) -> Tuple[np.ndarray, 'ExtensionArray']:
"""
Encode the extension array as an enumerated type.

Expand Down Expand Up @@ -726,8 +723,12 @@ def repeat(self, repeats, axis=None):
# Indexing methods
# ------------------------------------------------------------------------

def take(self, indices, allow_fill=False, fill_value=None):
# type: (Sequence[int], bool, Optional[Any]) -> ExtensionArray
def take(
self,
indices: Sequence[int],
allow_fill: bool = False,
fill_value: Any = None
) -> 'ExtensionArray':
"""
Take elements from an array.

Expand Down Expand Up @@ -816,8 +817,7 @@ def take(self, indices, allow_fill=False, fill_value=None):
# pandas.api.extensions.take
raise AbstractMethodError(self)

def copy(self, deep=False):
# type: (bool) -> ExtensionArray
def copy(self, deep: bool = False) -> 'ExtensionArray':
"""
Return a copy of the array.

Expand Down Expand Up @@ -853,8 +853,10 @@ def __repr__(self):
length=len(self),
dtype=self.dtype)

def _formatter(self, boxed=False):
# type: (bool) -> Callable[[Any], Optional[str]]
def _formatter(
self,
boxed: bool = False,
) -> Callable[[Any], Optional[str]]:
"""Formatting function for scalar values.

This is used in the default '__repr__'. The returned formatting
Expand All @@ -881,8 +883,7 @@ def _formatter(self, boxed=False):
return str
return repr

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

@classmethod
def _concat_same_type(cls, to_concat):
# type: (Sequence[ExtensionArray]) -> ExtensionArray
def _concat_same_type(
cls,
to_concat: Sequence['ExtensionArray']
) -> 'ExtensionArray':
"""
Concatenate multiple array

Expand All @@ -921,8 +924,7 @@ def _concat_same_type(cls, to_concat):
_can_hold_na = True

@property
def _ndarray_values(self):
# type: () -> np.ndarray
def _ndarray_values(self) -> np.ndarray:
"""
Internal pandas method for lossy conversion to a NumPy ndarray.

Expand Down
36 changes: 19 additions & 17 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from datetime import datetime, timedelta
import operator
from typing import Any, Sequence, Tuple, Union
from typing import Any, Sequence, Tuple, Type, Union
import warnings

import numpy as np
Expand Down Expand Up @@ -58,8 +58,7 @@ def _get_attributes_dict(self):
return {k: getattr(self, k, None) for k in self._attributes}

@property
def _scalar_type(self):
# type: () -> Union[type, Tuple[type]]
def _scalar_type(self) -> Union[Type, Tuple[Type]]:
"""The scalar associated with this datelike

* PeriodArray : Period
Expand All @@ -68,8 +67,10 @@ def _scalar_type(self):
"""
raise AbstractMethodError(self)

def _scalar_from_string(self, value):
# type: (str) -> Union[Period, Timestamp, Timedelta, NaTType]
def _scalar_from_string(
self,
value: str,
) -> Union[Period, Timestamp, Timedelta, NaTType]:
"""
Construct a scalar type from a string.

Expand All @@ -89,8 +90,10 @@ def _scalar_from_string(self, value):
"""
raise AbstractMethodError(self)

def _unbox_scalar(self, value):
# type: (Union[Period, Timestamp, Timedelta, NaTType]) -> int
def _unbox_scalar(
self,
value: Union[Period, Timestamp, Timedelta, NaTType],
) -> int:
"""
Unbox the integer value of a scalar `value`.

Expand All @@ -109,8 +112,10 @@ def _unbox_scalar(self, value):
"""
raise AbstractMethodError(self)

def _check_compatible_with(self, other):
# type: (Union[Period, Timestamp, Timedelta, NaTType]) -> None
def _check_compatible_with(
self,
other: Union[Period, Timestamp, Timedelta, NaTType],
) -> None:
"""
Verify that `self` and `other` are compatible.

Expand Down Expand Up @@ -350,8 +355,7 @@ def __iter__(self):
return (self._box_func(v) for v in self.asi8)

@property
def asi8(self):
# type: () -> np.ndarray
def asi8(self) -> np.ndarray:
"""
Integer representation of the values.

Expand Down Expand Up @@ -402,8 +406,7 @@ def shape(self):
return (len(self),)

@property
def size(self):
# type: () -> int
def size(self) -> int:
"""The number of elements in this array."""
return np.prod(self.shape)

Expand Down Expand Up @@ -461,10 +464,9 @@ def __getitem__(self, key):

def __setitem__(
self,
key, # type: Union[int, Sequence[int], Sequence[bool], slice]
value, # type: Union[NaTType, Any, Sequence[Any]]
):
# type: (...) -> None
key: Union[int, Sequence[int], Sequence[bool], slice],
value: Union[NaTType, Any, Sequence[Any]]
) -> None:
# I'm fudging the types a bit here. "Any" above really depends
# on type(self). For PeriodArray, it's Period (or stuff coercible
# to a period in from_sequence). For DatetimeArray, it's Timestamp...
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,7 @@ def _box_func(self):
return lambda x: Timestamp(x, freq=self.freq, tz=self.tz)

@property
def dtype(self):
# type: () -> Union[np.dtype, DatetimeTZDtype]
def dtype(self) -> Union[np.dtype, DatetimeTZDtype]:
"""
The dtype for the DatetimeArray.

Expand Down
6 changes: 2 additions & 4 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,7 @@ def astype(self, dtype, copy=True):
return astype_nansafe(data, dtype, copy=None)

@property
def _ndarray_values(self):
# type: () -> np.ndarray
def _ndarray_values(self) -> np.ndarray:
"""Internal pandas method for lossy conversion to a NumPy ndarray.

This method is not part of the pandas interface.
Expand Down Expand Up @@ -509,8 +508,7 @@ def value_counts(self, dropna=True):

return Series(array, index=index)

def _values_for_argsort(self):
# type: () -> np.ndarray
def _values_for_argsort(self) -> np.ndarray:
"""Return values for sorting.

Returns
Expand Down
Loading