From 342b757cb954b56fc21d78ef2c43f3d68950e6ed Mon Sep 17 00:00:00 2001 From: Trevor Bergeron Date: Tue, 21 May 2024 17:40:43 +0000 Subject: [PATCH] fix: Fix Null index assign series to column --- bigframes/dataframe.py | 11 ++++++----- tests/system/small/test_empty_index.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/bigframes/dataframe.py b/bigframes/dataframe.py index 105588de2f..0ddc94d6e6 100644 --- a/bigframes/dataframe.py +++ b/bigframes/dataframe.py @@ -399,11 +399,12 @@ def memory_usage(self, index: bool = True): column_sizes = self.dtypes.map( lambda dtype: bigframes.dtypes.DTYPE_BYTE_SIZES.get(dtype, 8) * n_rows ) - if index: + if index and self._has_index: index_size = pandas.Series([self.index._memory_usage()], index=["Index"]) column_sizes = pandas.concat([index_size, column_sizes]) return column_sizes + @requires_index def info( self, verbose: Optional[bool] = None, @@ -767,7 +768,7 @@ def _apply_series_binop_axis_0( block = block.drop_columns([get_column_left[column_id]]) block = block.drop_columns([series_col]) - block = block.with_index_labels(self.index.names) + block = block.with_index_labels(self._block.index.names) return DataFrame(block) def _apply_series_binop_axis_1( @@ -1610,7 +1611,7 @@ def _assign_series_join_on_index( # Update case, remove after copying into columns block = block.drop_columns([source_column]) - return DataFrame(block.with_index_labels(self.index.names)) + return DataFrame(block.with_index_labels(self._block.index.names)) def reset_index(self, *, drop: bool = False) -> DataFrame: block = self._block.reset_index(drop) @@ -3282,7 +3283,7 @@ def _prepare_export( array_value = self._block.expr new_col_labels, new_idx_labels = utils.get_standardized_ids( - self._block.column_labels, self.index.names + self._block.column_labels, self._block.index.names ) columns = list(self._block.value_columns) @@ -3319,7 +3320,7 @@ def _run_io_query( session = self._block.expr.session self._optimize_query_complexity() export_array, id_overrides = self._prepare_export( - index=index, ordering_id=ordering_id + index=index and self._has_index, ordering_id=ordering_id ) _, query_job = session._execute( diff --git a/tests/system/small/test_empty_index.py b/tests/system/small/test_empty_index.py index 7a1715e3d1..3216264a8b 100644 --- a/tests/system/small/test_empty_index.py +++ b/tests/system/small/test_empty_index.py @@ -169,6 +169,21 @@ def test_empty_index_df_self_aligns( ) +def test_empty_index_setitem(scalars_df_empty_index, scalars_pandas_df_default_index): + bf_result = scalars_df_empty_index.copy() + bf_result["new_col"] = ( + scalars_df_empty_index["int64_col"] + scalars_df_empty_index["float64_col"] + ) + pd_result = scalars_pandas_df_default_index.copy() + pd_result["new_col"] = ( + scalars_pandas_df_default_index["int64_col"] + + scalars_pandas_df_default_index["float64_col"] + ) + pd.testing.assert_frame_equal( + bf_result.to_pandas(), pd_result.reset_index(drop=True), check_dtype=False + ) + + def test_empty_index_df_concat(scalars_df_empty_index, scalars_pandas_df_default_index): bf_result = bpd.concat([scalars_df_empty_index, scalars_df_empty_index]) pd_result = pd.concat(