Skip to content

Commit 9a87386

Browse files
jbrockmendelproost
authored andcommitted
REF: make indexables cache_readonly (pandas-dev#30016)
1 parent 32612b6 commit 9a87386

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

pandas/io/pytables.py

+36-42
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from pandas._libs.tslibs import timezones
3030
from pandas.compat._optional import import_optional_dependency
3131
from pandas.errors import PerformanceWarning
32+
from pandas.util._decorators import cache_readonly
3233

3334
from pandas.core.dtypes.common import (
3435
ensure_object,
@@ -3522,43 +3523,39 @@ def validate_min_itemsize(self, min_itemsize):
35223523
"data_column"
35233524
)
35243525

3525-
@property
3526+
@cache_readonly
35263527
def indexables(self):
35273528
""" create/cache the indexables if they don't exist """
3528-
if self._indexables is None:
3529+
_indexables = []
3530+
3531+
# Note: each of the `name` kwargs below are str, ensured
3532+
# by the definition in index_cols.
3533+
# index columns
3534+
_indexables.extend(
3535+
[
3536+
IndexCol(name=name, axis=axis, pos=i)
3537+
for i, (axis, name) in enumerate(self.attrs.index_cols)
3538+
]
3539+
)
35293540

3530-
self._indexables = []
3541+
# values columns
3542+
dc = set(self.data_columns)
3543+
base_pos = len(_indexables)
35313544

3532-
# Note: each of the `name` kwargs below are str, ensured
3533-
# by the definition in index_cols.
3534-
# index columns
3535-
self._indexables.extend(
3536-
[
3537-
IndexCol(name=name, axis=axis, pos=i)
3538-
for i, (axis, name) in enumerate(self.attrs.index_cols)
3539-
]
3545+
def f(i, c):
3546+
assert isinstance(c, str)
3547+
klass = DataCol
3548+
if c in dc:
3549+
klass = DataIndexableCol
3550+
return klass.create_for_block(
3551+
i=i, name=c, pos=base_pos + i, version=self.version
35403552
)
35413553

3542-
# values columns
3543-
dc = set(self.data_columns)
3544-
base_pos = len(self._indexables)
3545-
3546-
def f(i, c):
3547-
assert isinstance(c, str)
3548-
klass = DataCol
3549-
if c in dc:
3550-
klass = DataIndexableCol
3551-
return klass.create_for_block(
3552-
i=i, name=c, pos=base_pos + i, version=self.version
3553-
)
3554-
3555-
# Note: the definition of `values_cols` ensures that each
3556-
# `c` below is a str.
3557-
self._indexables.extend(
3558-
[f(i, c) for i, c in enumerate(self.attrs.values_cols)]
3559-
)
3554+
# Note: the definition of `values_cols` ensures that each
3555+
# `c` below is a str.
3556+
_indexables.extend([f(i, c) for i, c in enumerate(self.attrs.values_cols)])
35603557

3561-
return self._indexables
3558+
return _indexables
35623559

35633560
def create_index(self, columns=None, optlevel=None, kind: Optional[str] = None):
35643561
"""
@@ -4140,7 +4137,6 @@ def write(self, **kwargs):
41404137
class AppendableTable(Table):
41414138
""" support the new appendable table formats """
41424139

4143-
_indexables = None
41444140
table_type = "appendable"
41454141

41464142
def write(
@@ -4540,23 +4536,21 @@ def get_attrs(self):
45404536
]
45414537
self.data_columns = [a.name for a in self.values_axes]
45424538

4543-
@property
4539+
@cache_readonly
45444540
def indexables(self):
45454541
""" create the indexables from the table description """
4546-
if self._indexables is None:
4547-
4548-
d = self.description
4542+
d = self.description
45494543

4550-
# the index columns is just a simple index
4551-
self._indexables = [GenericIndexCol(name="index", axis=0)]
4544+
# the index columns is just a simple index
4545+
_indexables = [GenericIndexCol(name="index", axis=0)]
45524546

4553-
for i, n in enumerate(d._v_names):
4554-
assert isinstance(n, str)
4547+
for i, n in enumerate(d._v_names):
4548+
assert isinstance(n, str)
45554549

4556-
dc = GenericDataIndexableCol(name=n, pos=i, values=[n])
4557-
self._indexables.append(dc)
4550+
dc = GenericDataIndexableCol(name=n, pos=i, values=[n])
4551+
_indexables.append(dc)
45584552

4559-
return self._indexables
4553+
return _indexables
45604554

45614555
def write(self, **kwargs):
45624556
raise NotImplementedError("cannot write on an generic table")

0 commit comments

Comments
 (0)