Skip to content

Commit 9fe43b7

Browse files
takluyverwesm
authored andcommitted
Add columns to IPython tab completions of DataFrame attributes.
1 parent fc78e34 commit 9fe43b7

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

pandas/core/frame.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from StringIO import StringIO
1616
import csv
1717
import operator
18+
import sys
1819
import warnings
1920

2021
from numpy import nan
@@ -550,7 +551,6 @@ def to_string(self, buf=None, columns=None, colSpace=None,
550551
nanRep='NaN', formatters=None, float_format=None,
551552
sparsify=True):
552553
from pandas.core.common import _format, adjoin
553-
import sys
554554

555555
if buf is None: # pragma: no cover
556556
buf = sys.stdout
@@ -629,7 +629,6 @@ def info(self, verbose=True, buf=None):
629629
If False, don't print column count summary
630630
buf : writable buffer, defaults to sys.stdout
631631
"""
632-
import sys
633632
if buf is None: # pragma: no cover
634633
buf = sys.stdout
635634

@@ -3304,6 +3303,22 @@ def _homogenize(data, index, columns, dtype=None):
33043303
def _put_str(s, space):
33053304
return ('%s' % s)[:space].ljust(space)
33063305

3306+
def install_ipython_completers():
3307+
"""Register the DataFrame type with IPython's tab completion machinery, so
3308+
that it knows about accessing column names as attributes."""
3309+
from IPython.utils.generics import complete_object
3310+
3311+
@complete_object.when_type(DataFrame)
3312+
def complete_dataframe(obj, prev_completions):
3313+
return prev_completions + [c for c in obj.columns \
3314+
if py3compat.isidentifier(c)]
3315+
3316+
# Importing IPython brings in about 200 modules, so we want to avoid it unless
3317+
# we're in IPython (when those modules are loaded anyway).
3318+
if "IPython" in sys.modules:
3319+
install_ipython_completers()
3320+
3321+
33073322
if __name__ == '__main__':
33083323
import nose
33093324
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

pandas/util/py3compat.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
import sys
22

33
PY3 = (sys.version_info[0] >= 3)
4+
5+
if PY3:
6+
def isidentifier(s):
7+
return s.isidentifier()
8+
9+
else:
10+
# Python 2
11+
import re
12+
_name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$")
13+
def isidentifier(s, dotted=False):
14+
return bool(_name_re.match(s))

0 commit comments

Comments
 (0)