From c4148483d32f4848737ecd652074c9ca23444e53 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 6 Dec 2019 15:24:40 -0800 Subject: [PATCH] REF: implement io.pytables._maybe_adjust_name (#30100) --- pandas/io/pytables.py | 57 ++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 04e0255bb6d42..a95d7f39ab82c 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -2222,26 +2222,12 @@ class DataCol(IndexCol): _info_fields = ["tz", "ordered"] @classmethod - def create_for_block( - cls, i: int, name=None, version=None, pos: Optional[int] = None - ): + def create_for_block(cls, name: str, version, pos: int): """ return a new datacol with the block i """ + assert isinstance(name, str) - cname = name or f"values_block_{i}" - if name is None: - name = cname - - # prior to 0.10.1, we named values blocks like: values_block_0 an the - # name values_0 - try: - if version[0] == 0 and version[1] <= 10 and version[2] == 0: - m = re.search(r"values_block_(\d+)", name) - if m: - grp = m.groups()[0] - name = f"values_{grp}" - except IndexError: - pass - + cname = name + name = _maybe_adjust_name(name, version) return cls(name=name, cname=cname, pos=pos) def __init__( @@ -3535,7 +3521,7 @@ def f(i, c): if c in dc: klass = DataIndexableCol return klass.create_for_block( - i=i, name=c, pos=base_pos + i, version=self.version + name=c, pos=base_pos + i, version=self.version ) # Note: the definition of `values_cols` ensures that each @@ -3914,16 +3900,16 @@ def get_blk_items(mgr, blocks): encoding=self.encoding, errors=self.errors, ) + adj_name = _maybe_adjust_name(new_name, self.version) typ = klass._get_atom(data_converted) - col = klass.create_for_block(i=i, name=new_name, version=self.version) - col.values = list(b_items) - col.typ = typ + col = klass( + name=adj_name, cname=new_name, values=list(b_items), typ=typ, pos=j + ) col.set_atom(block=b) col.set_data(data_converted) col.update_info(self.info) - col.set_pos(j) vaxes.append(col) @@ -4948,6 +4934,31 @@ def _need_convert(kind) -> bool: return False +def _maybe_adjust_name(name: str, version) -> str: + """ + Prior to 0.10.1, we named values blocks like: values_block_0 an the + name values_0, adjust the given name if necessary. + + Parameters + ---------- + name : str + version : Tuple[int, int, int] + + Returns + ------- + str + """ + try: + if version[0] == 0 and version[1] <= 10 and version[2] == 0: + m = re.search(r"values_block_(\d+)", name) + if m: + grp = m.groups()[0] + name = f"values_{grp}" + except IndexError: + pass + return name + + class Selection: """ Carries out a selection operation on a tables.Table object.