Ejemplo n.º 1
0
 def get_page(self, start: int, end: int) -> List[List[Any]]:
     ret = []
     columns = self._table.columns
     for i in self._index[start:end]:
         row = [i]
         for name in columns:
             col = self._table[name]
             row.append(remove_nan(col.loc[i]))
         ret.append(row)
     return ret
Ejemplo n.º 2
0
 def to_csv(self, filename, columns=None, sep=','):  # TODO: to be improved
     if columns is None:
         columns = self.columns
     with open(filename, 'wb') as f:
         for i in self.index:
             row = []
             for name in columns:
                 col = self[name]
                 row.append(str(remove_nan(get_physical_base(col).loc[i])))
             row = sep.join(row)
             f.write(row.encode('utf-8'))
             f.write(b'\n')
Ejemplo n.º 3
0
 def to_csv(self,
            filename: str,
            columns: Optional[List[str]] = None,
            sep: str = ",") -> None:  # TODO: to be improved
     if columns is None:
         columns = self.columns
     with open(filename, "wb") as f:
         for i in self.index:
             row = []
             for name in columns:
                 col = self[name]
                 row.append(str(remove_nan(get_physical_base(col).loc[i])))
             f.write(sep.join(row).encode("utf-8"))
             f.write(b"\n")
Ejemplo n.º 4
0
 def remove_nan(d):
     return remove_nan(d)
Ejemplo n.º 5
0
    def to_dict(self,
                orient: str = "dict",
                columns: Optional[List[str]] = None) -> Any:
        # pylint: disable=too-many-branches
        """
        Return a dictionary describing the contents of this columns.

        Parameters
        ----------
        orient : {'dict', 'list', 'split', 'rows', 'datatable', 'records', 'index'}
            TODO
        columns : list or `None`
            TODO
        """
        ret: Dict[str, Any]
        ret2: List[Any]
        if columns is None:
            columns = self.columns
        if orient == "dict":
            ret = dict()
            for name in columns:
                col = self[name]
                ret[name] = {
                    int(k): v
                    for (k, v) in dict(zip(self.index, col.tolist())).items()
                }  # because a custom JSONEncoder cannot fix it
            return ret
        if orient == "list":
            ret = dict()
            for name in columns:
                col = self[name]
                ret[name] = col.tolist()
            return ret
        if orient == "split":
            ret = {"index": list(self.index), "columns": columns}
            data = []
            cols = [self[c] for c in columns]
            for i in self.index:
                line = []
                for col in cols:
                    line.append(get_physical_base(col).loc[i])
                data.append(line)
            ret["data"] = data
            return ret
        if orient == "datatable":
            # not a pandas compliant mode but useful for JS DataTable
            ret2 = []
            for i in self.index:
                line = [i]
                for name in columns:
                    col = self[name]
                    line.append(remove_nan(get_physical_base(col).loc[i]))
                ret2.append(line)
            return ret2
        if orient in ("rows", "records"):
            ret2 = []
            for i in self.index:
                line2 = {}
                for name in columns:
                    col = self[name]
                    line2[name] = get_physical_base(col).loc[i]
                ret2.append(line2)
            return ret2
        if orient == "index":
            ret3: Dict[int, Any] = dict()
            for id_ in self.index:
                line2 = {}
                for name in columns:
                    col = self[name]
                    line2[name] = col.loc[id_]
                ret3[int(id_)] = line2
            return ret3
        raise ValueError(f"to_dict({orient}) not implemented")
Ejemplo n.º 6
0
    def to_dict(self, orient='dict', columns=None):
        # pylint: disable=too-many-branches
        """
        Return a dictionary describing the contents of this columns.

        Parameters
        ----------
        orient : {'dict', 'list', 'split', 'rows', 'record', 'index'}
            TODO
        columns : list or `None`
            TODO
        """
        if columns is None:
            columns = self.columns
        if orient == 'dict':
            ret = OrderedDict()
            for name in columns:
                col = self[name]
                ret[name] = dict(zip(self.index, col.tolist()))
            return ret
        if orient == 'list':
            ret = OrderedDict()
            for name in columns:
                col = self[name]
                ret[name] = col.tolist()
            return ret
        if orient == 'split':
            ret = {'index': self.index.tolist(), 'columns': columns}
            data = []
            cols = [self[c] for c in columns]
            for i in self.index:
                line = []
                for col in cols:
                    #col_i = col[i]
                    #if isinstance(col_i, np.ndarray):
                    #    col_i = col_i.tolist()

                    line.append(get_physical_base(col).loc[i])
                data.append(line)
            ret['data'] = data
            return ret
        #if orient == 'rows':
        #    data = []
        #    cols = [self[c] for c in columns]
        #    for i in self.index:
        #        line = [i]
        #        for col in cols:
        #            line.append(str(col[i]))
        #        data.append(dict(zip(columns, line)))
        #    return data
        if orient == 'datatable':  # not a pandas compliant mode but useful for JS DataTable
            ret = []
            for i in self.index:
                line = [i]
                for name in columns:
                    col = self[name]
                    line.append(remove_nan(get_physical_base(col).loc[i]))
                ret.append(line)
            return ret
        if orient in ('rows', 'records'):
            ret = []
            for i in self.index:
                line = OrderedDict()
                for name in columns:
                    col = self[name]
                    #line[name] = remove_nan_etc(col.values[i])
                    line[name] = get_physical_base(col).loc[i]
                ret.append(line)
            return ret
        if orient == 'index':
            ret = OrderedDict()
            for id_ in self.index:
                line = {}
                for name in columns:
                    col = self[name]
                    #line[name] = remove_nan_etc(col.values[i])
                    line[name] = col.loc[id_]
                ret[int(id_)] = line
            return ret
        raise ValueError(
            "to_dict(orient) not implemented for orient={}".format(orient))