Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. PySide: Updating pandas Dataframe imports
Forum Updated to NodeBB v4.3 + New Features

PySide: Updating pandas Dataframe imports

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    barbarossa
    wrote on 6 Nov 2014, 19:14 last edited by
    #1

    In my program I try to show different .csv/.txt files with the help of pandas module. The problem that I have is that only the first import is shown, while all further imports are not shown (the first import remains) ... although the data is read from the file.

    In example, I try to first upload the first .csv file:
    @1.0 2.0 3.0 a
    4.0 5.0 6.0 b@

    which is shown and then I import the other .csv file:
    @a b c d e f g h
    1 2 3 4 5 6 7 8
    q w e r t y u i@

    but is not shown.

    I would be happy of any suggestion why this is not working.

    @from PySide import QtGui
    from PySide import QtCore
    import pandas as pd
    QVariant = lambda value=None: value

    class IdentificationWidget(QtGui.QWidget):
    def init(self, data):
    super().init()

        self.data = data
        self.dataModel = DataFrameModel()
        self.dataTable = QtGui.QTableView()
    
    def update(self):
        print("update is being made")
        self.dataModel.signalUpdate(self.data)
        self.dataModel.printShape()
        self.dataTable.setModel(self.dataModel)
        self.main_layout = QtGui.QVBoxLayout()
        self.main_layout.addWidget(self.dataTable)
        self.setLayout(self.main_layout)
    

    class DataFrameModel(QtCore.QAbstractTableModel):
    ''' data model for a DataFrame class '''
    def init(self):
    super(DataFrameModel, self).init()
    self.df = pd.DataFrame()

    def setDataFrame(self, dataFrame):
        self.df = dataFrame
    
    def signalUpdate(self, dataIn):
        self.df = dataIn
        self.layoutChanged.emit()
    
    def printShape(self):
        print(self.df.shape[0], self.df.shape[1])
    #------------- table display functions -----------------
    def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
        if role != QtCore.Qt.DisplayRole:
            return QVariant()
    
        if orientation == QtCore.Qt.Horizontal:
            try:
                return self.df.columns.tolist()[section]
            except (IndexError, ):
                return QVariant()
        elif orientation == QtCore.Qt.Vertical:
            try:
                # return self.df.index.tolist()
                return self.df.index.tolist()[section]
            except (IndexError, ):
                return QVariant()
    
    def data(self, index, role=QtCore.Qt.DisplayRole):
        if role != QtCore.Qt.DisplayRole:
            return QVariant()
    
        if not index.isValid():
            return QVariant()
    
        return QVariant(str(self.df.ix[index.row(), index.column()]))
    
    def flags(self, index):
            flags = super(DataFrameModel, self).flags(index)
            flags |= QtCore.Qt.ItemIsEditable
            return flags
    
    def setData(self, index, value, role):
        row = self.df.index[index.row()]
        col = self.df.columns[index.column()]
    
        dtype = self.df[col].dtype
        if dtype != object:
            value = None if value == '' else dtype.type(value)
        self.df.set_value(row, col, value)
        return True
    
    def rowCount(self, index=QtCore.QModelIndex()):
        return self.df.shape[0]
    
    def columnCount(self, index=QtCore.QModelIndex()):
        return self.df.shape[1]
    

    class MainWindow(QtGui.QMainWindow):
    """Main window of the application."""
    def init(self):
    super(MainWindow, self).init()
    self.main_widget = QtGui.QWidget(self)

        # -- Stacked widget is used to -- stack widgets.
        self.stacked_widget = QtGui.QStackedWidget()
    
        # -- Menu bar.
        self.init_menu_bar()
    
        # -- Layout is put together
        grid = QtGui.QGridLayout()
        grid.addWidget(self.stacked_widget, 0, 0)
    
        self.main_widget.setLayout(grid)
    
        self.setCentralWidget(self.main_widget)
        self.setGeometry(50, 50, 800, 600)
    
    def init_menu_bar(self):
        """Initialize menu bar."""
        menu_bar = self.menuBar()
        file_menu = menu_bar.addMenu('&File')
    
        # .. Add uff import option.
        import_action = QtGui.QAction('&Open/Import', self)
        import_action.setShortcut('Ctrl+O')
        import_action.setStatusTip('Open file ...')
        file_menu.addAction(import_action)
        import_action.triggered.connect(self.getFiles)
    
    def getFiles(self):
        file_name, filtr = QtGui.QFileDialog.getOpenFileName(
            self, self.tr("Open File"), "/.", self.tr("(*.csv *.txt);;"))
        self.data = pd.read_csv(file_name, sep=' ')
        # -- Update all widgets.
    
        widgets = {0: IdentificationWidget}
        widget = widgets[0](self.data)
        widget.update()  # widget update
    
        self.stacked_widget.addWidget(widget)
    

    if name == 'main':
    import sys
    app = QtGui.QApplication(sys.argv)

    main_window = MainWindow()
    main_window.show()
    
    sys.exit(app.exec_())@
    
    1 Reply Last reply
    0

    1/1

    6 Nov 2014, 19:14

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved