Skip to content

Commit 0a77f5e

Browse files
committed
refs #281, Pandas partially working
Sorting code working. Still need to work out how to sort FK relationships by their description column
1 parent e3e50eb commit 0a77f5e

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

pysimplesql/pysimplesql.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ def prompt_save(
940940
return PROMPT_SAVE_DISCARDED
941941
return PROMPT_SAVE_PROCEED
942942
# if no
943-
self.rows.purge_virtual()
943+
self.purge_virtual()
944944
if vrows and update_elements:
945945
self.frm.update_elements(self.key)
946946
return PROMPT_SAVE_DISCARDED
@@ -1007,6 +1007,12 @@ def requery(
10071007

10081008
rows = self.driver.execute(query)
10091009
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+
10101016
# now we can restore the sort order
10111017
self.load_sort_settings(sort_settings)
10121018
self.sort(self.table)
@@ -2223,12 +2229,12 @@ def get_sort_key(row):
22232229

22242230
try:
22252231
self.rows.sort_values(
2226-
by=rows,
2227-
key=get_sort_key,
2232+
column,
22282233
ascending=not reverse,
22292234
inplace=True,
22302235
)
22312236
except KeyError:
2237+
print("OH NO")
22322238
logger.debug(f"DataFrame could not sort by column {column}. KeyError.")
22332239

22342240
def sort_by_index(self, index: int, table: str, reverse=False):
@@ -2272,7 +2278,13 @@ def sort_reset(self) -> None:
22722278
22732279
:returns: None
22742280
"""
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)
22762288

22772289
def sort(self, table: str) -> None:
22782290
"""
@@ -2293,6 +2305,7 @@ def sort(self, table: str) -> None:
22932305
self.sort_by_column(
22942306
self.rows.attrs["sort_column"], table, self.rows.attrs["sort_reverse"]
22952307
)
2308+
self.rows.reset_index(drop=True, inplace=True)
22962309

22972310
def sort_cycle(self, column: str, table: str) -> int:
22982311
"""
@@ -6015,7 +6028,6 @@ def set(
60156028
df = pd.DataFrame(row_data)
60166029
df.attrs["lastrowid"] = lastrowid
60176030
df.attrs["exception"] = exception
6018-
df.attrs["original_index"] = df.index.copy() # Store the original index
60196031
df.attrs["column_info"] = column_info
60206032
df.attrs["virtual"] = pd.Series(
60216033
[False] * len(df.index), index=df.index

0 commit comments

Comments
 (0)