From 17bf87e3d1154695812d52556f842e5bb0b801f6 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Tue, 11 Jun 2019 11:51:48 +0200 Subject: [PATCH 1/6] fix whatsnew code examples --- doc/source/whatsnew/v0.11.0.rst | 57 ++++++++++++++++++++++++--------- doc/source/whatsnew/v0.13.0.rst | 44 ++++++++++++++++++------- doc/source/whatsnew/v0.25.0.rst | 18 +++++------ 3 files changed, 83 insertions(+), 36 deletions(-) diff --git a/doc/source/whatsnew/v0.11.0.rst b/doc/source/whatsnew/v0.11.0.rst index c919698d15689..98c7c615b55f6 100644 --- a/doc/source/whatsnew/v0.11.0.rst +++ b/doc/source/whatsnew/v0.11.0.rst @@ -105,27 +105,54 @@ Conversion Mixed Conversion -.. ipython:: python - :okwarning: +.. code-block:: ipython - df3['D'] = '1.' - df3['E'] = '1' - df3.convert_objects(convert_numeric=True).dtypes + In [12]: df3['D'] = '1.' - # same, but specific dtype conversion - df3['D'] = df3['D'].astype('float16') - df3['E'] = df3['E'].astype('int32') - df3.dtypes + In [13]: df3['E'] = '1' + + In [14]: df3.convert_objects(convert_numeric=True).dtypes + Out[14]: + A float32 + B float64 + C float64 + D float64 + E int64 + dtype: object + + # same, but specific dtype conversion + In [15]: df3['D'] = df3['D'].astype('float16') + + In [16]: df3['E'] = df3['E'].astype('int32') + + In [17]: df3.dtypes + Out[17]: + A float32 + B float64 + C float64 + D float16 + E int32 + dtype: object Forcing Date coercion (and setting ``NaT`` when not datelike) -.. ipython:: python - :okwarning: +.. code-block:: ipython + + In [18]: import datetime + + In [19]: s = pd.Series([datetime.datetime(2001, 1, 1, 0, 0), 'foo', 1.0, 1, + ....: pd.Timestamp('20010104'), '20010105'], dtype='O') + ....: - import datetime - s = pd.Series([datetime.datetime(2001, 1, 1, 0, 0), 'foo', 1.0, 1, - pd.Timestamp('20010104'), '20010105'], dtype='O') - s.convert_objects(convert_dates='coerce') + In [20]: s.convert_objects(convert_dates='coerce') + Out[20]: + 0 2001-01-01 + 1 NaT + 2 NaT + 3 NaT + 4 2001-01-04 + 5 2001-01-05 + dtype: datetime64[ns] Dtype Gotchas ~~~~~~~~~~~~~ diff --git a/doc/source/whatsnew/v0.13.0.rst b/doc/source/whatsnew/v0.13.0.rst index 13a2f879211b3..095d1807ca873 100644 --- a/doc/source/whatsnew/v0.13.0.rst +++ b/doc/source/whatsnew/v0.13.0.rst @@ -271,17 +271,39 @@ This is like an ``append`` operation. A Panel setting operation on an arbitrary axis aligns the input to the Panel -.. ipython:: python - :okwarning: - - p = pd.Panel(np.arange(16).reshape(2, 4, 2), - items=['Item1', 'Item2'], - major_axis=pd.date_range('2001/1/12', periods=4), - minor_axis=['A', 'B'], dtype='float64') - p - p.loc[:, :, 'C'] = pd.Series([30, 32], index=p.items) - p - p.loc[:, :, 'C'] +.. code-block:: ipython + + In [20]: p = pd.Panel(np.arange(16).reshape(2, 4, 2), + ....: items=['Item1', 'Item2'], + ....: major_axis=pd.date_range('2001/1/12', periods=4), + ....: minor_axis=['A', 'B'], dtype='float64') + ....: + + In [21]: p + Out[21]: + + Dimensions: 2 (items) x 4 (major_axis) x 2 (minor_axis) + Items axis: Item1 to Item2 + Major_axis axis: 2001-01-12 00:00:00 to 2001-01-15 00:00:00 + Minor_axis axis: A to B + + In [22]: p.loc[:, :, 'C'] = pd.Series([30, 32], index=p.items) + + In [23]: p + Out[23]: + + Dimensions: 2 (items) x 4 (major_axis) x 3 (minor_axis) + Items axis: Item1 to Item2 + Major_axis axis: 2001-01-12 00:00:00 to 2001-01-15 00:00:00 + Minor_axis axis: A to C + + In [24]: p.loc[:, :, 'C'] + Out[24]: + Item1 Item2 + 2001-01-12 30.0 32.0 + 2001-01-13 30.0 32.0 + 2001-01-14 30.0 32.0 + 2001-01-15 30.0 32.0 Float64Index API Change ~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index f61c8bfbd782e..f4041bf058fba 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -145,20 +145,17 @@ Constructing a :class:`MultiIndex` with ``NaN`` levels or codes value < -1 was a Now, construction with codes value < -1 is not allowed and ``NaN`` levels' corresponding codes would be reassigned as -1. (:issue:`19387`) -.. ipython:: python - - mi1 = pd.MultiIndex(levels=[[np.nan, None, pd.NaT, 128, 2]], - codes=[[0, -1, 1, 2, 3, 4]]) - mi2 = pd.MultiIndex(levels=[[1, 2]], codes=[[0, -2]]) - *Previous Behavior*: .. code-block:: ipython - In [1]: mi1 + In [1]: pd.MultiIndex(levels=[[np.nan, None, pd.NaT, 128, 2]], + ...: codes=[[0, -1, 1, 2, 3, 4]]) + ...: Out[1]: MultiIndex(levels=[[nan, None, NaT, 128, 2]], codes=[[0, -1, 1, 2, 3, 4]]) - In [2]: mi2 + + In [2]: pd.MultiIndex(levels=[[1, 2]], codes=[[0, -2]]) Out[2]: MultiIndex(levels=[[1, 2]], codes=[[0, -2]]) @@ -166,8 +163,9 @@ would be reassigned as -1. (:issue:`19387`) .. ipython:: python - mi1 - mi2 + mi1 = pd.MultiIndex(levels=[[np.nan, None, pd.NaT, 128, 2]], + codes=[[0, -1, 1, 2, 3, 4]]) + mi2 = pd.MultiIndex(levels=[[1, 2]], codes=[[0, -2]]) .. _whatsnew_0250.api_breaking.groupby_apply_first_group_once: From a9880308fd8d3b57b5adfa9f36ed94ea2bba6a25 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Tue, 11 Jun 2019 13:36:09 +0200 Subject: [PATCH 2/6] fix docstring warnings --- pandas/core/frame.py | 3 +++ pandas/core/generic.py | 1 + 2 files changed, 4 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index bb3275c27a4ac..ef406ce3d6b02 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4000,6 +4000,7 @@ def rename(self, *args, **kwargs): intent. Rename columns using a mapping: + >>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) >>> df.rename(columns={"A": "a", "B": "c"}) a c @@ -4008,6 +4009,7 @@ def rename(self, *args, **kwargs): 2 3 6 Rename index using a mapping: + >>> df.rename(index={0: "x", 1: "y", 2: "z"}) A B x 1 4 @@ -4015,6 +4017,7 @@ def rename(self, *args, **kwargs): z 3 6 Cast index labels to a different type: + >>> df.index RangeIndex(start=0, stop=3, step=1) >>> df.rename(index=str).index diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 81fe87f190822..8ec8227d8a1d3 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -10867,6 +10867,7 @@ def _doc_parms(cls): level_output_1=0) _stat_func_see_also = """ + See Also -------- Series.sum : Return the sum. From fa83b3e34500a469944eb6fa31bb9199a3f9fab7 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Tue, 11 Jun 2019 15:45:07 +0200 Subject: [PATCH 3/6] fix example --- doc/source/whatsnew/v0.25.0.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index f4041bf058fba..eb029679cffe4 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -163,9 +163,9 @@ would be reassigned as -1. (:issue:`19387`) .. ipython:: python - mi1 = pd.MultiIndex(levels=[[np.nan, None, pd.NaT, 128, 2]], - codes=[[0, -1, 1, 2, 3, 4]]) - mi2 = pd.MultiIndex(levels=[[1, 2]], codes=[[0, -2]]) + pd.MultiIndex(levels=[[np.nan, None, pd.NaT, 128, 2]], + codes=[[0, -1, 1, 2, 3, 4]]) + pd.MultiIndex(levels=[[1, 2]], codes=[[0, -2]]) .. _whatsnew_0250.api_breaking.groupby_apply_first_group_once: From f53706185d30f66037ffe52d290cea1c4adf267a Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Tue, 11 Jun 2019 15:47:02 +0200 Subject: [PATCH 4/6] fix spacing --- doc/source/whatsnew/v0.11.0.rst | 4 ++-- doc/source/whatsnew/v0.25.0.rst | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.11.0.rst b/doc/source/whatsnew/v0.11.0.rst index 98c7c615b55f6..ba19d3c856683 100644 --- a/doc/source/whatsnew/v0.11.0.rst +++ b/doc/source/whatsnew/v0.11.0.rst @@ -141,8 +141,8 @@ Forcing Date coercion (and setting ``NaT`` when not datelike) In [18]: import datetime In [19]: s = pd.Series([datetime.datetime(2001, 1, 1, 0, 0), 'foo', 1.0, 1, - ....: pd.Timestamp('20010104'), '20010105'], dtype='O') - ....: + ....: pd.Timestamp('20010104'), '20010105'], dtype='O') + ....: In [20]: s.convert_objects(convert_dates='coerce') Out[20]: diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index eb029679cffe4..24c16c8a214ed 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -151,7 +151,7 @@ would be reassigned as -1. (:issue:`19387`) In [1]: pd.MultiIndex(levels=[[np.nan, None, pd.NaT, 128, 2]], ...: codes=[[0, -1, 1, 2, 3, 4]]) - ...: + ...: Out[1]: MultiIndex(levels=[[nan, None, NaT, 128, 2]], codes=[[0, -1, 1, 2, 3, 4]]) From 0db3d3f67e292c8cd0f11a35057a5fc05138600f Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Tue, 11 Jun 2019 17:11:56 +0200 Subject: [PATCH 5/6] more code-blocks --- doc/source/whatsnew/v0.11.0.rst | 116 ++++++++++++++++++++++++++------ 1 file changed, 97 insertions(+), 19 deletions(-) diff --git a/doc/source/whatsnew/v0.11.0.rst b/doc/source/whatsnew/v0.11.0.rst index ba19d3c856683..0dfcfca9a7464 100644 --- a/doc/source/whatsnew/v0.11.0.rst +++ b/doc/source/whatsnew/v0.11.0.rst @@ -165,11 +165,22 @@ dtypes, they *WILL* be respected, however (:issue:`2837`) The following will all result in ``int64`` dtypes -.. ipython:: python +.. code-block:: ipython + + In [21]: pd.DataFrame([1, 2], columns=['a']).dtypes + Out[21]: + a int64 + dtype: object - pd.DataFrame([1, 2], columns=['a']).dtypes - pd.DataFrame({'a': [1, 2]}).dtypes - pd.DataFrame({'a': 1}, index=range(2)).dtypes + In [22]: pd.DataFrame({'a': [1, 2]}).dtypes + Out[22]: + a int64 + dtype: object + + In [23]: pd.DataFrame({'a': 1}, index=range(2)).dtypes + Out[23]: + a int64 + dtype: object Keep in mind that ``DataFrame(np.array([1,2]))`` **WILL** result in ``int32`` on 32-bit platforms! @@ -179,28 +190,95 @@ Keep in mind that ``DataFrame(np.array([1,2]))`` **WILL** result in ``int32`` on Performing indexing operations on integer type data can easily upcast the data. The dtype of the input data will be preserved in cases where ``nans`` are not introduced. -.. ipython:: python +.. code-block:: ipython - dfi = df3.astype('int32') - dfi['D'] = dfi['D'].astype('int64') - dfi - dfi.dtypes + In [24]: dfi = df3.astype('int32') + + In [25]: dfi['D'] = dfi['D'].astype('int64') + + In [26]: dfi + Out[26]: + A B C D E + 0 0 0 0 1 1 + 1 -2 0 1 1 1 + 2 -2 0 2 1 1 + 3 0 -1 3 1 1 + 4 1 0 4 1 1 + 5 0 0 5 1 1 + 6 0 -1 6 1 1 + 7 0 0 7 1 1 + + In [27]: dfi.dtypes + Out[27]: + A int32 + B int32 + C int32 + D int64 + E int32 + dtype: object - casted = dfi[dfi > 0] - casted - casted.dtypes + In [28]: casted = dfi[dfi > 0] + + In [29]: casted + Out[29]: + A B C D E + 0 NaN NaN NaN 1 1 + 1 NaN NaN 1.0 1 1 + 2 NaN NaN 2.0 1 1 + 3 NaN NaN 3.0 1 1 + 4 1.0 NaN 4.0 1 1 + 5 NaN NaN 5.0 1 1 + 6 NaN NaN 6.0 1 1 + 7 NaN NaN 7.0 1 1 + + In [30]: casted.dtypes + Out[30]: + A float64 + B float64 + C float64 + D int64 + E int32 + dtype: object While float dtypes are unchanged. -.. ipython:: python +.. code-block:: ipython + + In [31]: df4 = df3.copy() - df4 = df3.copy() - df4['A'] = df4['A'].astype('float32') - df4.dtypes + In [32]: df4['A'] = df4['A'].astype('float32') - casted = df4[df4 > 0] - casted - casted.dtypes + In [33]: df4.dtypes + Out[33]: + A float32 + B float64 + C float64 + D float16 + E int32 + dtype: object + + In [34]: casted = df4[df4 > 0] + + In [35]: casted + Out[35]: + A B C D E + 0 NaN NaN NaN 1.0 1 + 1 NaN 0.567020 1.0 1.0 1 + 2 NaN 0.276232 2.0 1.0 1 + 3 NaN NaN 3.0 1.0 1 + 4 1.933792 NaN 4.0 1.0 1 + 5 NaN 0.113648 5.0 1.0 1 + 6 NaN NaN 6.0 1.0 1 + 7 NaN 0.524988 7.0 1.0 1 + + In [36]: casted.dtypes + Out[36]: + A float32 + B float64 + C float64 + D float16 + E int32 + dtype: object Datetimes Conversion ~~~~~~~~~~~~~~~~~~~~ From a9fe0790fa7a8e0a9b87b9d1e2e1d967cbb1893a Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Tue, 11 Jun 2019 17:13:34 +0200 Subject: [PATCH 6/6] allow exception --- doc/source/whatsnew/v0.25.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 24c16c8a214ed..2e2d2d99126d9 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -162,6 +162,7 @@ would be reassigned as -1. (:issue:`19387`) *New Behavior*: .. ipython:: python + :okexcept: pd.MultiIndex(levels=[[np.nan, None, pd.NaT, 128, 2]], codes=[[0, -1, 1, 2, 3, 4]])