From 09b6e45b7e02bc5964d2a32020171040f82c308b Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 19 Jan 2024 10:52:35 -1000 Subject: [PATCH] Consolidate cudf object handling in as_column (#14754) Since these paths are very similar, consolidating these paths and returning early for this case Authors: - Matthew Roeschke (https://github.com/mroeschke) Approvers: - Vyas Ramasubramani (https://github.com/vyasr) URL: https://github.com/rapidsai/cudf/pull/14754 --- python/cudf/cudf/core/column/column.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/python/cudf/cudf/core/column/column.py b/python/cudf/cudf/core/column/column.py index bc7b1ed97c0..df5d1c3879a 100644 --- a/python/cudf/cudf/core/column/column.py +++ b/python/cudf/cudf/core/column/column.py @@ -1871,6 +1871,7 @@ def as_column( If None (default), treats NaN values in arbitrary as null if there is no mask passed along with it. If True, combines the mask and NaNs to form a new validity mask. If False, leaves NaN values as is. + Only applies when arbitrary is not a cudf object (Index, Series, Column). dtype : optional Optionally typecast the constructed Column to the given dtype. @@ -1907,22 +1908,17 @@ def as_column( f'i{cudf.get_option("default_integer_bitwidth")//8}' ) if dtype is not None: - column = column.astype(dtype) + return column.astype(dtype) return column - elif isinstance(arbitrary, ColumnBase): + elif isinstance(arbitrary, (ColumnBase, cudf.Series, cudf.BaseIndex)): + # Ignoring nan_as_null per the docstring + if isinstance(arbitrary, cudf.Series): + arbitrary = arbitrary._column + elif isinstance(arbitrary, cudf.BaseIndex): + arbitrary = arbitrary._values if dtype is not None: return arbitrary.astype(dtype) - else: - return arbitrary - elif isinstance(arbitrary, cudf.Series): - data = arbitrary._column - if dtype is not None: - data = data.astype(dtype) - elif isinstance(arbitrary, cudf.BaseIndex): - data = arbitrary._values - if dtype is not None: - data = data.astype(dtype) - + return arbitrary elif hasattr(arbitrary, "__cuda_array_interface__"): desc = arbitrary.__cuda_array_interface__ shape = desc["shape"]