Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
enh(io) teach Gidx about len, shape, size
Browse files Browse the repository at this point in the history
  • Loading branch information
ankostis committed Sep 21, 2020
1 parent cfb2218 commit 9061c71
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions wltp/io.py
Expand Up @@ -17,7 +17,7 @@
import functools as fnt
import itertools as itt
import re
from typing import Callable, Iterable, List, Optional, Union
from typing import Callable, Iterable, List, Optional, Sequence, Union

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -122,10 +122,12 @@ class GearMultiIndexer:
"""
Multi-indexer for 2-level df columns like ``(item, gear)`` with 1-based & closed-bracket `gear`.
Example *grid_wots*::
Example 2-level *grid_wots* columns::
p_avail p_avail ... n_foo n_foo
g1 g2 ... g1 g2
g1 g2 ... g1 g2
df.columns = gidx[:]
... Warning::
negative indices might not work as expected if :attr:`gnames` do not start from ``g1``
Expand Down Expand Up @@ -166,16 +168,26 @@ class GearMultiIndexer:
- When `items` are given, you get a "product" MultiIndex:
>>> G.with_item("foo")[1:3]
>>> G = G.with_item("foo")
>>> G[1:3]
MultiIndex([('foo', 'g1'),
('foo', 'g2'),
('foo', 'g3')], )
>>> len(G)
5
>>> G.shape
(5, 2)
>>> G.size
10
Use no `items` to reset them:
>>> G.with_item('foo').with_item()[:]
>>> G = G.with_item()
>>> G[:]
['g1', 'g2', 'g3', 'g4', 'g5']
>>> G.shape
(5,)
- Notice that **G0** changes "negative" indices:
Expand All @@ -186,6 +198,7 @@ class GearMultiIndexer:
['g0', 'g1', 'g2', 'g3', 'g4', 'g5']
>>> G[[-5, -6, -7]]
['g1', 'g0', 'g5']
"""

#: 1st level column(s)
Expand All @@ -206,7 +219,7 @@ class GearMultiIndexer:
def from_ngears(
cls,
ngears: int,
items: Iterable[str] = None,
items: Sequence[str] = None,
gear_namer: GearGenerator = gear_name,
gear0=False,
):
Expand Down Expand Up @@ -317,15 +330,25 @@ def colidx_pairs(
item = (item,)
return pd.MultiIndex.from_tuples(itt.product(item, gnames))

@property
def ng(self):
def __len__(self):
"""
The number of gears extracted from 2-level dataframe.
It equals :attr:`top_gear` if :attr:`gnames` are from 1-->top_gear.
"""
return len(self.gnames)

ng = property(__len__)

@property
def shape(self):
y = (1 + len(self.items),) if self.items else ()
return self.ng, *y

@property
def size(self):
return self.ng * ((len(self.items) + 1) if self.items else 1)


@fnt.lru_cache()
def make_autograph(out_patterns=None, *args, **kw) -> autog.Autograph:
Expand Down

0 comments on commit 9061c71

Please sign in to comment.