-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data.py
53 lines (50 loc) · 1.75 KB
/
Data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import numpy as np
import wx
import wx.lib.sheet
import pandas as pd
class Data(wx.lib.sheet.CSheet):
parent, data = None, None
def __init__(self, parent):
wx.lib.sheet.CSheet.__init__(self, parent)
self.SetNumberRows(0)
self.SetNumberCols(0)
self.data = pd.DataFrame()
def readFile(self, filename):
if filename.endswith("csv"):
self.data = pd.read_csv(filename, delimiter=None, sep=None)
rows, cols = self.data.shape
self.SetNumberCols(cols)
self.SetNumberRows(rows)
self.fill()
def fill(self):
for i, name in enumerate(self.data.columns):
self.SetColLabelValue(i, name)
for j, val in enumerate(self.data[name]):
self.SetCellValue(j, i, str(val))
def __getitem__(self, key):
try:
return self.data[key].dropna()
except KeyError:
try:
return self.data[self.data.columns[key]].dropna() #allow numerical access
except (ValueError, IndexError):
dlg = wx.MessageDialog(self.parent, "Invalid Column",
style= wx.OK | wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
raise KeyError
def __len__(self):
return len(self.data)
def names(self):
return self.data.columns
def min(self):
return np.nanmin(self.data)
def max(self):
return np.nanmax(self.data)
def shape(self):
return self.data.shape
def OnCellChange(self, evt):
colName = self.GetColLabelValue(evt.Col)
v = self.GetCellValue(evt.Row, evt.Col)
t = self.data[colName].dtype.type
self.data.loc[evt.Row, colName] = t(v) # make uniform types