Skip to content

Commit 138acd3

Browse files
author
Tim Shawver
committed
Fixing issue with editing a table with unnamed index columns, also adding a test that will catch this sort of issue next time.
1 parent 2fd7f48 commit 138acd3

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

qgrid/grid.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,8 +1220,9 @@ def _handle_qgrid_msg_helper(self, content):
12201220
if col_info['type'] == 'datetime':
12211221
val_to_set = pd.to_datetime(val_to_set)
12221222

1223-
old_value = self._df.at[location]
1224-
self._df.at[location] = val_to_set
1223+
old_value = self._df.loc[location]
1224+
self._df.loc[location] = val_to_set
1225+
12251226
query = self._unfiltered_df[self._index_col_name] == \
12261227
content['unfiltered_index']
12271228
self._unfiltered_df.loc[query, content['column']] = val_to_set

qgrid/tests/test_grid.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ def test_edit_date():
7474
"2013-01-16T00:00:00.000Z")
7575

7676

77+
def test_edit_multi_index_df():
78+
df_multi = create_multi_index_df()
79+
view = QgridWidget(df=df_multi)
80+
old_val = df_multi.loc[('bar', 'two'), 1]
81+
82+
check_edit_success(view,
83+
1,
84+
1,
85+
old_val,
86+
round(old_val, pd.get_option('display.precision') - 1),
87+
3.45678,
88+
3.45678)
89+
90+
7791
def check_edit_success(widget,
7892
col_name,
7993
row_index,
@@ -85,7 +99,7 @@ def check_edit_success(widget,
8599
event_history = init_event_history('cell_edited', widget)
86100

87101
grid_data = json.loads(widget._df_json)['data']
88-
assert grid_data[row_index][col_name] == old_val_json
102+
assert grid_data[row_index][str(col_name)] == old_val_json
89103

90104
widget._handle_qgrid_msg_helper({
91105
'column': col_name,
@@ -95,9 +109,10 @@ def check_edit_success(widget,
95109
'value': new_val_json
96110
})
97111

112+
expected_index_val = widget._df.index[row_index]
98113
assert event_history == [{
99114
'name': 'cell_edited',
100-
'index': row_index,
115+
'index': expected_index_val,
101116
'column': col_name,
102117
'old': old_val_obj,
103118
'new': new_val_obj
@@ -107,7 +122,7 @@ def check_edit_success(widget,
107122
# call _update_table so the widget updates _df_json
108123
widget._update_table(fire_data_change_event=False)
109124
grid_data = json.loads(widget._df_json)['data']
110-
assert grid_data[row_index][col_name] == new_val_json
125+
assert grid_data[row_index][str(col_name)] == new_val_json
111126

112127

113128
def test_edit_number():

0 commit comments

Comments
 (0)