From 9969c4eb34706c812b9e514bca9059d37fd08da8 Mon Sep 17 00:00:00 2001 From: Adam Klein Date: Mon, 16 Jan 2012 11:43:01 -0500 Subject: [PATCH 1/5] ENH: ipython_directive to warning on exceptions when building docs --- doc/source/io.rst | 1 + doc/sphinxext/ipython_directive.py | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/doc/source/io.rst b/doc/source/io.rst index c83ef06bfca2f..6c7a60d2fb223 100644 --- a/doc/source/io.rst +++ b/doc/source/io.rst @@ -343,6 +343,7 @@ performance HDF5 format using the excellent `PyTables .. ipython:: python :suppress: + :okexcept: os.remove('store.h5') diff --git a/doc/sphinxext/ipython_directive.py b/doc/sphinxext/ipython_directive.py index 2f60b173339e4..c04c3475da459 100644 --- a/doc/sphinxext/ipython_directive.py +++ b/doc/sphinxext/ipython_directive.py @@ -196,7 +196,6 @@ def __init__(self): self.cout = cStringIO.StringIO() - # Create config object for IPython config = Config() config.Global.display_banner = False @@ -215,6 +214,20 @@ def __init__(self): # Create and initialize ipython, but don't start its mainloop IP = InteractiveShell.instance(config=config, profile_dir=profile) + + self.config = config + + def custom_handler(self, etype, value, tb, tb_offset=None): + tmpstderr = io.stderr + io.stderr = sys.stderr + if not config.suppress_exception_warning: + errstr = ("WARNING: Unhandled Exception: (%s, %s)\n" + % (etype, value)) + io.stderr.write(errstr) + io.stderr = tmpstderr + + IP.set_custom_exc((Exception,), custom_handler) + # io.stdout redirect must be done *after* instantiating InteractiveShell io.stdout = self.cout io.stderr = self.cout @@ -299,11 +312,17 @@ def process_input(self, data, input_prompt, lineno): is_verbatim = decorator=='@verbatim' or self.is_verbatim is_doctest = decorator=='@doctest' or self.is_doctest is_suppress = decorator=='@suppress' or self.is_suppress + is_okexcept = decorator=='@okexcept' or self.is_okexcept is_savefig = decorator is not None and \ decorator.startswith('@savefig') input_lines = input.split('\n') + if is_okexcept: + self.config.suppress_exception_warning = True + else: + self.config.suppress_exception_warning = False + continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2)) Nc = len(continuation) @@ -597,6 +616,7 @@ class IpythonDirective(Directive): 'suppress' : directives.flag, 'verbatim' : directives.flag, 'doctest' : directives.flag, + 'okexcept' : directives.flag, } shell = EmbeddedSphinxShell() @@ -662,6 +682,7 @@ def run(self): self.shell.is_suppress = 'suppress' in options self.shell.is_doctest = 'doctest' in options self.shell.is_verbatim = 'verbatim' in options + self.shell.is_okexcept = 'okexcept' in options # handle pure python code From 55a1ab95720c3794bc97089d37e7f38efa182841 Mon Sep 17 00:00:00 2001 From: Adam Klein Date: Mon, 16 Jan 2012 11:50:41 -0500 Subject: [PATCH 2/5] ENH: ipython_directive to warning on exceptions when building docs, small fix --- doc/source/io.rst | 1 - doc/sphinxext/ipython_directive.py | 5 +---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/doc/source/io.rst b/doc/source/io.rst index 6c7a60d2fb223..c83ef06bfca2f 100644 --- a/doc/source/io.rst +++ b/doc/source/io.rst @@ -343,7 +343,6 @@ performance HDF5 format using the excellent `PyTables .. ipython:: python :suppress: - :okexcept: os.remove('store.h5') diff --git a/doc/sphinxext/ipython_directive.py b/doc/sphinxext/ipython_directive.py index c04c3475da459..ff7f77ab0a50c 100644 --- a/doc/sphinxext/ipython_directive.py +++ b/doc/sphinxext/ipython_directive.py @@ -218,13 +218,10 @@ def __init__(self): self.config = config def custom_handler(self, etype, value, tb, tb_offset=None): - tmpstderr = io.stderr - io.stderr = sys.stderr if not config.suppress_exception_warning: errstr = ("WARNING: Unhandled Exception: (%s, %s)\n" % (etype, value)) - io.stderr.write(errstr) - io.stderr = tmpstderr + sys.stderr.write(errstr) IP.set_custom_exc((Exception,), custom_handler) From f2fe8ee4c2e6b3be6fda35a0d6c88def836a3b66 Mon Sep 17 00:00:00 2001 From: Adam Klein Date: Mon, 16 Jan 2012 12:39:49 -0500 Subject: [PATCH 3/5] ENH: ipython_directive to warning on exceptions when building docs, enhancement --- doc/source/io.rst | 2 ++ doc/sphinxext/ipython_directive.py | 23 ++++++++++++----------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/doc/source/io.rst b/doc/source/io.rst index c83ef06bfca2f..81683349e0034 100644 --- a/doc/source/io.rst +++ b/doc/source/io.rst @@ -343,11 +343,13 @@ performance HDF5 format using the excellent `PyTables .. ipython:: python :suppress: + :okexcept: os.remove('store.h5') .. ipython:: python + fo store = HDFStore('store.h5') print store diff --git a/doc/sphinxext/ipython_directive.py b/doc/sphinxext/ipython_directive.py index ff7f77ab0a50c..ee2cea4b08a7e 100644 --- a/doc/sphinxext/ipython_directive.py +++ b/doc/sphinxext/ipython_directive.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +# -*- c oding: utf-8 -*- """Sphinx directive to support embedded IPython code. This directive allows pasting of entire interactive IPython sessions, prompts @@ -90,7 +90,7 @@ # Globals #----------------------------------------------------------------------------- # for tokenizing blocks -COMMENT, INPUT, OUTPUT = range(3) +COMMENT, INPUT, OUTPUT = range(3) #----------------------------------------------------------------------------- # Functions and class declarations @@ -215,12 +215,12 @@ def __init__(self): # Create and initialize ipython, but don't start its mainloop IP = InteractiveShell.instance(config=config, profile_dir=profile) - self.config = config + self_closure = self def custom_handler(self, etype, value, tb, tb_offset=None): - if not config.suppress_exception_warning: - errstr = ("WARNING: Unhandled Exception: (%s, %s)\n" - % (etype, value)) + if not self_closure.suppress_exception_warning: + errstr = ("WARNING: Exception in statement '%s' => %s, %s)\n" + % (self_closure.datacontent[1], etype, value)) sys.stderr.write(errstr) IP.set_custom_exc((Exception,), custom_handler) @@ -316,12 +316,13 @@ def process_input(self, data, input_prompt, lineno): input_lines = input.split('\n') if is_okexcept: - self.config.suppress_exception_warning = True + self.suppress_exception_warning = True else: - self.config.suppress_exception_warning = False + self.suppress_exception_warning = False + + self.datacontent = data continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2)) - Nc = len(continuation) if is_savefig: image_file, image_directive = self.process_image(decorator) @@ -643,7 +644,7 @@ def get_config_options(self): def setup(self): # get config values (savefig_dir, source_dir, rgxin, - rgxout, promptin, promptout) = self.get_config_options() + rgxout, promptin, promptout) = self.get_config_options() # and attach to shell so we don't have to pass them around self.shell.rgxin = rgxin @@ -680,7 +681,7 @@ def run(self): self.shell.is_doctest = 'doctest' in options self.shell.is_verbatim = 'verbatim' in options self.shell.is_okexcept = 'okexcept' in options - + self.shell.current_content = self.content # handle pure python code if 'python' in self.arguments: From aae802bceda9d12e0711a248103bfce74405cf7a Mon Sep 17 00:00:00 2001 From: Adam Klein Date: Mon, 16 Jan 2012 12:44:04 -0500 Subject: [PATCH 4/5] ENH: ipython_directive to warning on exceptions when building docs, bug fis --- doc/sphinxext/ipython_directive.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/sphinxext/ipython_directive.py b/doc/sphinxext/ipython_directive.py index ee2cea4b08a7e..c92fa02494bbe 100644 --- a/doc/sphinxext/ipython_directive.py +++ b/doc/sphinxext/ipython_directive.py @@ -1,4 +1,4 @@ -# -*- c oding: utf-8 -*- +# -*- coding: utf-8 -*- """Sphinx directive to support embedded IPython code. This directive allows pasting of entire interactive IPython sessions, prompts @@ -219,7 +219,7 @@ def __init__(self): def custom_handler(self, etype, value, tb, tb_offset=None): if not self_closure.suppress_exception_warning: - errstr = ("WARNING: Exception in statement '%s' => %s, %s)\n" + errstr = ("WARNING: Exception in statement '%s', %s, %s)\n" % (self_closure.datacontent[1], etype, value)) sys.stderr.write(errstr) @@ -644,7 +644,7 @@ def get_config_options(self): def setup(self): # get config values (savefig_dir, source_dir, rgxin, - rgxout, promptin, promptout) = self.get_config_options() + rgxout, promptin, promptout) = self.get_config_options() # and attach to shell so we don't have to pass them around self.shell.rgxin = rgxin From 19c8daec4c6fe3217f9c70460dc721046ac28847 Mon Sep 17 00:00:00 2001 From: Adam Klein Date: Mon, 16 Jan 2012 14:49:02 -0500 Subject: [PATCH 5/5] ENH: docs build cleanly, per #635 --- doc/source/dsintro.rst | 3 ++- doc/source/io.rst | 1 - doc/source/merging.rst | 4 ++-- doc/source/missing_data.rst | 1 + doc/source/visualization.rst | 4 ++-- doc/sphinxext/ipython_directive.py | 18 +++--------------- pandas/core/daterange.py | 3 +++ pandas/core/frame.py | 22 +++++++++++----------- 8 files changed, 24 insertions(+), 32 deletions(-) diff --git a/doc/source/dsintro.rst b/doc/source/dsintro.rst index a87ebce8e3d7c..9cf616a4670d7 100644 --- a/doc/source/dsintro.rst +++ b/doc/source/dsintro.rst @@ -16,7 +16,8 @@ objects. To get started, import numpy and load pandas into your namespace: import numpy as np from pandas import * randn = np.random.randn - np.set_printoptions(precision=4, suppress=True, max_columns=8) + np.set_printoptions(precision=4, suppress=True) + set_printoptions(precision=4, max_columns=8) .. ipython:: python diff --git a/doc/source/io.rst b/doc/source/io.rst index 81683349e0034..6c7a60d2fb223 100644 --- a/doc/source/io.rst +++ b/doc/source/io.rst @@ -349,7 +349,6 @@ performance HDF5 format using the excellent `PyTables .. ipython:: python - fo store = HDFStore('store.h5') print store diff --git a/doc/source/merging.rst b/doc/source/merging.rst index 9b3c145d4f96a..874bdcf58ab33 100644 --- a/doc/source/merging.rst +++ b/doc/source/merging.rst @@ -152,8 +152,8 @@ along ``axis=0``, namely the index: .. ipython:: python s = Series(randn(10), index=np.arange(10)) - s1 = s[:5] - s2 = s[-5:] + s1 = s[:5] # note we're slicing with labels here, so 5 is included + s2 = s[6:] s1.append(s2) In the case of DataFrame, the indexes must be disjoint but the columns do not diff --git a/doc/source/missing_data.rst b/doc/source/missing_data.rst index 06c9eead9be16..493492121bab2 100644 --- a/doc/source/missing_data.rst +++ b/doc/source/missing_data.rst @@ -256,6 +256,7 @@ an ndarray (e.g. selecting values based on some criteria). If a boolean vector contains NAs, an exception will be generated: .. ipython:: python + :okexcept: reindexed = s.reindex(range(8)).fillna(0) reindexed[crit] diff --git a/doc/source/visualization.rst b/doc/source/visualization.rst index b4f1657fa6758..371057d82fdf1 100644 --- a/doc/source/visualization.rst +++ b/doc/source/visualization.rst @@ -170,7 +170,7 @@ groupings. For instance, df = DataFrame(np.random.rand(10,2), columns=['Col1', 'Col2'] ) df['X'] = Series(['A','A','A','A','A','B','B','B','B','B']) - plot.figure(); + plt.figure(); @savefig box_plot_ex2.png width=4.5in df.boxplot(by='X') @@ -184,7 +184,7 @@ columns: df['X'] = Series(['A','A','A','A','A','B','B','B','B','B']) df['Y'] = Series(['A','B','A','B','A','B','A','B','A','B']) - plot.figure(); + plt.figure(); @savefig box_plot_ex3.png width=4.5in df.boxplot(column=['Col1','Col2'], by=['X','Y']) diff --git a/doc/sphinxext/ipython_directive.py b/doc/sphinxext/ipython_directive.py index c92fa02494bbe..bbc8578c2b9b1 100644 --- a/doc/sphinxext/ipython_directive.py +++ b/doc/sphinxext/ipython_directive.py @@ -215,16 +215,6 @@ def __init__(self): # Create and initialize ipython, but don't start its mainloop IP = InteractiveShell.instance(config=config, profile_dir=profile) - self_closure = self - - def custom_handler(self, etype, value, tb, tb_offset=None): - if not self_closure.suppress_exception_warning: - errstr = ("WARNING: Exception in statement '%s', %s, %s)\n" - % (self_closure.datacontent[1], etype, value)) - sys.stderr.write(errstr) - - IP.set_custom_exc((Exception,), custom_handler) - # io.stdout redirect must be done *after* instantiating InteractiveShell io.stdout = self.cout io.stderr = self.cout @@ -315,11 +305,6 @@ def process_input(self, data, input_prompt, lineno): input_lines = input.split('\n') - if is_okexcept: - self.suppress_exception_warning = True - else: - self.suppress_exception_warning = False - self.datacontent = data continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2)) @@ -369,6 +354,9 @@ def process_input(self, data, input_prompt, lineno): if not is_suppress and not is_semicolon: ret.append(output) + if not is_okexcept and "Traceback" in output: + sys.stdout.write(output) + self.cout.truncate(0) return (ret, input_lines, output, is_doctest, image_file, image_directive) diff --git a/pandas/core/daterange.py b/pandas/core/daterange.py index bb2270697e839..408ec43c102fb 100644 --- a/pandas/core/daterange.py +++ b/pandas/core/daterange.py @@ -221,6 +221,9 @@ def __getitem__(self, key): new_index.name = self.name return new_index else: + if result.ndim > 1: + return result + return Index(result, name=self.name) def summary(self): diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 046bcac674a28..0d9b0911871ea 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3546,17 +3546,17 @@ def plot(self, subplots=False, sharex=True, sharey=False, use_index=True, empty = self[col].count() == 0 y = self[col].values if not empty else np.zeros(x.shape) - try: - if subplots: - ax = axes[i] - ax.plot(x, y, 'k', label=str(col), **kwds) - ax.legend(loc='best') - else: - ax.plot(x, y, label=str(col), **kwds) - except Exception, e: - msg = ('Unable to plot data %s vs index %s,\n' - 'error was: %s' % (str(y), str(x), str(e))) - raise Exception(msg) + #try: + if subplots: + ax = axes[i] + ax.plot(x, y, 'k', label=str(col), **kwds) + ax.legend(loc='best') + else: + ax.plot(x, y, label=str(col), **kwds) + #except Exception, e: + # msg = ('Unable to plot data %s vs index %s,\n' + # 'error was: %s' % (str(y), str(x), str(e))) + # raise Exception(msg) ax.grid(grid)