Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add get_attr behavior for Panel, similar to DataFrame #554

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+31 −0
Split
View
@@ -4,6 +4,7 @@
# pylint: disable=E1103,W0231,W0212,W0621
import operator
+import sys
import numpy as np
from pandas.core.common import (PandasError, _mut_exclusive,
@@ -509,6 +510,14 @@ def set_value(self, item, major, minor, value):
def _box_item_values(self, key, values):
return DataFrame(values, index=self.major_axis, columns=self.minor_axis)
+ def __getattr__(self, name):
+ """After regular attribute access, try looking up the name of an item.
+ This allows simpler access to items for interactive use."""
+ if name in self.items:
+ return self[name]
+ raise AttributeError("'%s' object has no attribute '%s'" %
+ (type(self).__name__, name))
+
def _slice(self, slobj, axis=0):
new_data = self._data.get_slice(slobj, axis=axis)
return self._constructor(new_data)
@@ -1269,3 +1278,22 @@ def _get_distinct_indexes(indexes):
def _monotonic(arr):
return not (arr[1:] < arr[:-1]).any()
+
+def install_ipython_completers(): # pragma: no cover
+ """Register the Panel type with IPython's tab completion machinery, so
+ that it knows about accessing column names as attributes."""
+ from IPython.utils.generics import complete_object
+
+ @complete_object.when_type(Panel)
+ def complete_dataframe(obj, prev_completions):
+ return prev_completions + [c for c in obj.items \
+ if isinstance(c, basestring) and py3compat.isidentifier(c)]
+
+# Importing IPython brings in about 200 modules, so we want to avoid it unless
+# we're in IPython (when those modules are loaded anyway).
+if "IPython" in sys.modules: # pragma: no cover
+ try:
+ install_ipython_completers()
+ except Exception:
+ pass
+
@@ -960,6 +960,9 @@ def test_rename(self):
renamed_nocopy['foo'] = 3.
self.assert_((self.panel['ItemA'].values == 3).all())
+ def test_get_attr(self):
+ assert_frame_equal(self.panel['ItemA'], self.panel.ItemA)
+
class TestLongPanel(unittest.TestCase):
"""
LongPanel no longer exists, but...