From 04cbca3f021d6765127a80e43932cb0a650094b8 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 9 Dec 2019 08:13:39 -0800 Subject: [PATCH 1/2] REF: finish setting pytables attr in constructors --- pandas/io/pytables.py | 46 +++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 2c9c1af5953ff..14c63d0e39c2a 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -2120,10 +2120,6 @@ def set_info(self, info): if idx is not None: self.__dict__.update(idx) - def get_attr(self): - """ set the kind for this column """ - self.kind = getattr(self.attrs, self.kind_attr, None) - def set_attr(self): """ set the kind for this column """ setattr(self.attrs, self.kind_attr, self.kind) @@ -2170,9 +2166,6 @@ def convert(self, values: np.ndarray, nan_rep, encoding: str, errors: str): assert isinstance(values, np.ndarray), type(values) self.values = Int64Index(np.arange(len(values))) - def get_attr(self): - pass - def set_attr(self): pass @@ -2207,6 +2200,7 @@ def __init__( table=None, meta=None, metadata=None, + dtype=None, ): super().__init__( name=name, @@ -2221,7 +2215,7 @@ def __init__( meta=meta, metadata=metadata, ) - self.dtype = None + self.dtype = dtype self.data = None @property @@ -2443,15 +2437,6 @@ def convert(self, values: np.ndarray, nan_rep, encoding: str, errors: str): self.data = own_data - def get_attr(self): - """ get the data for this column """ - self.values = getattr(self.attrs, self.kind_attr, None) - self.dtype = getattr(self.attrs, self.dtype_attr, None) - self.meta = getattr(self.attrs, self.meta_attr, None) - assert self.typ is not None - assert self.dtype is not None - self.kind = _dtype_to_kind(self.dtype) - def set_attr(self): """ set the data for this column """ setattr(self.attrs, self.kind_attr, self.values) @@ -2490,8 +2475,7 @@ def get_atom_timedelta64(cls, shape): class GenericDataIndexableCol(DataIndexableCol): """ represent a generic pytables data column """ - def get_attr(self): - pass + pass class Fixed: @@ -3437,6 +3421,7 @@ def indexables(self): _indexables = [] desc = self.description + table_attrs = self.table.attrs # Note: each of the `name` kwargs below are str, ensured # by the definition in index_cols. @@ -3445,16 +3430,20 @@ def indexables(self): atom = getattr(desc, name) md = self.read_metadata(name) meta = "category" if md is not None else None + + kind_attr = f"{name}_kind" + kind = getattr(table_attrs, kind_attr, None) + index_col = IndexCol( name=name, axis=axis, pos=i, + kind=kind, typ=atom, table=self.table, meta=meta, metadata=md, ) - index_col.get_attr() _indexables.append(index_col) # values columns @@ -3469,18 +3458,29 @@ def f(i, c): atom = getattr(desc, c) adj_name = _maybe_adjust_name(c, self.version) + + # TODO: why kind_attr here? + values = getattr(table_attrs, f"{adj_name}_kind", None) + dtype = getattr(table_attrs, f"{adj_name}_dtype", None) + kind = _dtype_to_kind(dtype) + md = self.read_metadata(c) - meta = "category" if md is not None else None + # TODO: figure out why these two versions of `meta` dont always match. + # meta = "category" if md is not None else None + meta = getattr(table_attrs, f"{adj_name}_meta", None) + obj = klass( name=adj_name, cname=c, + values=values, + kind=kind, pos=base_pos + i, typ=atom, table=self.table, meta=meta, metadata=md, + dtype=dtype, ) - obj.get_attr() return obj # Note: the definition of `values_cols` ensures that each @@ -4501,7 +4501,6 @@ def indexables(self): index_col = GenericIndexCol( name="index", axis=0, table=self.table, meta=meta, metadata=md ) - index_col.get_attr() _indexables = [index_col] @@ -4520,7 +4519,6 @@ def indexables(self): meta=meta, metadata=md, ) - dc.get_attr() _indexables.append(dc) return _indexables From 33effe5102f74663ecd31788adbae8ae99581b50 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 9 Dec 2019 10:05:54 -0800 Subject: [PATCH 2/2] mypy fixup --- pandas/io/pytables.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 14c63d0e39c2a..6847b1b2a08f5 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -2370,6 +2370,7 @@ def convert(self, values: np.ndarray, nan_rep, encoding: str, errors: str): self.data = values own_data = self.data + assert isinstance(own_data, np.ndarray) # for mypy # use the meta if needed meta = _ensure_decoded(self.meta)