From 10872e30e0ef53712b06b8951f9e224d58b909ae Mon Sep 17 00:00:00 2001 From: Shenyang Cai Date: Fri, 13 Sep 2024 20:04:29 +0000 Subject: [PATCH] fix: Fix a bug that raises exception when re-indexing columns with their original order --- bigframes/dataframe.py | 5 +++++ tests/system/small/test_dataframe.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/bigframes/dataframe.py b/bigframes/dataframe.py index 2ae6aefe1b..c066e5d33a 100644 --- a/bigframes/dataframe.py +++ b/bigframes/dataframe.py @@ -1913,6 +1913,11 @@ def _reindex_rows( def _reindex_columns(self, columns): block = self._block new_column_index, indexer = self.columns.reindex(columns) + + if indexer is None: + # The new index is the same as the old one. Do nothing. + return self + result_cols = [] for label, index in zip(columns, indexer): if index >= 0: diff --git a/tests/system/small/test_dataframe.py b/tests/system/small/test_dataframe.py index f51b597650..af56b61448 100644 --- a/tests/system/small/test_dataframe.py +++ b/tests/system/small/test_dataframe.py @@ -3664,6 +3664,21 @@ def test_df_reindex_columns(scalars_df_index, scalars_pandas_df_index): ) +def test_df_reindex_columns_with_same_order(scalars_df_index, scalars_pandas_df_index): + # First, make sure the two dataframes have the same columns in order. + columns = ["int64_col", "int64_too"] + bf = scalars_df_index[columns] + pd_df = scalars_pandas_df_index[columns] + + bf_result = bf.reindex(columns=columns).to_pandas() + pd_result = pd_df.reindex(columns=columns) + + pd.testing.assert_frame_equal( + bf_result, + pd_result, + ) + + def test_df_equals_identical(scalars_df_index, scalars_pandas_df_index): unsupported = [ "geography_col",