|
29 | 29 | from pandas._libs.tslibs import timezones |
30 | 30 | from pandas.compat._optional import import_optional_dependency |
31 | 31 | from pandas.errors import PerformanceWarning |
| 32 | +from pandas.util._decorators import cache_readonly |
32 | 33 |
|
33 | 34 | from pandas.core.dtypes.common import ( |
34 | 35 | ensure_object, |
@@ -3522,43 +3523,39 @@ def validate_min_itemsize(self, min_itemsize): |
3522 | 3523 | "data_column" |
3523 | 3524 | ) |
3524 | 3525 |
|
3525 | | - @property |
| 3526 | + @cache_readonly |
3526 | 3527 | def indexables(self): |
3527 | 3528 | """ 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 | + ) |
3529 | 3540 |
|
3530 | | - self._indexables = [] |
| 3541 | + # values columns |
| 3542 | + dc = set(self.data_columns) |
| 3543 | + base_pos = len(_indexables) |
3531 | 3544 |
|
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 |
3540 | 3552 | ) |
3541 | 3553 |
|
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)]) |
3560 | 3557 |
|
3561 | | - return self._indexables |
| 3558 | + return _indexables |
3562 | 3559 |
|
3563 | 3560 | def create_index(self, columns=None, optlevel=None, kind: Optional[str] = None): |
3564 | 3561 | """ |
@@ -4140,7 +4137,6 @@ def write(self, **kwargs): |
4140 | 4137 | class AppendableTable(Table): |
4141 | 4138 | """ support the new appendable table formats """ |
4142 | 4139 |
|
4143 | | - _indexables = None |
4144 | 4140 | table_type = "appendable" |
4145 | 4141 |
|
4146 | 4142 | def write( |
@@ -4540,23 +4536,21 @@ def get_attrs(self): |
4540 | 4536 | ] |
4541 | 4537 | self.data_columns = [a.name for a in self.values_axes] |
4542 | 4538 |
|
4543 | | - @property |
| 4539 | + @cache_readonly |
4544 | 4540 | def indexables(self): |
4545 | 4541 | """ create the indexables from the table description """ |
4546 | | - if self._indexables is None: |
4547 | | - |
4548 | | - d = self.description |
| 4542 | + d = self.description |
4549 | 4543 |
|
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)] |
4552 | 4546 |
|
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) |
4555 | 4549 |
|
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) |
4558 | 4552 |
|
4559 | | - return self._indexables |
| 4553 | + return _indexables |
4560 | 4554 |
|
4561 | 4555 | def write(self, **kwargs): |
4562 | 4556 | raise NotImplementedError("cannot write on an generic table") |
|
0 commit comments