Skip to content

Commit 09b3c0a

Browse files
committed
Merge pull request #5050 from jreback/dir
API: provide __dir__ method (and local context) for tab completion / remove ipython completers code
2 parents dce05cf + c7f6560 commit 09b3c0a

File tree

6 files changed

+21
-68
lines changed

6 files changed

+21
-68
lines changed

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ API Changes
274274
support ``pow`` or ``mod`` with non-scalars. (:issue:`3765`)
275275
- Provide numpy compatibility with 1.7 for a calling convention like ``np.prod(pandas_object)`` as numpy
276276
call with additional keyword args (:issue:`4435`)
277+
- Provide __dir__ method (and local context) for tab completion / remove ipython completers code
278+
(:issue:`4501`)
277279

278280

279281
Internal Refactoring

pandas/core/base.py

+10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ def __repr__(self):
4848
"""
4949
return str(self)
5050

51+
def _local_dir(self):
52+
""" provide addtional __dir__ for this object """
53+
return []
54+
55+
def __dir__(self):
56+
"""
57+
Provide method name lookup and completion
58+
Only provide 'public' methods
59+
"""
60+
return list(sorted(list(set(dir(type(self)) + self._local_dir()))))
5161

5262
class PandasObject(StringMixin):
5363
"""baseclass for various pandas objects"""

pandas/core/frame.py

-19
Original file line numberDiff line numberDiff line change
@@ -4674,25 +4674,6 @@ def _put_str(s, space):
46744674
return ('%s' % s)[:space].ljust(space)
46754675

46764676

4677-
def install_ipython_completers(): # pragma: no cover
4678-
"""Register the DataFrame type with IPython's tab completion machinery, so
4679-
that it knows about accessing column names as attributes."""
4680-
from IPython.utils.generics import complete_object
4681-
4682-
@complete_object.when_type(DataFrame)
4683-
def complete_dataframe(obj, prev_completions):
4684-
return prev_completions + [c for c in obj.columns
4685-
if isinstance(c, compat.string_types) and compat.isidentifier(c)]
4686-
4687-
4688-
# Importing IPython brings in about 200 modules, so we want to avoid it unless
4689-
# we're in IPython (when those modules are loaded anyway).
4690-
if "IPython" in sys.modules: # pragma: no cover
4691-
try:
4692-
install_ipython_completers()
4693-
except Exception:
4694-
pass
4695-
46964677
#----------------------------------------------------------------------
46974678
# Add plotting methods to DataFrame
46984679

pandas/core/generic.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import pandas.core.common as com
1717
import pandas.core.datetools as datetools
1818
from pandas import compat, _np_version_under1p7
19-
from pandas.compat import map, zip, lrange
19+
from pandas.compat import map, zip, lrange, string_types, isidentifier
2020
from pandas.core.common import (isnull, notnull, is_list_like,
2121
_values_from_object,
2222
_infer_dtype_from_scalar, _maybe_promote,
@@ -109,6 +109,11 @@ def __unicode__(self):
109109
prepr = '[%s]' % ','.join(map(com.pprint_thing, self))
110110
return '%s(%s)' % (self.__class__.__name__, prepr)
111111

112+
def _local_dir(self):
113+
""" add the string-like attributes from the info_axis """
114+
return [c for c in self._info_axis
115+
if isinstance(c, string_types) and isidentifier(c) ]
116+
112117
@property
113118
def _constructor_sliced(self):
114119
raise NotImplementedError
@@ -252,7 +257,7 @@ def _get_axis_number(self, axis):
252257

253258
def _get_axis_name(self, axis):
254259
axis = self._AXIS_ALIASES.get(axis, axis)
255-
if isinstance(axis, compat.string_types):
260+
if isinstance(axis, string_types):
256261
if axis in self._AXIS_NUMBERS:
257262
return axis
258263
else:
@@ -1326,7 +1331,7 @@ def filter(self, items=None, like=None, regex=None, axis=None):
13261331
if items is not None:
13271332
return self.reindex(**{axis_name: [r for r in items if r in axis_values]})
13281333
elif like:
1329-
matchf = lambda x: (like in x if isinstance(x, compat.string_types)
1334+
matchf = lambda x: (like in x if isinstance(x, string_types)
13301335
else like in str(x))
13311336
return self.select(matchf, axis=axis_name)
13321337
elif regex:
@@ -2616,7 +2621,7 @@ def tshift(self, periods=1, freq=None, axis=0, **kwds):
26162621

26172622
offset = _resolve_offset(freq, kwds)
26182623

2619-
if isinstance(offset, compat.string_types):
2624+
if isinstance(offset, string_types):
26202625
offset = datetools.to_offset(offset)
26212626

26222627
block_axis = self._get_block_manager_axis(axis)

pandas/core/groupby.py

-27
Original file line numberDiff line numberDiff line change
@@ -2704,30 +2704,3 @@ def numpy_groupby(data, labels, axis=0):
27042704
group_sums = np.add.reduceat(ordered_data, groups_at, axis=axis)
27052705

27062706
return group_sums
2707-
2708-
#-----------------------------------------------------------------------
2709-
# Helper functions
2710-
2711-
2712-
from pandas import compat
2713-
import sys
2714-
2715-
2716-
def install_ipython_completers(): # pragma: no cover
2717-
"""Register the DataFrame type with IPython's tab completion machinery, so
2718-
that it knows about accessing column names as attributes."""
2719-
from IPython.utils.generics import complete_object
2720-
2721-
@complete_object.when_type(DataFrameGroupBy)
2722-
def complete_dataframe(obj, prev_completions):
2723-
return prev_completions + [c for c in obj.obj.columns
2724-
if isinstance(c, compat.string_types) and compat.isidentifier(c)]
2725-
2726-
2727-
# Importing IPython brings in about 200 modules, so we want to avoid it unless
2728-
# we're in IPython (when those modules are loaded anyway).
2729-
if "IPython" in sys.modules: # pragma: no cover
2730-
try:
2731-
install_ipython_completers()
2732-
except Exception:
2733-
pass

pandas/core/panel.py

-18
Original file line numberDiff line numberDiff line change
@@ -1230,21 +1230,3 @@ def f(self, other, axis=0):
12301230
LongPanel = DataFrame
12311231

12321232

1233-
def install_ipython_completers(): # pragma: no cover
1234-
"""Register the Panel type with IPython's tab completion machinery, so
1235-
that it knows about accessing column names as attributes."""
1236-
from IPython.utils.generics import complete_object
1237-
1238-
@complete_object.when_type(Panel)
1239-
def complete_dataframe(obj, prev_completions):
1240-
return prev_completions + [c for c in obj.keys()
1241-
if isinstance(c, compat.string_types)
1242-
and compat.isidentifier(c)]
1243-
1244-
# Importing IPython brings in about 200 modules, so we want to avoid it unless
1245-
# we're in IPython (when those modules are loaded anyway).
1246-
if "IPython" in sys.modules: # pragma: no cover
1247-
try:
1248-
install_ipython_completers()
1249-
except Exception:
1250-
pass

0 commit comments

Comments
 (0)