From 6234b85d04ce5fef8c74aaf3bffbf6e529f56068 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 12 Aug 2024 13:26:08 -0700 Subject: [PATCH] reset label_dtype after empty --- python/cudf/cudf/core/dataframe.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/python/cudf/cudf/core/dataframe.py b/python/cudf/cudf/core/dataframe.py index 2a39ec13cdf..b60338323a0 100644 --- a/python/cudf/cudf/core/dataframe.py +++ b/python/cudf/cudf/core/dataframe.py @@ -365,12 +365,13 @@ def _setitem_tuple_arg(self, key, value): try: columns_df = self._frame._get_columns_by_label(key[1]) except KeyError: - if not self._frame.empty and isinstance(key[0], slice): + was_empty = self._frame.empty + if not was_empty and isinstance(key[0], slice): pos_range = _get_label_range_or_mask( self._frame.index, key[0].start, key[0].stop, key[0].step ) idx = self._frame.index[pos_range] - elif self._frame.empty and isinstance(key[0], slice): + elif was_empty and isinstance(key[0], slice): idx = None else: if is_scalar(key[0]): @@ -396,6 +397,9 @@ def _setitem_tuple_arg(self, key, value): idx if idx is not None else cudf.RangeIndex(len(new_col)) ) self._frame._data.insert(key[1], new_col) + if was_empty: + # TODO: Create a cudf_to_pandas_dtype function to use here + self._frame._data.label_dtype = None else: if is_scalar(value): for col in columns_df._column_names: @@ -3308,7 +3312,9 @@ def _insert(self, loc, name, value, nan_as_null=None, ignore_index=True): dtype=dtype, ) + was_empty = False if len(self) == 0: + was_empty = True if isinstance(value, (pd.Series, Series)): if not ignore_index: self.index = cudf.Index(value.index) @@ -3338,6 +3344,9 @@ def _insert(self, loc, name, value, nan_as_null=None, ignore_index=True): value = column.as_column(value, nan_as_null=nan_as_null) self._data.insert(name, value, loc=loc) + if was_empty: + # TODO: Create a cudf_to_pandas_dtype function to use here + self._data.label_dtype = None @property # type:ignore @_performance_tracking