@@ -183,13 +183,17 @@ def _explode_array_columns(
183183 """Explode array columns into new rows."""
184184 exploded_rows = []
185185 array_row_groups : dict [str , list [int ]] = {}
186- for orig_idx , row in dataframe .iterrows ():
186+ non_array_columns = dataframe .columns .drop (array_columns ).tolist ()
187+ non_array_df = dataframe [non_array_columns ]
188+
189+ for orig_idx in dataframe .index :
190+ non_array_data = non_array_df .loc [orig_idx ].to_dict ()
187191 array_values = {}
188192 max_len_in_row = 0
189193 non_na_array_found = False
190194
191195 for col_name in array_columns :
192- val = row [ col_name ]
196+ val = dataframe . loc [ orig_idx , col_name ]
193197 if val is not None and not (
194198 isinstance (val , list ) and len (val ) == 1 and pd .isna (val [0 ])
195199 ):
@@ -200,7 +204,7 @@ def _explode_array_columns(
200204 array_values [col_name ] = []
201205
202206 if not non_na_array_found :
203- new_row = row .copy ()
207+ new_row = non_array_data .copy ()
204208 for col_name in array_columns :
205209 new_row [f"{ col_name } " ] = pd .NA
206210 exploded_rows .append (new_row )
@@ -212,11 +216,7 @@ def _explode_array_columns(
212216
213217 # Create one row per array element, up to max_len_in_row
214218 for array_idx in range (max_len_in_row ):
215- new_row = row .copy ()
216-
217- # Remove array columns from the row copy
218- for col_name in array_columns :
219- new_row = new_row .drop (col_name )
219+ new_row = non_array_data .copy ()
220220
221221 # Add the specific array element for this index
222222 for col_name in array_columns :
@@ -234,7 +234,8 @@ def _explode_array_columns(
234234 array_row_groups [orig_key ].append (len (exploded_rows ) - 1 )
235235
236236 if exploded_rows :
237- exploded_df = pd .DataFrame (exploded_rows )
237+ # Reconstruct the DataFrame to maintain original column order
238+ exploded_df = pd .DataFrame (exploded_rows )[dataframe .columns ]
238239 for col in exploded_df .columns :
239240 # After explosion, object columns that are all-numeric (except for NAs)
240241 # should be converted to a numeric dtype for proper alignment.
0 commit comments