<s> """<STR_LIT>""" <EOL> import warnings <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> try : <EOL> from PyQt4 . QtCore import QAbstractTableModel , Qt , QVariant , QModelIndex <EOL> from PyQt4 . QtGui import ( <EOL> QApplication , QDialog , QVBoxLayout , QTableView , QWidget ) <EOL> except ImportError : <EOL> from PySide . QtCore import QAbstractTableModel , Qt , QModelIndex <EOL> from PySide . QtGui import ( <EOL> QApplication , QDialog , QVBoxLayout , QTableView , QWidget ) <EOL> QVariant = lambda value = None : value <EOL> from pandas import DataFrame , Index <EOL> class DataFrameModel ( QAbstractTableModel ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( DataFrameModel , self ) . __init__ ( ) <EOL> self . df = DataFrame ( ) <EOL> def setDataFrame ( self , dataFrame ) : <EOL> self . df = dataFrame <EOL> def signalUpdate ( self ) : <EOL> """<STR_LIT>""" <EOL> self . layoutChanged . emit ( ) <EOL> def headerData ( self , section , orientation , role = Qt . DisplayRole ) : <EOL> if role != Qt . DisplayRole : <EOL> return QVariant ( ) <EOL> if orientation == Qt . Horizontal : <EOL> try : <EOL> return self . df . columns . tolist ( ) [ section ] <EOL> except ( IndexError , ) : <EOL> return QVariant ( ) <EOL> elif orientation == Qt . Vertical : <EOL> try : <EOL> return self . df . index . tolist ( ) [ section ] <EOL> except ( IndexError , ) : <EOL> return QVariant ( ) <EOL> def data ( self , index , role = Qt . DisplayRole ) : <EOL> if role != Qt . DisplayRole : <EOL> return QVariant ( ) <EOL> if not index . isValid ( ) : <EOL> return QVariant ( ) <EOL> return QVariant ( str ( self . df . ix [ index . row ( ) , index . column ( ) ] ) ) <EOL> def flags ( self , index ) : <EOL> flags = super ( DataFrameModel , self ) . flags ( index ) <EOL> flags |= Qt . ItemIsEditable <EOL> return flags <EOL> def setData ( self , index , value , role ) : <EOL> row = self . df . index [ index . row ( ) ] <EOL> col = self . df . columns [ index . column ( ) ] <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> value = value . toPyObject ( ) <EOL> else : <EOL> dtype = self . df [ col ] . dtype <EOL> if dtype != object : <EOL> value = None if value == '<STR_LIT>' else dtype . type ( value ) <EOL> self . df . set_value ( row , col , value ) <EOL> return True <EOL> def rowCount ( self , index = QModelIndex ( ) ) : <EOL> return self . df . shape [ <NUM_LIT:0> ] <EOL> def columnCount ( self , index = QModelIndex ( ) ) : <EOL> return self . df . shape [ <NUM_LIT:1> ] <EOL> class DataFrameWidget ( QWidget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dataFrame , parent = None ) : <EOL> super ( DataFrameWidget , self ) . __init__ ( parent ) <EOL> self . dataModel = DataFrameModel ( ) <EOL> self . dataTable = QTableView ( ) <EOL> self . dataTable . setModel ( self . dataModel ) <EOL> layout = QVBoxLayout ( ) <EOL> layout . addWidget ( self . dataTable ) <EOL> self . setLayout ( layout ) <EOL> self . setDataFrame ( dataFrame ) <EOL> def setDataFrame ( self , dataFrame ) : <EOL> self . dataModel . setDataFrame ( dataFrame ) <EOL> self . dataModel . signalUpdate ( ) <EOL> self . dataTable . resizeColumnsToContents ( ) <EOL> def testDf ( ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT:int>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , '<STR_LIT:float>' : [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> '<STR_LIT:string>' : [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] , '<STR_LIT>' : [ np . nan , np . nan , np . nan ] } <EOL> return DataFrame ( data , index = Index ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> columns = [ '<STR_LIT:int>' , '<STR_LIT:float>' , '<STR_LIT:string>' , '<STR_LIT>' ] ) <EOL> class Form ( QDialog ) : <EOL> def __init__ ( self , parent = None ) : <EOL> super ( Form , self ) . __init__ ( parent ) <EOL> df = testDf ( ) <EOL> widget = DataFrameWidget ( df ) <EOL> widget . resizeColumnsToContents ( ) <EOL> layout = QVBoxLayout ( ) <EOL> layout . addWidget ( widget ) <EOL> self . setLayout ( layout ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> import numpy as np <EOL> app = QApplication ( sys . argv ) <EOL> form = Form ( ) <EOL> form . show ( ) <EOL> app . exec_ ( ) </s>
