@@ -940,7 +940,7 @@ def prompt_save(
940
940
return PROMPT_SAVE_DISCARDED
941
941
return PROMPT_SAVE_PROCEED
942
942
# if no
943
- self .rows . purge_virtual ()
943
+ self .purge_virtual ()
944
944
if vrows and update_elements :
945
945
self .frm .update_elements (self .key )
946
946
return PROMPT_SAVE_DISCARDED
@@ -1007,6 +1007,12 @@ def requery(
1007
1007
1008
1008
rows = self .driver .execute (query )
1009
1009
self .rows = rows
1010
+
1011
+ if "sort_order" not in self .rows .attrs :
1012
+ # Store the sort order as a dictionary in the attrs of the DataFrame
1013
+ sort_order = self .rows [self .pk_column ].to_list ()
1014
+ self .rows .attrs ["sort_order" ] = {self .pk_column : sort_order }
1015
+
1010
1016
# now we can restore the sort order
1011
1017
self .load_sort_settings (sort_settings )
1012
1018
self .sort (self .table )
@@ -2223,12 +2229,12 @@ def get_sort_key(row):
2223
2229
2224
2230
try :
2225
2231
self .rows .sort_values (
2226
- by = rows ,
2227
- key = get_sort_key ,
2232
+ column ,
2228
2233
ascending = not reverse ,
2229
2234
inplace = True ,
2230
2235
)
2231
2236
except KeyError :
2237
+ print ("OH NO" )
2232
2238
logger .debug (f"DataFrame could not sort by column { column } . KeyError." )
2233
2239
2234
2240
def sort_by_index (self , index : int , table : str , reverse = False ):
@@ -2272,7 +2278,13 @@ def sort_reset(self) -> None:
2272
2278
2273
2279
:returns: None
2274
2280
"""
2275
- self .rows .index = self .rows .attrs ["original_index" ]
2281
+ # Restore the original sort order
2282
+ sort_column = list (self .rows .attrs ["sort_order" ].keys ())[0 ]
2283
+ sort_order = self .rows .attrs ["sort_order" ][sort_column ]
2284
+ self .rows .loc [:, sort_column ] = pd .Categorical (
2285
+ self .rows [sort_column ], categories = sort_order , ordered = True
2286
+ )
2287
+ self .rows .sort_values (sort_column , inplace = True )
2276
2288
2277
2289
def sort (self , table : str ) -> None :
2278
2290
"""
@@ -2293,6 +2305,7 @@ def sort(self, table: str) -> None:
2293
2305
self .sort_by_column (
2294
2306
self .rows .attrs ["sort_column" ], table , self .rows .attrs ["sort_reverse" ]
2295
2307
)
2308
+ self .rows .reset_index (drop = True , inplace = True )
2296
2309
2297
2310
def sort_cycle (self , column : str , table : str ) -> int :
2298
2311
"""
@@ -6015,7 +6028,6 @@ def set(
6015
6028
df = pd .DataFrame (row_data )
6016
6029
df .attrs ["lastrowid" ] = lastrowid
6017
6030
df .attrs ["exception" ] = exception
6018
- df .attrs ["original_index" ] = df .index .copy () # Store the original index
6019
6031
df .attrs ["column_info" ] = column_info
6020
6032
df .attrs ["virtual" ] = pd .Series (
6021
6033
[False ] * len (df .index ), index = df .index
0 commit comments