Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
Already on GitHub? Sign in to your account
Using pandas with PyQt #435
Closed
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
06a9797
added qtpandas.py : a module for integration of pandas into PyQt
sjev ccbe5eb
test
sjev ae42569
Merge branch 'master' of https://github.com/wesm/pandas
sjev 0380599
updated to 0.6.1 functionality (data type retaining). Used input from…
sjev 83830d2
fixed issue with py2exe
sjev
Jump to file or symbol
Failed to load files and symbols.
| @@ -0,0 +1,114 @@ | ||
| +''' | ||
| +Easy integration of DataFrame into pyqt framework | ||
| + | ||
| +@author: Jev Kuznetsov | ||
| +''' | ||
| +from PyQt4.QtCore import (QAbstractTableModel,Qt,QVariant,QModelIndex, SIGNAL) | ||
| +from PyQt4.QtGui import (QApplication,QDialog,QVBoxLayout, QTableView, QWidget) | ||
| + | ||
| +from pandas import DataFrame, Index | ||
| + | ||
| + | ||
| + | ||
| +class DataFrameModel(QAbstractTableModel): | ||
| + ''' data model for a DataFrame class ''' | ||
| + def __init__(self): | ||
| + super(DataFrameModel,self).__init__() | ||
| + self.df = DataFrame() | ||
| + | ||
| + def setDataFrame(self,dataFrame): | ||
| + self.df = dataFrame | ||
| + | ||
| + def signalUpdate(self): | ||
| + ''' tell viewers to update their data (this is full update, not efficient)''' | ||
| + self.layoutChanged.emit() | ||
| + | ||
| + #------------- table display functions ----------------- | ||
| + def headerData(self,section,orientation,role=Qt.DisplayRole): | ||
| + if role != Qt.DisplayRole: | ||
| + return QVariant() | ||
| + | ||
| + if orientation == Qt.Horizontal: | ||
| + try: | ||
| + return self.df.columns.tolist()[section] | ||
| + except (IndexError, ): | ||
| + return QVariant() | ||
| + elif orientation == Qt.Vertical: | ||
| + try: | ||
| + #return self.df.index.tolist() | ||
| + return self.df.index.tolist()[section] | ||
| + except (IndexError, ): | ||
| + return QVariant() | ||
| + | ||
| + def data(self, index, role=Qt.DisplayRole): | ||
| + if role != Qt.DisplayRole: | ||
| + return QVariant() | ||
| + | ||
| + if not index.isValid(): | ||
| + return QVariant() | ||
| + | ||
| + return QVariant(str(self.df.ix[index.row(),index.column()])) | ||
| + | ||
| + | ||
| + def rowCount(self, index=QModelIndex()): | ||
| + return self.df.shape[0] | ||
| + | ||
| + def columnCount(self, index=QModelIndex()): | ||
| + return self.df.shape[1] | ||
| + | ||
| + | ||
| +class DataFrameWidget(QWidget): | ||
| + ''' a simple widget for using DataFrames in a gui ''' | ||
| + def __init__(self,dataFrame, parent=None): | ||
| + super(DataFrameWidget,self).__init__(parent) | ||
| + | ||
| + self.dataModel = DataFrameModel() | ||
| + self.dataModel.setDataFrame(dataFrame) | ||
| + | ||
| + self.dataTable = QTableView() | ||
| + self.dataTable.setModel(self.dataModel) | ||
| + self.dataModel.signalUpdate() | ||
| + | ||
| + layout = QVBoxLayout() | ||
| + layout.addWidget(self.dataTable) | ||
| + self.setLayout(layout) | ||
| + | ||
| + | ||
| + | ||
| + def resizeColumnsToContents(self): | ||
| + self.dataTable.resizeColumnsToContents() | ||
| + | ||
| +#-----------------stand alone test code | ||
| + | ||
| +def testDf(): | ||
| + ''' creates test dataframe ''' | ||
| + data = {'int':[1,2,3],'float':[1.5,2.5,3.5],'string':['a','b','c'],'nan':[np.nan,np.nan,np.nan]} | ||
| + return DataFrame(data, index=Index(['AAA','BBB','CCC']))[['int','float','string','nan']] | ||
| + | ||
| + | ||
| +class Form(QDialog): | ||
| + def __init__(self,parent=None): | ||
| + super(Form,self).__init__(parent) | ||
| + | ||
| + df = testDf() # make up some data | ||
| + widget = DataFrameWidget(df) | ||
| + widget.resizeColumnsToContents() | ||
| + | ||
| + layout = QVBoxLayout() | ||
| + layout.addWidget(widget) | ||
| + self.setLayout(layout) | ||
| + | ||
| +if __name__=='__main__': | ||
| + import sys | ||
| + import numpy as np | ||
| + | ||
| + app = QApplication(sys.argv) | ||
| + form = Form() | ||
| + form.show() | ||
| + app.exec_() | ||
| + | ||
| + | ||
| + | ||
| + | ||
| + | ||
| + |