Skip to content

Commit 2032c49

Browse files
jbrockmendelproost
authored andcommitted
REF: implement io.pytables._maybe_adjust_name (pandas-dev#30100)
1 parent 7c0adaf commit 2032c49

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

pandas/io/pytables.py

+34-23
Original file line numberDiff line numberDiff line change
@@ -2222,26 +2222,12 @@ class DataCol(IndexCol):
22222222
_info_fields = ["tz", "ordered"]
22232223

22242224
@classmethod
2225-
def create_for_block(
2226-
cls, i: int, name=None, version=None, pos: Optional[int] = None
2227-
):
2225+
def create_for_block(cls, name: str, version, pos: int):
22282226
""" return a new datacol with the block i """
2227+
assert isinstance(name, str)
22292228

2230-
cname = name or f"values_block_{i}"
2231-
if name is None:
2232-
name = cname
2233-
2234-
# prior to 0.10.1, we named values blocks like: values_block_0 an the
2235-
# name values_0
2236-
try:
2237-
if version[0] == 0 and version[1] <= 10 and version[2] == 0:
2238-
m = re.search(r"values_block_(\d+)", name)
2239-
if m:
2240-
grp = m.groups()[0]
2241-
name = f"values_{grp}"
2242-
except IndexError:
2243-
pass
2244-
2229+
cname = name
2230+
name = _maybe_adjust_name(name, version)
22452231
return cls(name=name, cname=cname, pos=pos)
22462232

22472233
def __init__(
@@ -3535,7 +3521,7 @@ def f(i, c):
35353521
if c in dc:
35363522
klass = DataIndexableCol
35373523
return klass.create_for_block(
3538-
i=i, name=c, pos=base_pos + i, version=self.version
3524+
name=c, pos=base_pos + i, version=self.version
35393525
)
35403526

35413527
# Note: the definition of `values_cols` ensures that each
@@ -3914,16 +3900,16 @@ def get_blk_items(mgr, blocks):
39143900
encoding=self.encoding,
39153901
errors=self.errors,
39163902
)
3903+
adj_name = _maybe_adjust_name(new_name, self.version)
39173904

39183905
typ = klass._get_atom(data_converted)
39193906

3920-
col = klass.create_for_block(i=i, name=new_name, version=self.version)
3921-
col.values = list(b_items)
3922-
col.typ = typ
3907+
col = klass(
3908+
name=adj_name, cname=new_name, values=list(b_items), typ=typ, pos=j
3909+
)
39233910
col.set_atom(block=b)
39243911
col.set_data(data_converted)
39253912
col.update_info(self.info)
3926-
col.set_pos(j)
39273913

39283914
vaxes.append(col)
39293915

@@ -4948,6 +4934,31 @@ def _need_convert(kind) -> bool:
49484934
return False
49494935

49504936

4937+
def _maybe_adjust_name(name: str, version) -> str:
4938+
"""
4939+
Prior to 0.10.1, we named values blocks like: values_block_0 an the
4940+
name values_0, adjust the given name if necessary.
4941+
4942+
Parameters
4943+
----------
4944+
name : str
4945+
version : Tuple[int, int, int]
4946+
4947+
Returns
4948+
-------
4949+
str
4950+
"""
4951+
try:
4952+
if version[0] == 0 and version[1] <= 10 and version[2] == 0:
4953+
m = re.search(r"values_block_(\d+)", name)
4954+
if m:
4955+
grp = m.groups()[0]
4956+
name = f"values_{grp}"
4957+
except IndexError:
4958+
pass
4959+
return name
4960+
4961+
49514962
class Selection:
49524963
"""
49534964
Carries out a selection operation on a tables.Table object.

0 commit comments

Comments
 (0)