Skip to content

Commit c2a87b6

Browse files
committed
Use 'id' as default index name
Signed-off-by: Itay Dafna <[email protected]>
1 parent 35b968a commit c2a87b6

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

ipydatagrid/datagrid.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,22 @@ def data(self):
378378
@staticmethod
379379
def generate_data_object(dataframe, guid_key="ipydguuid"):
380380
dataframe[guid_key] = pd.RangeIndex(0, dataframe.shape[0])
381+
382+
# Renaming default index name from 'index' to 'id' on
383+
# single index DataFrames. This allows users to use
384+
# 'index' as a column name. If 'id' exists, we add _x
385+
# suffix to id, where { x | 0 <= x <= inf }
386+
if not isinstance(dataframe.index, pd.MultiIndex):
387+
if "id" in dataframe.columns:
388+
index = 0
389+
new_index_name = f'id_{index}'
390+
while new_index_name in dataframe.columns:
391+
index += 1
392+
new_index_name = f'id_{index}'
393+
dataframe = dataframe.rename_axis(new_index_name)
394+
else:
395+
dataframe = dataframe.rename_axis("id")
396+
381397
schema = pd.io.json.build_table_schema(dataframe)
382398
reset_index_dataframe = dataframe.reset_index()
383399
data = reset_index_dataframe.to_dict(orient="records")

0 commit comments

Comments
 (0)