From 0bc8bc8648bb2c8c439211126820167a6405e257 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Sat, 29 Aug 2015 01:17:01 +0100 Subject: [PATCH 1/2] Revert "Revert "Merge pull request #10727 from jorisvandenbossche/sphinx-traceback" --- doc/source/advanced.rst | 34 ++++++++----------- doc/source/basics.rst | 10 +++--- doc/source/dsintro.rst | 6 ++-- doc/source/indexing.rst | 12 +++---- doc/source/options.rst | 6 +--- doc/source/timeseries.rst | 8 ++--- .../ipython_sphinxext/ipython_directive.py | 14 +++++--- 7 files changed, 41 insertions(+), 49 deletions(-) diff --git a/doc/source/advanced.rst b/doc/source/advanced.rst index 850f59c2713eb..1888345e1055c 100644 --- a/doc/source/advanced.rst +++ b/doc/source/advanced.rst @@ -661,18 +661,14 @@ values NOT in the categories, similarly to how you can reindex ANY pandas index. Reshaping and Comparision operations on a ``CategoricalIndex`` must have the same categories or a ``TypeError`` will be raised. - .. code-block:: python - - In [9]: df3 = pd.DataFrame({'A' : np.arange(6), - 'B' : pd.Series(list('aabbca')).astype('category')}) - - In [11]: df3 = df3.set_index('B') - - In [11]: df3.index - Out[11]: CategoricalIndex([u'a', u'a', u'b', u'b', u'c', u'a'], categories=[u'a', u'b', u'c'], ordered=False, name=u'B', dtype='category') + .. ipython:: python + :okexcept: - In [12]: pd.concat([df2, df3] - TypeError: categories must match existing categories when appending + df3 = pd.DataFrame({'A' : np.arange(6), + 'B' : pd.Series(list('aabbca')).astype('category')}) + df3 = df3.set_index('B') + df3.index + pd.concat([df2, df3] .. _indexing.float64index: @@ -738,20 +734,18 @@ In float indexes, slicing using floats is allowed In non-float indexes, slicing using floats will raise a ``TypeError`` -.. code-block:: python - - In [1]: pd.Series(range(5))[3.5] - TypeError: the label [3.5] is not a proper indexer for this index type (Int64Index) +.. ipython:: python + :okexcept: - In [1]: pd.Series(range(5))[3.5:4.5] - TypeError: the slice start [3.5] is not a proper indexer for this index type (Int64Index) + pd.Series(range(5))[3.5] + pd.Series(range(5))[3.5:4.5] Using a scalar float indexer will be deprecated in a future version, but is allowed for now. -.. code-block:: python +.. ipython:: python + :okwarning: - In [3]: pd.Series(range(5))[3.0] - Out[3]: 3 + pd.Series(range(5))[3.0] Here is a typical use-case for using this type of indexing. Imagine that you have a somewhat irregular timedelta-like indexing scheme, but the data is recorded as floats. This could for diff --git a/doc/source/basics.rst b/doc/source/basics.rst index 956c90ae63034..6bfbfb87f2c55 100644 --- a/doc/source/basics.rst +++ b/doc/source/basics.rst @@ -352,13 +352,11 @@ objects of the same length: Trying to compare ``Index`` or ``Series`` objects of different lengths will raise a ValueError: -.. code-block:: python - - In [55]: pd.Series(['foo', 'bar', 'baz']) == pd.Series(['foo', 'bar']) - ValueError: Series lengths must match to compare +.. ipython:: python + :okexcept: - In [56]: pd.Series(['foo', 'bar', 'baz']) == pd.Series(['foo']) - ValueError: Series lengths must match to compare + pd.Series(['foo', 'bar', 'baz']) == pd.Series(['foo', 'bar']) + pd.Series(['foo', 'bar', 'baz']) == pd.Series(['foo']) Note that this is different from the numpy behavior where a comparison can be broadcast: diff --git a/doc/source/dsintro.rst b/doc/source/dsintro.rst index 5a62e7dccea34..847044c4745f9 100644 --- a/doc/source/dsintro.rst +++ b/doc/source/dsintro.rst @@ -143,10 +143,10 @@ label: If a label is not contained, an exception is raised: -.. code-block:: python +.. ipython:: python + :okexcept: - >>> s['f'] - KeyError: 'f' + s['f'] Using the ``get`` method, a missing label will return None or specified default: diff --git a/doc/source/indexing.rst b/doc/source/indexing.rst index 38629ee7baaea..a49a4745f7200 100644 --- a/doc/source/indexing.rst +++ b/doc/source/indexing.rst @@ -293,10 +293,10 @@ Selection By Label dfl = pd.DataFrame(np.random.randn(5,4), columns=list('ABCD'), index=pd.date_range('20130101',periods=5)) dfl - .. code-block:: python + .. ipython:: python + :okexcept: - In [4]: dfl.loc[2:3] - TypeError: cannot do slice indexing on with these indexers [2] of + dfl.loc[2:3] String likes in slicing *can* be convertible to the type of the index and lead to natural slicing. @@ -475,13 +475,11 @@ A single indexer that is out of bounds will raise an ``IndexError``. A list of indexers where any element is out of bounds will raise an ``IndexError`` -.. code-block:: python +.. ipython:: python + :okexcept: dfl.iloc[[4,5,6]] - IndexError: positional indexers are out-of-bounds - dfl.iloc[:,4] - IndexError: single positional indexer is out-of-bounds .. _indexing.basics.partial_setting: diff --git a/doc/source/options.rst b/doc/source/options.rst index 26871a11473de..834b4b642c393 100644 --- a/doc/source/options.rst +++ b/doc/source/options.rst @@ -57,11 +57,7 @@ The following will **not work** because it matches multiple option names, e.g. .. ipython:: python :okexcept: - try: - pd.get_option("column") - except KeyError as e: - print(e) - + pd.get_option("column") **Note:** Using this form of shorthand may cause your code to break if new options with similar names are added in future versions. diff --git a/doc/source/timeseries.rst b/doc/source/timeseries.rst index 6f30ff3f51ad5..a2067b9a37d55 100644 --- a/doc/source/timeseries.rst +++ b/doc/source/timeseries.rst @@ -205,9 +205,9 @@ Invalid Data Pass ``errors='coerce'`` to convert invalid data to ``NaT`` (not a time): .. ipython:: python - :okexcept: # this is the default, raise when unparseable + @okexcept to_datetime(['2009/07/31', 'asd'], errors='raise') # return the original input when unparseable @@ -656,7 +656,7 @@ apply the offset to each element. rng + DateOffset(months=2) s + DateOffset(months=2) s - DateOffset(months=2) - + If the offset class maps directly to a ``Timedelta`` (``Day``, ``Hour``, ``Minute``, ``Second``, ``Micro``, ``Milli``, ``Nano``) it can be used exactly like a ``Timedelta`` - see the @@ -670,7 +670,7 @@ used exactly like a ``Timedelta`` - see the td + Minute(15) Note that some offsets (such as ``BQuarterEnd``) do not have a -vectorized implementation. They can still be used but may +vectorized implementation. They can still be used but may calculate signficantly slower and will raise a ``PerformanceWarning`` .. ipython:: python @@ -1702,13 +1702,13 @@ the top example will fail as it contains ambiguous times and the bottom will infer the right offset. .. ipython:: python - :okexcept: rng_hourly = DatetimeIndex(['11/06/2011 00:00', '11/06/2011 01:00', '11/06/2011 01:00', '11/06/2011 02:00', '11/06/2011 03:00']) # This will fail as there are ambiguous times + @okexcept rng_hourly.tz_localize('US/Eastern') rng_hourly_eastern = rng_hourly.tz_localize('US/Eastern', ambiguous='infer') rng_hourly_eastern.tolist() diff --git a/doc/sphinxext/ipython_sphinxext/ipython_directive.py b/doc/sphinxext/ipython_sphinxext/ipython_directive.py index ad7ada8e4eea3..04a9e804f9af2 100644 --- a/doc/sphinxext/ipython_sphinxext/ipython_directive.py +++ b/doc/sphinxext/ipython_sphinxext/ipython_directive.py @@ -465,10 +465,6 @@ def process_input(self, data, input_prompt, lineno): self.cout.seek(0) output = self.cout.read() - if not is_suppress and not is_semicolon: - ret.append(output) - elif is_semicolon: # get spacing right - ret.append('') # context information filename = self.state.document.current_source @@ -498,6 +494,16 @@ def process_input(self, data, input_prompt, lineno): sys.stdout.write(s) sys.stdout.write('<<<' + ('-' * 73) + '\n') + # if :okexcept: has been specified, display shorter traceback + if is_okexcept and "Traceback" in output: + traceback = output.split('\n\n') + output = traceback[-1] + + if not is_suppress and not is_semicolon: + ret.append(output) + elif is_semicolon: # get spacing right + ret.append('') + self.cout.truncate(0) return (ret, input_lines, output, is_doctest, decorator, image_file, image_directive) From d6fd1b0e0e717324da6614530d8ab1903dfd7496 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Sat, 29 Aug 2015 01:23:35 +0100 Subject: [PATCH 2/2] Change eval example to static ipython block --- doc/source/enhancingperf.rst | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/doc/source/enhancingperf.rst b/doc/source/enhancingperf.rst index 855a459f48cf4..c53252bd7193f 100644 --- a/doc/source/enhancingperf.rst +++ b/doc/source/enhancingperf.rst @@ -633,11 +633,14 @@ With :func:`pandas.eval` you cannot use the ``@`` prefix *at all*, because it isn't defined in that context. ``pandas`` will let you know this if you try to use ``@`` in a top-level call to :func:`pandas.eval`. For example, -.. ipython:: python - :okexcept: +.. ipython:: + + In [41]: a, b = 1, 2 + + In [42]: pd.eval('@a + b') + SyntaxError: The '@' prefix is not allowed in top-level eval calls, + please refer to your variables by name without the '@' prefix - a, b = 1, 2 - pd.eval('@a + b') In this case, you should simply refer to the variables like you would in standard Python.