Example #1
0
 def coldata(self, key):
     '''
     Return column data as one dimension array.
     
     :param key: (*string*) Column name.
     
     :returns: (*NDArray*) Colomn data.
     '''
     if isinstance(key, str):
         print key
         values = self.data.getColumnData(key).getDataValues()
         return NDArray(ArrayUtil.array(values))
     return None
Example #2
0
    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 = []
                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 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)