def test_columnCount(): model = DataFrameModel(pandas.DataFrame([0], columns=['A'])) assert model.columnCount() == 1 model = DataFrameModel( pandas.DataFrame(numpy.arange(100).reshape(1, 100), columns=numpy.arange(100))) assert model.columnCount() == 100
def setDataFrame(self, dataFrame): self.df = dataFrame dataModel = DataFrameModel() dataModel.setDataFrame(self.df) self.dataModel = dataModel self.dataListView.setModel(dataModel) self.dataTableView.setViewModel(dataModel) self.dataComboBox.setModel(dataModel) # self.dataTableView.resizeColumnsToContents() # create a simple item model for our choosing combobox columnModel = QtGui.QStandardItemModel() for column in self.df.columns: columnModel.appendRow(QtGui.QStandardItem(column)) self.chooseColumnComboBox.setModel(columnModel) self.tableViewColumnDtypes.setModel(dataModel.columnDtypeModel()) self.tableViewColumnDtypes.horizontalHeader().setDefaultSectionSize( 200) self.tableViewColumnDtypes.setItemDelegateForColumn( 1, DtypeComboDelegate(self.tableViewColumnDtypes)) dataModel.changingDtypeFailed.connect(self.changeColumnValue)
def test_copyDataFrame(copy, operator): dataFrame = pandas.DataFrame([0], columns=['A']) model = DataFrameModel(dataFrame, copyDataFrame=copy) assert operator(id(model.dataFrame()), id(dataFrame)) model.setDataFrame(dataFrame, copyDataFrame=copy) assert operator(id(model.dataFrame()), id(dataFrame))
def test_TimestampFormat(): model = DataFrameModel() assert model.timestampFormat == Qt.ISODate newFormat = u"yy-MM-dd hh:mm" model.timestampFormat = newFormat assert model.timestampFormat == newFormat with pytest.raises(TypeError) as excinfo: model.timestampFormat = "yy-MM-dd hh:mm" assert "unicode" in unicode(excinfo.value)
def test_setDataFrame(): dataFrame = pandas.DataFrame([0], columns=['A']) model = DataFrameModel() model.setDataFrame(dataFrame) assert not model.dataFrame().empty assert model.dataFrame() is dataFrame with pytest.raises(TypeError) as excinfo: model.setDataFrame(None) assert "pandas.core.frame.DataFrame" in unicode(excinfo.value)
def _previewFile(self): """Updates the preview widgets with new models for both tab panes. """ dataFrame = self._loadCSVDataFrame() dataFrameModel = DataFrameModel(dataFrame) self._previewTableView.setModel(dataFrameModel) columnModel = dataFrameModel.columnDtypeModel() columnModel.changeFailed.connect(self.updateStatusBar) self._datatypeTableView.setModel(columnModel)
def display_query(selcols): ## setup a new empty model model1 = DataFrameModel() ## setup an application and create a table view widget app = QtGui.QApplication([]) widget1 = DataTableWidget() widget1.resize(1600, 800) widget1.show() ## asign the created model widget1.setViewModel(model1) ## fill the model with data model1.setDataFrame(selcols) ## start the app""" app.exec_() return
def accepted(self): """Successfully close the widget and return the loaded model. This method is also a `SLOT`. The dialog will be closed, when the `ok` button is pressed. If a `DataFrame` was loaded, it will be emitted by the signal `load`. """ model = self._previewTableView.model() if model is not None: df = model.dataFrame().copy() dfModel = DataFrameModel(df) self.load.emit(dfModel, self._filename) self._resetWidgets() self.accept()
def test_flags(): model = DataFrameModel(pandas.DataFrame([0], columns=['A'])) index = model.index(0, 0) assert index.isValid() assert model.flags(index) == Qt.ItemIsSelectable | Qt.ItemIsEnabled model.enableEditing(True) assert model.flags( index) == Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable model.setDataFrame(pandas.DataFrame([True], columns=['A'])) index = model.index(0, 0) model.enableEditing(True) assert model.flags( index) != Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable assert model.flags( index ) == Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable
def test_remove_columns_random(self, dataFrame): columnNames = dataFrame.columns.tolist() columnNames = [(i, n) for i, n in enumerate(columnNames)] for cycle in xrange(1000): elements = random.randint(1, len(columnNames)) names = random.sample(columnNames, elements) df = dataFrame.copy() model = DataFrameModel(df) assert not model.removeDataFrameColumns(names) model.enableEditing(True) model.removeDataFrameColumns(names) _columnSet = set(columnNames) _removedSet = set(names) remainingColumns = _columnSet - _removedSet for idx, col in remainingColumns: assert col in model.dataFrame().columns.tolist()
def test_editing(self, dataFrame, qtbot): model = DataFrameModel(dataFrame) tableView = QtGui.QTableView() qtbot.addWidget(tableView) tableView.setModel(model) delegate = TextDelegate(tableView) createDelegate(numpy.dtype('O'), 0, tableView) tableView.show() index = model.index(0, 0) preedit_data = index.data() assert not model.editable model.enableEditing(True) tableView.edit(index) editor = tableView.findChildren(QtGui.QLineEdit)[0] qtbot.keyPress(editor, QtCore.Qt.Key_F) qtbot.keyPress(editor, QtCore.Qt.Key_Enter) QtGui.QApplication.processEvents() with qtbot.waitSignal(timeout=100): assert index.data(QtCore.Qt.DisplayRole) == 'f'
def test_rowCount(): model = DataFrameModel(pandas.DataFrame([0], columns=['A'])) assert model.rowCount() == 1 model = DataFrameModel(pandas.DataFrame(numpy.arange(100), columns=['A'])) assert model.rowCount() == 100
def test_initDataFrame(): model = DataFrameModel() assert model.dataFrame().empty
def test_initDataFrameWithDataFrame(): dataFrame = pandas.DataFrame([0], columns=['A']) model = DataFrameModel(dataFrame) assert not model.dataFrame().empty assert model.dataFrame() is dataFrame
def dataModel2(): df = pandas.DataFrame([[1,2,3,4,5,6,7,8]], columns=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) model = DataFrameModel(df) return model
def dataModel(): df = pandas.DataFrame([10], columns=['A']) model = DataFrameModel(df) return model
import pandas import numpy import sys from pandasqt.excepthook import excepthook sys.excepthook = excepthook # use QtGui from the compat module to take care if correct sip version, etc. from pandasqt.compat import QtGui from pandasqt.models.DataFrameModel import DataFrameModel from pandasqt.views.DataTableView import DataTableWidget from pandasqt.views._ui import icons_rc """setup a new empty model""" model = DataFrameModel() """setup an application and create a table view widget""" app = QtGui.QApplication([]) widget = DataTableWidget() widget.resize(800, 600) widget.show() """asign the created model""" widget.setViewModel(model) """create some test data""" data = { 'A': [10, 11, 12], 'B': [20, 21, 22], 'C': ['Peter Pan', 'Cpt. Hook', 'Tinkerbell'] } df = pandas.DataFrame(data) """convert the column to the numpy.int8 datatype to test the delegates in the table int8 is limited to -128-127 """ df['A'] = df['A'].astype(numpy.int8)
def model(self, dataFrame): return DataFrameModel(dataFrame)
def test_headerData(orientation, role, index, expectedHeader): model = DataFrameModel(pandas.DataFrame([0], columns=['A'])) assert model.headerData(index, orientation, role) == expectedHeader