Skip to content

Csv import widget #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jan 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion examples/TestApp.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import sys
import pandas
from pandasqt import DataFrameModel, setDelegatesFromDtype, DtypeComboDelegate, DataSearch
from pandasqt.csvwidget import CSVImportDialog, CSVExportDialog
from util import getCsvData, getRandomData

class TestWidget(QtGui.QWidget):
Expand Down Expand Up @@ -45,11 +46,18 @@ def __init__(self, parent=None):

self.buttonCsvData = QtGui.QPushButton("load csv data")
self.buttonRandomData = QtGui.QPushButton("load random data")
self.buttonCsvData.clicked.connect(lambda: self.setDataFrame( getCsvData() ))
importDialog = CSVImportDialog(self)
importDialog.load.connect(self.updateModel)
self.buttonCsvData.clicked.connect(lambda: importDialog.show())
self.buttonRandomData.clicked.connect(lambda: self.setDataFrame( getRandomData(rows=100, columns=100) ))

self.exportDialog = CSVExportDialog(self)

self.buttonCSVExport = QtGui.QPushButton("export to csv")
self.buttonCSVExport.clicked.connect(self._exportModel)
self.buttonLayout = QtGui.QHBoxLayout()
self.buttonLayout.addWidget(self.buttonCsvData)
self.buttonLayout.addWidget(self.buttonCSVExport)
self.buttonLayout.addWidget(self.buttonRandomData)

self.mainLayout = QtGui.QVBoxLayout()
Expand Down Expand Up @@ -115,6 +123,20 @@ def setDataFrame(self, dataFrame):
dataModel.dtypeChanged.connect(self.updateDelegates)
dataModel.changingDtypeFailed.connect(self.changeColumnValue)

@QtCore.pyqtSlot()
def _exportModel(self):
model = self.dataTableView.model()
self.exportDialog.setExportModel(model)
self.exportDialog.show()

@QtCore.pyqtSlot('QAbstractItemModel')
def updateModel(self, model):
self.dataListView.setModel(model)
self.dataTableView.setModel(model)
self.dataComboBox.setModel(model)

self.tableViewColumnDtypes.setModel(model.columnDtypeModel())

def setModelColumn(self, index):
self.dataListView.setModelColumn(index)
self.dataComboBox.setModelColumn(index)
Expand Down
22 changes: 11 additions & 11 deletions examples/testData/test1.csv
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
str_value,unicode_value,timestamp_value,int8_value,int16_value,int32_value,int64_value,uint8_value,uint16_value,uint32_value,uint64_value,float16_value,float32_value,float64_value,float128_value,bool_value
Rom,äöü,1970-04-03 20:00:00+0100,1,1,1,1,1,1,1,1,0.1,0.1,0.1,0.1,TRUE
Berlin,äöü,1985-01-20 08:00:00+0100,4,4,4,4,4,4,4,4,10.8,10.8,10.8,10.8,FALSE
Hamburg,äöü,1990-10-08 10:00:00+0100,10,10,10,10,10,10,10,10,20.55546,20.55546,20.55546,20.55546,FALSE
New York,äöü,1995-05-12 22:00:00+0100,6,6,6,6,6,6,6,6,30.0343,30.0343,30.0343,30.0343,TRUE
London,äöü,1999-03-02 17:00:00+0100,10,10,10,10,10,10,10,10,1,1,1,1,TRUE
Paris,äöü,2001-06-14 15:00:00+0100,19,19,19,19,19,19,19,19,22.54,22.54,22.54,22.54,FALSE
Stockholm,äöü,2005-02-09 12:00:00+0100,22,22,22,22,22,22,22,22,5,5,5,5,TRUE
Amsterdam,äöü,2010-05-06 19:00:00+0100,127,32767,2147483647,9223372036854775807,0,0,0,0,1.11111,1.11111111,1.11111111111111111,1.11111111111111111111,FALSE
Prag,äöü,2013-02-22 14:00,-128,-32768,-2147483648,-9223372036854775808,255,65535,4294967295,18446744073709551615,33.3336,33.3336,33.111111111111111,33.11111111111111111111,FALSE
Warschau,äöü,2013-02-22 14:00,-128,-32768,-2147483648,-9223372036854775808,255,65535,4294967295,18446744073709551615,33.3336,33.3336,33.111111111111111,33.11111111111111111111,FALSE
str_value,unicode_value,timestamp_value,int8_value,int16_value,int32_value,int64_value,uint8_value,uint16_value,uint32_value,uint64_value,float16_value,float32_value,float64_value,bool_value
Rom,äöü,1970-04-03 20:00:00,1,1,1,1,1,1,1,1,0.1,0.1,0.1,TRUE
Berlin,äöü,1985-01-20 08:00:00,4,4,4,4,4,4,4,4,10.8,10.8,10.8,FALSE
Hamburg,äöü,1990-10-08 10:00:00,10,10,10,10,10,10,10,10,20.55546,20.55546,20.55546,FALSE
New York,äöü,1995-05-12 22:00:00,6,6,6,6,6,6,6,6,30.0343,30.0343,30.0343,TRUE
London,äöü,1999-03-02 17:00:00,10,10,10,10,10,10,10,10,1,1,1,TRUE
Paris,äöü,2001-06-14 15:00:00,19,19,19,19,19,19,19,19,22.54,22.54,22.54,FALSE
Stockholm,äöü,2005-02-09 12:00:00,22,22,22,22,22,22,22,22,5,5,5,TRUE
Amsterdam,äöü,2010-05-06 19:00:00,127,32767,2147483647,9223372036854775807,0,0,0,0,1.11111,1.11111111,1.11111111111111111,FALSE
Prag,äöü,2013-02-22 14:00,-128,-32768,-2147483648,-9223372036854775808,255,65535,4294967295,18446744073709551615,33.3336,33.3336,33.111111111111111,FALSE
Warschau,äöü,2013-02-22 14:00,-128,-32768,-2147483648,-9223372036854775808,255,65535,4294967295,18446744073709551615,33.3336,33.3336,33.111111111111111,FALSE
6 changes: 3 additions & 3 deletions examples/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def getCsvData():
"float16_value": numpy.float16,
"float32_value": numpy.float32,
"float64_value": numpy.float64,
"float128_value": numpy.float128,
"bool_value": numpy.bool_,
# "float128_value": numpy.float128,
"bool_value": numpy.bool_
}
delimiter = ","
encoding = "utf-8"
Expand Down Expand Up @@ -50,4 +50,4 @@ def getRandomData(rows=100, columns=5):
data = {}
for column in columns:
data[column] = numpy.random.rand(rows)
return pandas.DataFrame(data, columns=columns)
return pandas.DataFrame(data, columns=columns)
8 changes: 1 addition & 7 deletions pandasqt/BigIntSpinbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
@author: Matthias Ludwig - Datalyze Solutions
"""

import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)

from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.QtCore import Qt
from pandasqt.compat import Qt, QtCore, QtGui

class BigIntSpinbox(QtGui.QAbstractSpinBox):
"""Custom spinbox for very big integers (like numpy.int64 and uint64)
Expand Down
37 changes: 19 additions & 18 deletions pandasqt/ColumnDtypeModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,8 @@
@author: Matthias Ludwig - Datalyze Solutions
"""

import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)

try:
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.QtCore import Qt
except ImportError:
from PySide import QtCore
from PySide import QtGui
from PySide.QtCore import Qt
from pandasqt.compat import Qt, QtCore, QtGui


import pandas
import numpy as np
Expand All @@ -30,10 +20,11 @@ class ColumnDtypeModel(QtCore.QAbstractTableModel):

Attributes:
dtypeChanged (QtCore.pyqtSignal(columnName)): emitted after a column has changed it's data type.
changingDtypeFailed (QtCore.pyqtSignal(columnName, index, dtype)): emitted after a column has changed it's data type.
changeFailed (QtCore.pyqtSignal('QString')): emitted if a column
datatype could not be changed. An errormessage is provided.
"""
dtypeChanged = QtCore.pyqtSignal(object)
changingDtypeFailed = QtCore.pyqtSignal(object, QtCore.QModelIndex, object)
changeFailed = QtCore.pyqtSignal('QString')

def __init__(self, dataFrame=None, language='en', autoApplyChanges=True):
"""the __init__ method.
Expand Down Expand Up @@ -171,6 +162,8 @@ def data(self, index, role=Qt.DisplayRole):

if role == Qt.DisplayRole or role == Qt.EditRole:
if col == 0:
if columnName == index.row():
return index.row()
return columnName
elif col == 1:
return self._dtypeTranslator.tr(columnDtype)
Expand Down Expand Up @@ -212,21 +205,29 @@ def setData(self, index, value, role=DTYPE_CHANGE_ROLE):
self.layoutAboutToBeChanged.emit()

dtype, language = self._dtypeTranslator.lookup(value)

currentDtype = np.dtype(index.data(role=DTYPE_ROLE))
if dtype is not None:
if dtype != np.dtype(index.data(role=DTYPE_ROLE)):
if dtype != currentDtype:
col = index.column()
#row = self._dataFrame.columns[index.column()]
columnName = self._dataFrame.columns[index.row()]

if self.autoApplyChanges():
try:
self._dataFrame[columnName] = self._dataFrame[columnName].astype(dtype)
if dtype == np.dtype('<M8[ns]'):
self._dataFrame[columnName] = self._dataFrame[columnName].apply(pandas.to_datetime)
else:
self._dataFrame[columnName] = self._dataFrame[columnName].astype(dtype)
self.layoutChanged.emit()
self.dtypeChanged.emit(columnName)
return True
except Exception as e:
raise NotImplementedError, "dtype changing not fully working, original error:\n{}".format(e)
message = 'Could not change datatype %s of column %s to datatype %s' % (currentDtype, columnName, dtype)
self.changeFailed.emit(message)
# self._dataFrame[columnName] = self._dataFrame[columnName].astype(currentDtype)
# self.layoutChanged.emit()
# self.dtypeChanged.emit(columnName)
#raise NotImplementedError, "dtype changing not fully working, original error:\n{}".format(e)
return False


Expand Down
117 changes: 0 additions & 117 deletions pandasqt/CsvImportWidget.py

This file was deleted.

14 changes: 2 additions & 12 deletions pandasqt/CustomDelegates.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
# -*- coding: utf-8 -*-

import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)

try:
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.QtCore import Qt
except ImportError:
from PySide import QtCore
from PySide import QtGui
from PySide.QtCore import Qt

from pandasqt.compat import Qt, QtCore, QtGui

import numpy
from BigIntSpinbox import BigIntSpinbox
Expand Down
Loading