Construct BlockManager from string description. String description syntax looks similar to np.matrix initializer. It looks like this:: a,b,c: f8; d,e,f: i8 Rules are rather simple: * see list of supported datatypes in `create_block` method * components are semic
(descr, item_shape=None)
| 180 | |
| 181 | |
| 182 | def create_mgr(descr, item_shape=None): |
| 183 | """ |
| 184 | Construct BlockManager from string description. |
| 185 | |
| 186 | String description syntax looks similar to np.matrix initializer. It looks |
| 187 | like this:: |
| 188 | |
| 189 | a,b,c: f8; d,e,f: i8 |
| 190 | |
| 191 | Rules are rather simple: |
| 192 | |
| 193 | * see list of supported datatypes in `create_block` method |
| 194 | * components are semicolon-separated |
| 195 | * each component is `NAME,NAME,NAME: DTYPE_ID` |
| 196 | * whitespace around colons & semicolons are removed |
| 197 | * components with same DTYPE_ID are combined into single block |
| 198 | * to force multiple blocks with same dtype, use '-SUFFIX':: |
| 199 | |
| 200 | "a:f8-1; b:f8-2; c:f8-foobar" |
| 201 | |
| 202 | """ |
| 203 | if item_shape is None: |
| 204 | item_shape = (N,) |
| 205 | |
| 206 | offset = 0 |
| 207 | mgr_items = [] |
| 208 | block_placements = {} |
| 209 | for d in descr.split(";"): |
| 210 | d = d.strip() |
| 211 | if not len(d): |
| 212 | continue |
| 213 | names, blockstr = d.partition(":")[::2] |
| 214 | blockstr = blockstr.strip() |
| 215 | names = names.strip().split(",") |
| 216 | |
| 217 | mgr_items.extend(names) |
| 218 | placement = list(np.arange(len(names)) + offset) |
| 219 | try: |
| 220 | block_placements[blockstr].extend(placement) |
| 221 | except KeyError: |
| 222 | block_placements[blockstr] = placement |
| 223 | offset += len(names) |
| 224 | |
| 225 | mgr_items = Index(mgr_items) |
| 226 | |
| 227 | blocks = [] |
| 228 | num_offset = 0 |
| 229 | for blockstr, placement in block_placements.items(): |
| 230 | typestr = blockstr.split("-")[0] |
| 231 | blocks.append( |
| 232 | create_block( |
| 233 | typestr, placement, item_shape=item_shape, num_offset=num_offset |
| 234 | ) |
| 235 | ) |
| 236 | num_offset += len(placement) |
| 237 | |
| 238 | sblocks = sorted(blocks, key=lambda b: b.mgr_locs[0]) |
| 239 | return BlockManager( |
no test coverage detected