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

Replace _is_datetime64tz/interval_dtype with isinstance #14943

Merged
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
18 changes: 13 additions & 5 deletions python/cudf/cudf/core/column/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@
from cudf._typing import ColumnLike, Dtype, ScalarLike
from cudf.api.types import (
_is_categorical_dtype,
_is_datetime64tz_dtype,
_is_interval_dtype,
_is_non_decimal_numeric_dtype,
_is_pandas_nullable_extension_dtype,
infer_dtype,
Expand Down Expand Up @@ -2263,9 +2261,17 @@ def as_column(
np_type = None
try:
if dtype is not None:
if _is_categorical_dtype(dtype) or _is_interval_dtype(dtype):
if dtype in {"category", "interval"} or isinstance(
dtype,
(
cudf.CategoricalDtype,
cudf.IntervalDtype,
pd.IntervalDtype,
pd.CategoricalDtype,
),
):
raise TypeError
if _is_datetime64tz_dtype(dtype):
if isinstance(dtype, pd.DatetimeTZDtype):
raise NotImplementedError(
"Use `tz_localize()` to construct "
"timezone aware data."
Expand Down Expand Up @@ -2413,7 +2419,9 @@ def as_column(
elif np_type == np.str_:
sr = pd.Series(arbitrary, dtype="str")
data = as_column(sr, nan_as_null=nan_as_null)
elif _is_interval_dtype(dtype):
elif dtype == "interval" or isinstance(
dtype, (pd.IntervalDtype, cudf.IntervalDtype)
):
sr = pd.Series(arbitrary, dtype="interval")
data = as_column(sr, nan_as_null=nan_as_null, dtype=dtype)
elif (
Expand Down
9 changes: 2 additions & 7 deletions python/cudf/cudf/core/column/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@
DtypeObj,
ScalarLike,
)
from cudf.api.types import (
_is_datetime64tz_dtype,
is_datetime64_dtype,
is_scalar,
is_timedelta64_dtype,
)
from cudf.api.types import is_datetime64_dtype, is_scalar, is_timedelta64_dtype
from cudf.core._compat import PANDAS_GE_200, PANDAS_GE_220
from cudf.core.buffer import Buffer, cuda_array_interface_wrapper
from cudf.core.column import ColumnBase, as_column, column, string
Expand Down Expand Up @@ -702,7 +697,7 @@ def can_cast_safely(self, to_dtype: Dtype) -> bool:
return False

def _with_type_metadata(self, dtype):
if _is_datetime64tz_dtype(dtype):
if isinstance(dtype, pd.DatetimeTZDtype):
return DatetimeTZColumn(
data=self.base_data,
dtype=dtype,
Expand Down
10 changes: 1 addition & 9 deletions python/cudf/cudf/core/column/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import pyarrow as pa

import cudf
from cudf.api.types import _is_interval_dtype
from cudf.core.column import StructColumn
from cudf.core.dtypes import CategoricalDtype, IntervalDtype

Expand Down Expand Up @@ -94,20 +93,13 @@ def as_interval_column(self, dtype):
new_struct = self._get_decategorized_column()
return IntervalColumn.from_struct_column(new_struct)
else:
# a user can directly input the string `interval` as the dtype
# when creating an interval series or interval dataframe
if _is_interval_dtype(dtype):
dtype = IntervalDtype(
self.dtype.subtype, self.dtype.closed
)
children = self.children
return IntervalColumn(
size=self.size,
dtype=dtype,
mask=self.mask,
offset=self.offset,
null_count=self.null_count,
children=children,
children=self.children,
)
else:
raise ValueError("dtype must be IntervalDtype")
Expand Down
5 changes: 4 additions & 1 deletion python/cudf/cudf/core/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ def to_pandas(self) -> pd.CategoricalDtype:
def _init_categories(self, categories: Any):
if categories is None:
return categories
if len(categories) == 0 and not _is_interval_dtype(categories):
if len(categories) == 0 and not isinstance(
getattr(categories, "dtype", None),
(cudf.IntervalDtype, pd.IntervalDtype),
):
dtype = "object" # type: Any
else:
dtype = None
Expand Down
Loading