Skip to content

Sync Fork from Upstream Repo #81

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 7 commits into from
Mar 7, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ Backwards incompatible API changes
now raise a ``TypeError`` if a not-accepted keyword argument is passed into it.
Previously a ``UnsupportedFunctionCall`` was raised (``AssertionError`` if ``min_count`` passed into :meth:`~DataFrameGroupby.median``) (:issue:`31485`)
- :meth:`DataFrame.at` and :meth:`Series.at` will raise a ``TypeError`` instead of a ``ValueError`` if an incompatible key is passed, and ``KeyError`` if a missing key is passed, matching the behavior of ``.loc[]`` (:issue:`31722`)
- Passing an integer dtype other than ``int64`` to ``np.array(period_index, dtype=...)`` will now raise ``TypeError`` instead of incorrectly using ``int64`` (:issue:`32255`)
-

.. _whatsnew_110.api_breaking.indexing_raises_key_errors:

Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ class Timedelta(_Timedelta):

Possible values:

* 'Y', 'M', 'W', 'D', 'T', 'S', 'L', 'U', or 'N'
* 'W', 'D', 'T', 'S', 'L', 'U', or 'N'
* 'days' or 'day'
* 'hours', 'hour', 'hr', or 'h'
* 'minutes', 'minute', 'min', or 'm'
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ def tz_localize(self, tz, ambiguous="raise", nonexistent="raise"):
# ----------------------------------------------------------------
# Conversion Methods - Vectorized analogues of Timestamp methods

def to_pydatetime(self):
def to_pydatetime(self) -> np.ndarray:
"""
Return Datetime Array/Index as object ndarray of datetime.datetime
objects.
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,12 @@ def freq(self):
return self.dtype.freq

def __array__(self, dtype=None) -> np.ndarray:
# overriding DatetimelikeArray
if dtype == "i8":
return self.asi8
elif dtype == bool:
return ~self._isnan

# This will raise TypeErorr for non-object dtypes
return np.array(list(self), dtype=object)

def __arrow_array__(self, type=None):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ def total_seconds(self):
"""
return self._maybe_mask_results(1e-9 * self.asi8, fill_value=None)

def to_pytimedelta(self):
def to_pytimedelta(self) -> np.ndarray:
"""
Return Timedelta Array/Index as object ndarray of datetime.timedelta
objects.
Expand Down
76 changes: 2 additions & 74 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import numpy as np

from pandas._libs import algos, lib
from pandas._libs import algos
from pandas._libs.tslibs import conversion
from pandas._typing import ArrayLike, DtypeObj

Expand All @@ -19,14 +19,7 @@
PeriodDtype,
registry,
)
from pandas.core.dtypes.generic import (
ABCCategorical,
ABCDatetimeIndex,
ABCIndexClass,
ABCPeriodArray,
ABCPeriodIndex,
ABCSeries,
)
from pandas.core.dtypes.generic import ABCCategorical, ABCIndexClass
from pandas.core.dtypes.inference import ( # noqa:F401
is_array_like,
is_bool,
Expand Down Expand Up @@ -606,71 +599,6 @@ def is_excluded_dtype(dtype) -> bool:
return _is_dtype(arr_or_dtype, condition)


def is_period_arraylike(arr) -> bool:
"""
Check whether an array-like is a periodical array-like or PeriodIndex.

Parameters
----------
arr : array-like
The array-like to check.

Returns
-------
boolean
Whether or not the array-like is a periodical array-like or
PeriodIndex instance.

Examples
--------
>>> is_period_arraylike([1, 2, 3])
False
>>> is_period_arraylike(pd.Index([1, 2, 3]))
False
>>> is_period_arraylike(pd.PeriodIndex(["2017-01-01"], freq="D"))
True
"""
if isinstance(arr, (ABCPeriodIndex, ABCPeriodArray)):
return True
elif isinstance(arr, (np.ndarray, ABCSeries)):
return is_period_dtype(arr.dtype)
return getattr(arr, "inferred_type", None) == "period"


def is_datetime_arraylike(arr) -> bool:
"""
Check whether an array-like is a datetime array-like or DatetimeIndex.

Parameters
----------
arr : array-like
The array-like to check.

Returns
-------
boolean
Whether or not the array-like is a datetime array-like or
DatetimeIndex.

Examples
--------
>>> is_datetime_arraylike([1, 2, 3])
False
>>> is_datetime_arraylike(pd.Index([1, 2, 3]))
False
>>> is_datetime_arraylike(pd.DatetimeIndex([1, 2, 3]))
True
"""
if isinstance(arr, ABCDatetimeIndex):
return True
elif isinstance(arr, (np.ndarray, ABCSeries)):
return (
is_object_dtype(arr.dtype)
and lib.infer_dtype(arr, skipna=False) == "datetime"
)
return getattr(arr, "inferred_type", None) == "datetime"


def is_dtype_equal(source, target) -> bool:
"""
Check if two dtypes are equal.
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
is_number,
is_numeric_dtype,
is_object_dtype,
is_period_arraylike,
is_re_compilable,
is_scalar,
is_timedelta64_dtype,
Expand Down Expand Up @@ -1342,7 +1341,7 @@ def __neg__(self):

def __pos__(self):
values = self._values
if is_bool_dtype(values) or is_period_arraylike(values):
if is_bool_dtype(values):
arr = values
elif (
is_numeric_dtype(values)
Expand Down
30 changes: 14 additions & 16 deletions pandas/core/indexes/accessors.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
"""
datetimelike delegation
"""
from typing import TYPE_CHECKING

import numpy as np

from pandas.core.dtypes.common import (
is_categorical_dtype,
is_datetime64_dtype,
is_datetime64tz_dtype,
is_datetime_arraylike,
is_integer_dtype,
is_list_like,
is_period_arraylike,
is_period_dtype,
is_timedelta64_dtype,
)
from pandas.core.dtypes.generic import ABCSeries
Expand All @@ -21,9 +22,12 @@
from pandas.core.indexes.datetimes import DatetimeIndex
from pandas.core.indexes.timedeltas import TimedeltaIndex

if TYPE_CHECKING:
from pandas import Series # noqa:F401


class Properties(PandasDelegate, PandasObject, NoNewAttributesMixin):
def __init__(self, data, orig):
def __init__(self, data: "Series", orig):
if not isinstance(data, ABCSeries):
raise TypeError(
f"cannot convert an object of type {type(data)} to a datetimelike index"
Expand All @@ -45,12 +49,8 @@ def _get_values(self):
elif is_timedelta64_dtype(data.dtype):
return TimedeltaIndex(data, copy=False, name=self.name)

else:
if is_period_arraylike(data):
# TODO: use to_period_array
return PeriodArray(data, copy=False)
if is_datetime_arraylike(data):
return DatetimeIndex(data, copy=False, name=self.name)
elif is_period_dtype(data):
return PeriodArray(data, copy=False)

raise TypeError(
f"cannot convert an object of type {type(data)} to a datetimelike index"
Expand Down Expand Up @@ -137,7 +137,7 @@ class DatetimeProperties(Properties):
Raises TypeError if the Series does not contain datetimelike values.
"""

def to_pydatetime(self):
def to_pydatetime(self) -> np.ndarray:
"""
Return the data as an array of native Python datetime objects.

Expand Down Expand Up @@ -209,7 +209,7 @@ class TimedeltaProperties(Properties):
Raises TypeError if the Series does not contain datetimelike values.
"""

def to_pytimedelta(self):
def to_pytimedelta(self) -> np.ndarray:
"""
Return an array of native `datetime.timedelta` objects.

Expand Down Expand Up @@ -271,7 +271,7 @@ def components(self):
2 0 0 0 2 0 0 0
3 0 0 0 3 0 0 0
4 0 0 0 4 0 0 0
""" # noqa: E501
"""
return self._get_values().components.set_index(self._parent.index)

@property
Expand Down Expand Up @@ -303,7 +303,7 @@ class PeriodProperties(Properties):
class CombinedDatetimelikeProperties(
DatetimeProperties, TimedeltaProperties, PeriodProperties
):
def __new__(cls, data):
def __new__(cls, data: "Series"):
# CombinedDatetimelikeProperties isn't really instantiated. Instead
# we need to choose which parent (datetime or timedelta) is
# appropriate. Since we're checking the dtypes anyway, we'll just
Expand All @@ -330,9 +330,7 @@ def __new__(cls, data):
return DatetimeProperties(data, orig)
elif is_timedelta64_dtype(data.dtype):
return TimedeltaProperties(data, orig)
elif is_period_arraylike(data):
elif is_period_dtype(data):
return PeriodProperties(data, orig)
elif is_datetime_arraylike(data):
return DatetimeProperties(data, orig)

raise AttributeError("Can only use .dt accessor with datetimelike values")
4 changes: 0 additions & 4 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,6 @@ def __contains__(self, key: Any) -> bool:
hash(key)
return contains(self, key, container=self._engine)

def __array__(self, dtype=None) -> np.ndarray:
""" the array interface, return my values """
return np.array(self._data, dtype=dtype)

@Appender(Index.astype.__doc__)
def astype(self, dtype, copy=True):
if is_interval_dtype(dtype):
Expand Down
3 changes: 0 additions & 3 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,6 @@ def _simple_new(cls, values, name=None, freq=None, tz=None, dtype=None):

# --------------------------------------------------------------------

def __array__(self, dtype=None) -> np.ndarray:
return np.asarray(self._data, dtype=dtype)

@cache_readonly
def _is_dates_only(self) -> bool:
"""
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/indexes/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ def __iter__(self):

# ---------------------------------------------------------------------

def __array__(self, dtype=None) -> np.ndarray:
return np.asarray(self._data, dtype=dtype)

@property
def _ndarray_values(self) -> np.ndarray:
return self._data._ndarray_values
Expand Down
7 changes: 0 additions & 7 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
is_dtype_equal,
is_float,
is_integer,
is_integer_dtype,
is_object_dtype,
is_scalar,
pandas_dtype,
Expand Down Expand Up @@ -338,12 +337,6 @@ def _int64index(self) -> Int64Index:
# ------------------------------------------------------------------------
# Index Methods

def __array__(self, dtype=None) -> np.ndarray:
if is_integer_dtype(dtype):
return self.asi8
else:
return self.astype(object).values

def __array_wrap__(self, result, context=None):
"""
Gets called after a ufunc. Needs additional handling as
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def _data(self):
return self._cached_data

@cache_readonly
def _int64index(self):
def _int64index(self) -> Int64Index:
return Int64Index._simple_new(self._data, name=self.name)

def _get_data_as_items(self):
Expand Down
12 changes: 5 additions & 7 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3144,14 +3144,15 @@ def _safe_reshape(arr, new_shape):
return arr


def _putmask_smart(v, mask, n):
def _putmask_smart(v: np.ndarray, mask: np.ndarray, n) -> np.ndarray:
"""
Return a new ndarray, try to preserve dtype if possible.

Parameters
----------
v : `values`, updated in-place (array like)
mask : np.ndarray
v : np.ndarray
`values`, updated in-place.
mask : np.ndarray[bool]
Applies to both sides (array like).
n : `new values` either scalar or an array like aligned with `values`

Expand Down Expand Up @@ -3218,9 +3219,6 @@ def _putmask_preserve(nv, n):
# change the dtype if needed
dtype, _ = maybe_promote(n.dtype)

if is_extension_array_dtype(v.dtype) and is_object_dtype(dtype):
v = v._internal_get_values(dtype)
else:
v = v.astype(dtype)
v = v.astype(dtype)

return _putmask_preserve(v, n)
6 changes: 3 additions & 3 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
is_list_like,
is_numeric_v_string_like,
is_scalar,
is_sparse,
)
from pandas.core.dtypes.concat import concat_compat
from pandas.core.dtypes.dtypes import ExtensionDtype
from pandas.core.dtypes.generic import ABCExtensionArray, ABCSeries
from pandas.core.dtypes.missing import isna

import pandas.core.algorithms as algos
from pandas.core.arrays.sparse import SparseDtype
from pandas.core.base import PandasObject
from pandas.core.indexers import maybe_convert_indices
from pandas.core.indexes.api import Index, MultiIndex, ensure_index
Expand Down Expand Up @@ -843,8 +843,8 @@ def _interleave(self) -> np.ndarray:

# TODO: https://github.com/pandas-dev/pandas/issues/22791
# Give EAs some input on what happens here. Sparse needs this.
if is_sparse(dtype):
dtype = dtype.subtype # type: ignore
if isinstance(dtype, SparseDtype):
dtype = dtype.subtype
elif is_extension_array_dtype(dtype):
dtype = "object"

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ def nanskew(
Examples
--------
>>> import pandas.core.nanops as nanops
>>> s = pd.Series([1,np.nan, 1, 2])
>>> s = pd.Series([1, np.nan, 1, 2])
>>> nanops.nanskew(s)
1.7320508075688787
"""
Expand Down Expand Up @@ -1065,7 +1065,7 @@ def nankurt(
Examples
--------
>>> import pandas.core.nanops as nanops
>>> s = pd.Series([1,np.nan, 1, 3, 2])
>>> s = pd.Series([1, np.nan, 1, 3, 2])
>>> nanops.nankurt(s)
-1.2892561983471076
"""
Expand Down
Loading