-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f92c916
commit 92ee89c
Showing
6 changed files
with
728 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from PyQt5 import QtCore | ||
|
||
import pandas as pd | ||
|
||
class PandasModel(QtCore.QAbstractTableModel): | ||
def __init__(self, df = pd.DataFrame(), parent=None): | ||
QtCore.QAbstractTableModel.__init__(self, parent=parent) | ||
self._df = df | ||
|
||
def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole): | ||
if role != QtCore.Qt.DisplayRole: | ||
return QtCore.QVariant() | ||
|
||
if orientation == QtCore.Qt.Horizontal: | ||
try: | ||
return self._df.columns.tolist()[section] | ||
except (IndexError, ): | ||
return QtCore.QVariant() | ||
elif orientation == QtCore.Qt.Vertical: | ||
try: | ||
# return self.df.index.tolist() | ||
return self._df.index.tolist()[section] | ||
except (IndexError, ): | ||
return QtCore.QVariant() | ||
|
||
def data(self, index, role=QtCore.Qt.DisplayRole): | ||
if role != QtCore.Qt.DisplayRole: | ||
return QtCore.QVariant() | ||
|
||
if not index.isValid(): | ||
return QtCore.QVariant() | ||
|
||
return QtCore.QVariant(str(self._df.ix[index.row(), index.column()])) | ||
|
||
def setData(self, index, value, role): | ||
row = self._df.index[index.row()] | ||
col = self._df.columns[index.column()] | ||
if hasattr(value, 'toPyObject'): | ||
# PyQt4 gets a QVariant | ||
value = value.toPyObject() | ||
else: | ||
# PySide gets an unicode | ||
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, parent=QtCore.QModelIndex()): | ||
return len(self._df.index) | ||
|
||
def columnCount(self, parent=QtCore.QModelIndex()): | ||
return len(self._df.columns) | ||
|
||
def sort(self, column, order): | ||
colname = self._df.columns.tolist()[column] | ||
self.layoutAboutToBeChanged.emit() | ||
self._df.sort_values(colname, ascending= order == QtCore.Qt.AscendingOrder, inplace=True) | ||
self._df.reset_index(inplace=True, drop=True) | ||
self.layoutChanged.emit() |
Oops, something went wrong.