Skip to content

Commit

Permalink
moved aliases to dtypes.py
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahyurick committed Aug 2, 2021
1 parent b65d32d commit 38e9893
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
28 changes: 7 additions & 21 deletions python/cudf/cudf/core/column/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
min_unsigned_type,
np_to_pa_dtype,
pandas_dtypes_to_cudf_dtypes,
pandas_dtypes_alias_to_cudf_alias,
)
from cudf.utils.utils import mask_dtype

Expand Down Expand Up @@ -881,14 +882,13 @@ def astype(self, dtype: Dtype, **kwargs) -> ColumnBase:
if is_categorical_dtype(dtype):
return self.as_categorical_column(dtype, **kwargs)

dtype = pandas_dtypes_to_cudf_dtypes.get(dtype, dtype)
dtype = (
pandas_dtypes_alias_to_cudf_alias.get(dtype, dtype)
if isinstance(dtype, str)
else pandas_dtypes_to_cudf_dtypes.get(dtype, dtype)
)
if _is_non_decimal_numeric_dtype(dtype):
try:
return self.as_numerical_column(dtype, **kwargs)
except TypeError:
return self.as_numerical_column(
self.convert_alias(dtype), **kwargs
)
return self.as_numerical_column(dtype, **kwargs)
elif is_categorical_dtype(dtype):
return self.as_categorical_column(dtype, **kwargs)
elif pandas_dtype(dtype).type in {
Expand Down Expand Up @@ -968,20 +968,6 @@ def as_numerical_column(
) -> "cudf.core.column.NumericalColumn":
raise NotImplementedError

def convert_alias(self, dtype):
aliases = {
"UInt8": "uint8",
"UInt16": "uint16",
"UInt32": "uint32",
"UInt64": "uint64",
"Int8": "int8",
"Int16": "int16",
"Int32": "int32",
"Int64": "int64",
"boolean": "bool",
}
return aliases[dtype]

def as_datetime_column(
self, dtype: Dtype, **kwargs
) -> "cudf.core.column.DatetimeColumn":
Expand Down
2 changes: 2 additions & 0 deletions python/cudf/cudf/tests/test_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ def test_concatenate_large_column_strings():
("Int32", "int32"),
("Int64", "int64"),
("boolean", "bool"),
("Float32", "float32"),
("Float64", "float64"),
],
)
@pytest.mark.parametrize(
Expand Down
14 changes: 14 additions & 0 deletions python/cudf/cudf/utils/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,25 @@
pd.StringDtype(): np.dtype("object"),
}

pandas_dtypes_alias_to_cudf_alias = {
"UInt8": "uint8",
"UInt16": "uint16",
"UInt32": "uint32",
"UInt64": "uint64",
"Int8": "int8",
"Int16": "int16",
"Int32": "int32",
"Int64": "int64",
"boolean": "bool",
}

if PANDAS_GE_120:
cudf_dtypes_to_pandas_dtypes[np.dtype("float32")] = pd.Float32Dtype()
cudf_dtypes_to_pandas_dtypes[np.dtype("float64")] = pd.Float64Dtype()
pandas_dtypes_to_cudf_dtypes[pd.Float32Dtype()] = np.dtype("float32")
pandas_dtypes_to_cudf_dtypes[pd.Float64Dtype()] = np.dtype("float64")
pandas_dtypes_alias_to_cudf_alias["Float32"] = "float32"
pandas_dtypes_alias_to_cudf_alias["Float64"] = "float64"

SIGNED_INTEGER_TYPES = {"int8", "int16", "int32", "int64"}
UNSIGNED_TYPES = {"uint8", "uint16", "uint32", "uint64"}
Expand Down

0 comments on commit 38e9893

Please sign in to comment.