def _set_value(self, value): if self._units is None: units = [None] * len(self._columns) else: units = self._units # Build new columns new_cols = [] for col, unit in zip(self._columns, units): if unit is not None: new_col = "{} [{}]".format(col, unit) else: new_col = col new_cols.append(new_col) # setup a new empty model model = DataFrameModel() # set table view widget model self.datatable.setViewModel(model) if value is None: data = pd.DataFrame(columns=new_cols) else: data = value data.columns = new_cols # fill the model with data model.setDataFrame(data) return
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): # print index.data(QtCore.Qt.DisplayRole).toPyObject() assert index.data(QtCore.Qt.DisplayRole).toPyObject() == 'f'
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_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 _previewFile(self): """Updates the preview widgets with new models for both tab panes. """ dataFrame = self._loadCSVDataFrame() dataFrameModel = DataFrameModel(dataFrame) dataFrameModel.enableEditing(True) self._previewTableView.setModel(dataFrameModel) columnModel = dataFrameModel.columnDtypeModel() columnModel.changeFailed.connect(self.updateStatusBar) self._datatypeTableView.setModel(columnModel)
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 _set_value(self, value, dtypes=None): # setup a new model data = self._get_dataframe(value, dtypes) model = DataFrameModel(data) # Modify the appearance and function of the table if fixed rows are # required. if (self._fixed_index_col is not None and self._fixed_index_names is not None): model.freeze_first = True self.datatable.hideVerticalHeader(True) # set table view widget model self.datatable.setViewModel(model) return
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_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 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_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_headerData(orientation, role, index, expectedHeader): model = DataFrameModel(pandas.DataFrame([0], columns=['A'])) assert model.headerData(index, orientation, role) == expectedHeader
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 test_rowCount(): model = DataFrameModel(pandas.DataFrame([0], columns=['A'])) assert model.rowCount() == 1 model = DataFrameModel(pandas.DataFrame(numpy.arange(200), columns=['A'])) assert model.rowCount() == 100
import pandas import numpy import sys from dtocean_qt.excepthook import excepthook sys.excepthook = excepthook # use QtGui from the compat module to take care if correct sip version, etc. from dtocean_qt.compat import QtGui from dtocean_qt.models.DataFrameModel import DataFrameModel from dtocean_qt.views.DataTableView import DataTableWidget from dtocean_qt.views._ui import icons_rc """setup a new empty model""" model = DataFrameModel() model.freeze_first = True """setup an application and create a table view widget""" app = QtGui.QApplication([]) widget = DataTableWidget() widget.resize(800, 600) widget.hideVerticalHeader(True) 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'], 'D': [True, True, False] } df = pandas.DataFrame(data)
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 dataModel(): df = pandas.DataFrame([10], columns=['A']) model = DataFrameModel(df) return model
def test_initDataFrame(): model = DataFrameModel() assert model.dataFrame().empty
def dataModel3(): df = pandas.DataFrame(numpy.arange(200), columns=['A']) model = DataFrameModel(df) return model
def test_initDataFrameWithDataFrame(): dataFrame = pandas.DataFrame([0], columns=['A']) model = DataFrameModel(dataFrame) assert not model.dataFrame().empty assert model.dataFrame() is dataFrame
def model(self, dataFrame): return DataFrameModel(dataFrame)
def _set_value(self, value): if self._units is None: units = [None] * len(self._columns) else: units = self._units # Build new columns new_cols = [] for col, unit in zip(self._columns, units): if unit is not None: new_col = "{} [{}]".format(col, unit) else: new_col = col new_cols.append(new_col) # setup a new empty model model = DataFrameModel() # set table view widget model self.datatable.setViewModel(model) # No data is stored if value is None: data = pd.DataFrame(columns=new_cols) model.setDataFrame(data) return # Check the columns of the stored data against the expected, for # legacy support if len(value.columns) == len(new_cols): safe_cols = new_cols elif len(value.columns) < len(new_cols): # Strip any units clean_cols = [re.sub(r'\s\[[^)]*\]', '', x) for x in new_cols] safe_cols = [] for col in value.columns: match = [ new_cols[clean_cols.index(x)] for x in clean_cols if col == x ] if len(match) == 0: continue safe_cols.append(match[0]) else: extra_cols = len(value.columns) - len(new_cols) err_str = ("Input data has {} more column(s) than defined in " "columns argument").format(extra_cols) raise ValueError(err_str) data = value data.columns = safe_cols # fill the model with data model.setDataFrame(data) return