Skip to content

CLN: remove is_period_arraylike, is_datetime_arraylike #32406

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 4 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
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
15 changes: 4 additions & 11 deletions pandas/core/indexes/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
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 Down Expand Up @@ -45,12 +44,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 @@ -330,9 +325,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")
12 changes: 0 additions & 12 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,18 +281,6 @@ def test_is_string_dtype():
assert com.is_string_dtype(pd.array(["a", "b"], dtype="string"))


def test_is_period_arraylike():
assert not com.is_period_arraylike([1, 2, 3])
assert not com.is_period_arraylike(pd.Index([1, 2, 3]))
assert com.is_period_arraylike(pd.PeriodIndex(["2017-01-01"], freq="D"))


def test_is_datetime_arraylike():
assert not com.is_datetime_arraylike([1, 2, 3])
assert not com.is_datetime_arraylike(pd.Index([1, 2, 3]))
assert com.is_datetime_arraylike(pd.DatetimeIndex([1, 2, 3]))


integer_dtypes: List = []


Expand Down
4 changes: 2 additions & 2 deletions pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from pandas.core.dtypes.common import (
is_datetime64_dtype,
is_period_arraylike,
is_period_dtype,
is_timedelta64_dtype,
)
from pandas.core.dtypes.generic import ABCSeries
Expand Down Expand Up @@ -270,7 +270,7 @@ def infer_freq(index, warn: bool = True) -> Optional[str]:
index = values

inferer: _FrequencyInferer
if is_period_arraylike(index):
if is_period_dtype(index):
raise TypeError(
"PeriodIndex given. Check the `freq` attribute "
"instead of using infer_freq."
Expand Down