Skip to content

Commit 048fb0d

Browse files
committed
DOC: Adding an example of using the DataFrameWidget.
1 parent c7ff72a commit 048fb0d

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

doc/source/visualization.rst

+76
Original file line numberDiff line numberDiff line change
@@ -594,3 +594,79 @@ Andrews curves charts:
594594
595595
@savefig andrews_curve_winter.png
596596
andrews_curves(data, 'Name', colormap='winter')
597+
598+
599+
****************************************
600+
Visualizing your data in Qt applications
601+
****************************************
602+
603+
There is an experimental support for visualizing DataFrames in PyQt4 and PySide
604+
applications. At the moment you can display and edit the values of the cells
605+
in the DataFrame. Qt will take care of displaying just the portion of the
606+
DataFrame that is currently visible and the edits will be immediately saved to
607+
the underlying DataFrame
608+
609+
To demonstrate this we will create a simple PySide application that will switch
610+
between two editable DataFrames. For this will use the ``DataFrameModel`` class
611+
that handles the access to the DataFrame, and the ``DataFrameWidget``, which is
612+
just a thin layer around the ``QTableView``.
613+
614+
.. code-block:: python
615+
616+
import numpy as np
617+
import pandas as pd
618+
from pandas.sandbox.qtpandas import DataFrameModel, DataFrameWidget
619+
from PySide import QtGui, QtCore
620+
621+
# Or if you use PyQt4:
622+
# from PyQt4 import QtGui, QtCore
623+
624+
class MainWidget(QtGui.QWidget):
625+
def __init__(self, parent=None):
626+
super(MainWidget, self).__init__(parent)
627+
628+
# Create two DataFrames
629+
self.df1 = pd.DataFrame(np.arange(9).reshape(3, 3),
630+
columns=['foo', 'bar', 'baz'])
631+
self.df2 = pd.DataFrame({
632+
'int': [1, 2, 3],
633+
'float': [1.5, 2.5, 3.5],
634+
'string': ['a', 'b', 'c'],
635+
'nan': [np.nan, np.nan, np.nan]
636+
}, index=['AAA', 'BBB', 'CCC'],
637+
columns=['int', 'float', 'string', 'nan'])
638+
639+
# Create the widget and set the first DataFrame
640+
self.widget = DataFrameWidget(self.df1)
641+
642+
# Create the buttons for changing DataFrames
643+
self.button_first = QtGui.QPushButton('First')
644+
self.button_first.clicked.connect(self.on_first_click)
645+
self.button_second = QtGui.QPushButton('Second')
646+
self.button_second.clicked.connect(self.on_second_click)
647+
648+
# Set the layout
649+
vbox = QtGui.QVBoxLayout()
650+
vbox.addWidget(self.widget)
651+
hbox = QtGui.QHBoxLayout()
652+
hbox.addWidget(self.button_first)
653+
hbox.addWidget(self.button_second)
654+
vbox.addLayout(hbox)
655+
self.setLayout(vbox)
656+
657+
def on_first_click(self):
658+
'''Sets the first DataFrame'''
659+
self.widget.setDataFrame(self.df1)
660+
661+
def on_second_click(self):
662+
'''Sets the second DataFrame'''
663+
self.widget.setDataFrame(self.df2)
664+
665+
if __name__ == '__main__':
666+
import sys
667+
668+
# Initialize the application
669+
app = QtGui.QApplication(sys.argv)
670+
mw = MainWidget()
671+
mw.show()
672+
app.exec_()

0 commit comments

Comments
 (0)