Search for specified dimension in dataset and return level (referred to by either label or position) and axis {0 or ‘index’, 1 or ‘columns’} If default_idx is True, return a single unnamed index if present
(df,dim,default_idx=False)
| 1592 | self.fieldlimits[field] = [None,None] |
| 1593 | |
| 1594 | def _get_dim(df,dim,default_idx=False): |
| 1595 | """ |
| 1596 | Search for specified dimension in dataset and return |
| 1597 | level (referred to by either label or position) and |
| 1598 | axis {0 or ‘index’, 1 or ‘columns’} |
| 1599 | |
| 1600 | If default_idx is True, return a single unnamed index |
| 1601 | if present |
| 1602 | """ |
| 1603 | assert(dim in dimension_names.keys()), \ |
| 1604 | "Dimension '"+dim+"' not supported" |
| 1605 | |
| 1606 | # 1. Try to find dim based on name |
| 1607 | for name in dimension_names[dim]: |
| 1608 | if name in df.index.names: |
| 1609 | if debug: print("Found "+dim+" dimension in index with name '{}'".format(name)) |
| 1610 | return name, 0 |
| 1611 | else: |
| 1612 | try: |
| 1613 | if name in df.columns: |
| 1614 | if debug: print("Found "+dim+" dimension in column with name '{}'".format(name)) |
| 1615 | return name, 1 |
| 1616 | except AttributeError: |
| 1617 | # pandas Series has no columns |
| 1618 | pass |
| 1619 | |
| 1620 | # 2. Look for Datetime or Timedelta index |
| 1621 | if dim=='time': |
| 1622 | for idx in range(len(df.index.names)): |
| 1623 | if isinstance(df.index.get_level_values(idx),(pd.DatetimeIndex,pd.TimedeltaIndex,pd.PeriodIndex)): |
| 1624 | if debug: print("Found "+dim+" dimension in index with level {} without a name ".format(idx)) |
| 1625 | return idx, 0 |
| 1626 | |
| 1627 | # 3. If default index is True, assume that a |
| 1628 | # single nameless index corresponds to the |
| 1629 | # requested dimension |
| 1630 | if (not isinstance(df.index,(pd.MultiIndex,pd.DatetimeIndex,pd.TimedeltaIndex,pd.PeriodIndex)) |
| 1631 | and default_idx and (df.index.name is None) ): |
| 1632 | if debug: print("Assuming nameless index corresponds to '{}' dimension".format(dim)) |
| 1633 | return 0,0 |
| 1634 | |
| 1635 | # 4. Did not found requested dimension |
| 1636 | if debug: print("Found no "+dim+" dimension") |
| 1637 | return None, None |
| 1638 | |
| 1639 | |
| 1640 | def _get_available_fieldnames(df,fieldnames): |
no outgoing calls
no test coverage detected