Skip to content

Commit a56a95d

Browse files
committed
Fix empty categorical astype
1 parent 599042f commit a56a95d

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

python/cudf/cudf/core/column/column.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,12 +2290,12 @@ def cast(self, dtype: DtypeObj) -> ColumnBase:
22902290
def astype(self, dtype: DtypeObj, copy: bool | None = False) -> ColumnBase:
22912291
if self.dtype == dtype:
22922292
result = self
2293+
elif isinstance(dtype, CategoricalDtype):
2294+
result = self.as_categorical_column(dtype)
22932295
elif len(self) == 0:
22942296
result = column_empty(0, dtype=dtype)
22952297
else:
2296-
if isinstance(dtype, CategoricalDtype):
2297-
result = self.as_categorical_column(dtype)
2298-
elif is_dtype_obj_interval(dtype):
2298+
if is_dtype_obj_interval(dtype):
22992299
result = self.as_interval_column(dtype) # type: ignore[arg-type]
23002300
elif is_dtype_obj_list(dtype) or is_dtype_obj_struct(dtype):
23012301
if isinstance(dtype, pd.ArrowDtype):

python/cudf/cudf/tests/series/methods/test_astype.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,12 @@ def test_cast_float_nan_to_bool_pandas_compat():
145145
def test_empty_astype_always_castable(type1, type2, as_dtype, copy):
146146
ser = cudf.Series([], dtype=as_dtype(type1))
147147
result = ser.astype(as_dtype(type2), copy=copy)
148-
expected = cudf.Series([], dtype=as_dtype(type2))
148+
if type2 == "category":
149+
# Empty astype to category inherits the source dtype as the
150+
# categories dtype, matching pandas behavior.
151+
expected = cudf.Series([], dtype=result.dtype)
152+
else:
153+
expected = cudf.Series([], dtype=as_dtype(type2))
149154
assert_eq(result, expected)
150155
if not copy and cudf.dtype(type1) == cudf.dtype(type2):
151156
assert ser._column is result._column

0 commit comments

Comments
 (0)