def timecol(self, colname): ''' Set time column. :param colname: (*string*) The Name of the column which will be set as time column. For time statistic calculation such as daily average. ''' tdata = TimeTableData(self.data.dataTable) tdata.setTimeColName(colname) self.data = tdata; self.timedata = True
def ave_hour(self, colnames): ''' Hourly average function. Time column is needed. :param colnames: (*list*) Column names. :returns: (*PyTableData*) Result table contains some rows of hourly average data of the columns. ''' if not self.timedata: print 'There is no time column!' return None else: cols = self.data.findColumns(colnames) dtable = self.data.ave_Hour(cols) ttd = TimeTableData(dtable) ttd.setTimeColName('Date') return PyTableData(ttd)
def sum_dayofweek(self, colnames, day=None): ''' Day of week summary function. Time column is needed. :param colnames: (*list*) Column names. :returns: (*PyTableData*) Result table contains some rows of day of week summary data of the columns. ''' if not self.timedata: print 'There is no time column!' return None else: cols = self.data.findColumns(colnames) dtable = self.data.sum_DayOfWeek(cols) ttd = TimeTableData(dtable, 'Date') return PyTableData(ttd)
def __getitem__(self, key): if isinstance(key, basestring): coldata = self.data.getColumnData(key) if coldata.getDataType().isNumeric(): return MIArray(ArrayUtil.array(coldata.getDataValues())) elif coldata.getDataType() == DataTypes.Date: vv = coldata.getData() r = [] cal = Calendar.getInstance() for v in vv: cal.setTime(v) year = cal.get(Calendar.YEAR) month = cal.get(Calendar.MONTH) + 1 day = cal.get(Calendar.DAY_OF_MONTH) hour = cal.get(Calendar.HOUR_OF_DAY) minute = cal.get(Calendar.MINUTE) second = cal.get(Calendar.SECOND) dt = datetime.datetime(year, month, day, hour, minute, second) r.append(dt) return r else: return MIArray(ArrayUtil.array(coldata.getData())) hascolkey = True if isinstance(key, tuple): ridx = key[0] cidx = key[1] if isinstance(ridx, int) and isinstance(cidx, int): if ridx < 0: ridx = self.shape[0] + ridx if cidx < 0: cidx = self.shape[1] + cidx return self.data.getValue(ridx, cidx) elif isinstance(ridx, int) and isinstance(cidx, basestring): if ridx < 0: ridx = self.shape[0] + ridx return self.data.getValue(ridx, cidx) else: key = (key, slice(None)) hascolkey = False k = key[0] if isinstance(k, int): sidx = k if sidx < 0: sidx = self.shape[0] + sidx eidx = sidx + 1 step = 1 rowkey = Range(sidx, eidx, step) elif isinstance(k, slice): if isinstance(k.start, basestring): t = miutil.str2date(k.start) t = miutil.jdate(t) sidx = self.data.getTimeIndex(t) if sidx < 0: sidx = 0 else: sidx = 0 if k.start is None else k.start if sidx < 0: sidx = self.shape[0] + sidx if isinstance(k.stop, basestring): t = miutil.str2date(k.stop) t = miutil.jdate(t) eidx = self.data.getTimeIndex(t) + 1 if eidx < 0: eidx = self.shape[0] else: eidx = self.shape[0] if k.stop is None else k.stop if eidx < 0: eidx = self.shape[0] + eidx step = 1 if k.step is None else k.step rowkey = Range(sidx, eidx, step) elif isinstance(k, list): if isinstance(k[0], basestring): tlist = [] for tstr in k: t = miutil.jdate(miutil.str2date(tstr)) idx = self.data.getTimeIndex_Ex(t) if idx >= 0: tlist.append(idx) rowkey = tlist else: rowkey = k else: return None tcolname = self.data.getTimeColName() if not hascolkey: r = self.data.select(rowkey) if r.findColumn(tcolname) is None: r = TableData(r) else: r = TimeTableData(r, tcolname) return PyTableData(r) k = key[1] if isinstance(k, int): sidx = k if sidx < 0: sidx = self.shape[1] + sidx eidx = sidx + 1 step = 1 colkey = Range(sidx, eidx, step) elif isinstance(k, slice): sidx = 0 if k.start is None else k.start if sidx < 0: sidx = self.shape[1] + sidx eidx = self.shape[1] if k.stop is None else k.stop if eidx < 0: eidx = self.shape[1] + eidx step = 1 if k.step is None else k.step colkey = Range(sidx, eidx, step) elif isinstance(k, list): if isinstance(k[0], basestring): cols = self.data.findColumns(k) else: cols = self.data.findColumns_Index(k) colkey = cols elif isinstance(k, basestring): rows = self.data.getRows(rowkey) coldata = self.data.getColumnData(rows, k) if coldata.getDataType().isNumeric(): return MIArray(ArrayUtil.array(coldata.getDataValues())) else: return MIArray(ArrayUtil.array(coldata.getData())) else: return None r = self.data.select(rowkey, colkey) if r.findColumn(tcolname) is None: r = TableData(r) else: r = TimeTableData(r, tcolname) return PyTableData(r)
def __getitem__(self, key): if isinstance(key, basestring): coldata = self.data.getColumnData(key) if coldata.getDataType().isNumeric(): return NDArray(ArrayUtil.array(coldata.getDataValues())) elif coldata.getDataType() == DataType.DATE: vv = coldata.getData() r = [] for v in vv: dt = miutil.pydate(v) r.append(dt) return r else: return NDArray(ArrayUtil.array(coldata.getData())) hascolkey = True if isinstance(key, tuple): ridx = key[0] cidx = key[1] if isinstance(ridx, int) and isinstance(cidx, int): if ridx < 0: ridx = self.shape[0] + ridx if cidx < 0: cidx = self.shape[1] + cidx return self.data.getValue(ridx, cidx) elif isinstance(ridx, int) and isinstance(cidx, basestring): if ridx < 0: ridx = self.shape[0] + ridx return self.data.getValue(ridx, cidx) else: key = (key, slice(None)) hascolkey = False k = key[0] if isinstance(k, int): sidx = k if sidx < 0: sidx = self.shape[0] + sidx eidx = sidx + 1 step = 1 rowkey = Range(sidx, eidx, step) elif isinstance(k, slice): if isinstance(k.start, basestring): t = miutil.str2date(k.start) t = miutil.jdate(t) sidx = self.data.getTimeIndex(t) if sidx < 0: sidx = 0 else: sidx = 0 if k.start is None else k.start if sidx < 0: sidx = self.shape[0] + sidx if isinstance(k.stop, basestring): t = miutil.str2date(k.stop) t = miutil.jdate(t) eidx = self.data.getTimeIndex(t) + 1 if eidx < 0: eidx = self.shape[0] else: eidx = self.shape[0] if k.stop is None else k.stop if eidx < 0: eidx = self.shape[0] + eidx step = 1 if k.step is None else k.step rowkey = Range(sidx, eidx, step) elif isinstance(k, list): if isinstance(k[0], basestring): tlist = [] for tstr in k: t = miutil.jdate(miutil.str2date(tstr)) idx = self.data.getTimeIndex_Ex(t) if idx >= 0: tlist.append(idx) rowkey = tlist else: rowkey = k else: return None tcolname = self.data.getTimeColName() if not hascolkey: r = self.data.select(rowkey) if r.findColumn(tcolname) is None: r = TableData(r) else: r = TimeTableData(r, tcolname) return PyTableData(r) k = key[1] if isinstance(k, int): sidx = k if sidx < 0: sidx = self.shape[1] + sidx eidx = sidx + 1 step = 1 colkey = Range(sidx, eidx, step) elif isinstance(k, slice): sidx = 0 if k.start is None else k.start if sidx < 0: sidx = self.shape[1] + sidx eidx = self.shape[1] if k.stop is None else k.stop if eidx < 0: eidx = self.shape[1] + eidx step = 1 if k.step is None else k.step colkey = Range(sidx, eidx, step) elif isinstance(k, list): if isinstance(k[0], basestring): cols = self.data.findColumns(k) else: cols = self.data.findColumns_Index(k) colkey = cols elif isinstance(k, basestring): rows = self.data.getRows(rowkey) coldata = self.data.getColumnData(rows, k) if coldata.getDataType().isNumeric(): return NDArray(ArrayUtil.array(coldata.getDataValues())) else: return NDArray(ArrayUtil.array(coldata.getData())) else: return None r = self.data.select(rowkey, colkey) if r.findColumn(tcolname) is None: r = TableData(r) else: r = TimeTableData(r, tcolname) return PyTableData(r)