Home > other >  pandas AttributeError: 'NoneType' object has no attribute 'shape'
pandas AttributeError: 'NoneType' object has no attribute 'shape'

Time:05-16

I am trying to delete a row from a pandas dataframe based in the index value that the user gives me. It is giving me an error from my PandasModel file whenever I try to drop it. It is giving me the error even if the dataframe is not empty and the index does exist.

Error

AttributeError: 'NoneType' object has no attribute 'shape'

main.py Code

#Drop choosen index
self.df_Edit = self.df_Edit.drop(index = int(self.Edit.Line_Index.text()), inplace = True)

#Set the DataFrame to the Panads DataModel
self.model = PandasModel(self.df_Edit)
#Insert the DataFrame onto the Tabels
self.Edit.tableView.setModel(self.model)

PandasModel

class PandasModel(QAbstractTableModel):

    def __init__(self, data):
    
        QAbstractTableModel.__init__(self)
        self._data = data

    def rowCount(self, parent = None):
    
        return self._data.shape[0]

    def columnCount(self, parent = None):
    
        return self._data.shape[1]

    def data(self, index, role=Qt.DisplayRole):
    
        if index.isValid():
        
            if role == Qt.DisplayRole:
                return str(self._data.iloc[index.row(), index.column()])
        return None
        
    def headerData(self, col, orientation, role):
    
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return self._data.columns[col]
        return None

CodePudding user response:

Here is your problem:

self.df_Edit = self.df_Edit.drop(index = int(self.Edit.Line_Index.text()), inplace = True)

You SPECIFICALLY asked for "inplace" operation. In that case. dropna doesn't return anything, because it did the operation in place. That sets self.df_edit to None, and results in your error.

You need one or the other:

self.df_Edit.drop(index = int(self.Edit.Line_Index.text()), inplace = True)
# or
self.df_Edit = self.df_Edit.drop(index = int(self.Edit.Line_Index.text()))
  • Related