quantopian / qgrid

An interactive grid for sorting, filtering, and editing DataFrames in Jupyter notebooks
Apache License 2.0
3.03k stars 424 forks source link

add_row function breaks when using a numeric index #364

Open info-rchitect opened 3 years ago

info-rchitect commented 3 years ago

Environment

Description of Issue

I am trying to add an empty row at the end of my qgrid widget. The row argument is:

[('ID', ''), ('SOURCE', ''), ('PROJECT', ''), ('NAME', ''), ('STATUS', ''), ('SHAPE', ''), ('TIME', '')]

The error is:

C:\notebook_analytics\nemawashi\nemawashi_lib\user_interfaces\jobs.py in add_jobs_to_qgrid_widget(self, job)
    340                 empty_row.append((column_name, ''))
    341             print(empty_row)
--> 342             self.widgets['jobs_qgrid'].add_row(empty_row)
    343         jobs_df = self.widgets['jobs_qgrid'].df
    344         (col_opts, col_defs) = self.__fetch_qgrid_options()

~\Anaconda3\envs\nemawashi\lib\site-packages\qgrid\grid.py in add_row(self, row)
   1617             added_index = self._duplicate_last_row()
   1618         else:
-> 1619             added_index = self._add_row(row)
   1620 
   1621         self._notify_listeners({

~\Anaconda3\envs\nemawashi\lib\site-packages\qgrid\grid.py in _add_row(self, row)
   1666         print(f"dictrow={dict(row)}")
   1667         print(f"indexname={df.index.name}")
-> 1668         index_col_val = dict(row)[df.index.name]
   1669 
   1670         # check that the given column names match what

KeyError: None

The issue here is that the documentation says the following for the _add_row function.

def _add_row(self, row):
        """
        Append a new row to the end of the DataFrame given a list of 2-tuples
        of (column name, column value). This method will work for DataFrames
        with arbitrary index types.
        """

However, the code actual assumes there is an index name set within the dataframe here. A dataframe with an integer index will not have an index name. Either the _add_row function needs to be re-written or an option could be added to the add_row function to add an empty row.

qgrid_widget.add_row(empty=True)

Adding an empty row is very easy regardless if there is an index or not.

Reproduction Steps

  1. Create a qgrid widget with an integer index
  2. Call the add_row function with a row argument

What steps have you taken to resolve this already?

The workaround for me is to add the empty row in pandas and then re-instantiate the qgrid widget.

Anything else?

Please advise on the PR direction folks want and I will create the PR.