|
4 | 4 | # pylint: disable=E1103,W0231,W0212,W0621
|
5 | 5 |
|
6 | 6 | import operator
|
| 7 | +import sys |
7 | 8 | import numpy as np
|
8 | 9 |
|
9 | 10 | from pandas.core.common import (PandasError, _mut_exclusive,
|
@@ -509,6 +510,14 @@ def set_value(self, item, major, minor, value):
|
509 | 510 | def _box_item_values(self, key, values):
|
510 | 511 | return DataFrame(values, index=self.major_axis, columns=self.minor_axis)
|
511 | 512 |
|
| 513 | + def __getattr__(self, name): |
| 514 | + """After regular attribute access, try looking up the name of an item. |
| 515 | + This allows simpler access to items for interactive use.""" |
| 516 | + if name in self.items: |
| 517 | + return self[name] |
| 518 | + raise AttributeError("'%s' object has no attribute '%s'" % |
| 519 | + (type(self).__name__, name)) |
| 520 | + |
512 | 521 | def _slice(self, slobj, axis=0):
|
513 | 522 | new_data = self._data.get_slice(slobj, axis=axis)
|
514 | 523 | return self._constructor(new_data)
|
@@ -1191,3 +1200,22 @@ def _get_distinct_indexes(indexes):
|
1191 | 1200 |
|
1192 | 1201 | def _monotonic(arr):
|
1193 | 1202 | return not (arr[1:] < arr[:-1]).any()
|
| 1203 | + |
| 1204 | +def install_ipython_completers(): # pragma: no cover |
| 1205 | + """Register the Panel type with IPython's tab completion machinery, so |
| 1206 | + that it knows about accessing column names as attributes.""" |
| 1207 | + from IPython.utils.generics import complete_object |
| 1208 | + |
| 1209 | + @complete_object.when_type(Panel) |
| 1210 | + def complete_dataframe(obj, prev_completions): |
| 1211 | + return prev_completions + [c for c in obj.items \ |
| 1212 | + if isinstance(c, basestring) and py3compat.isidentifier(c)] |
| 1213 | + |
| 1214 | +# Importing IPython brings in about 200 modules, so we want to avoid it unless |
| 1215 | +# we're in IPython (when those modules are loaded anyway). |
| 1216 | +if "IPython" in sys.modules: # pragma: no cover |
| 1217 | + try: |
| 1218 | + install_ipython_completers() |
| 1219 | + except Exception: |
| 1220 | + pass |
| 1221 | + |
0 commit comments