From 3f5d728f871e6709b9ce3520b38c0746b03ed62f Mon Sep 17 00:00:00 2001 From: Michael Charlton Date: Fri, 9 Dec 2016 11:30:51 +0000 Subject: [PATCH 1/5] astype method now takes dict mapping col names to datatypes #14761 Updating documentation to reflect change --- doc/source/basics.rst | 4 +++- doc/source/whatsnew/v0.20.0.txt | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/source/basics.rst b/doc/source/basics.rst index e5aa6b577270a..ada7877c4505c 100644 --- a/doc/source/basics.rst +++ b/doc/source/basics.rst @@ -1755,12 +1755,14 @@ then the more *general* one will be used as the result of the operation. # conversion of dtypes df3.astype('float32').dtypes +.. versionadded:: 0.20.0 + Convert a subset of columns to a specified type using :meth:`~DataFrame.astype` .. ipython:: python dft = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'c': [7, 8, 9]}) - dft[['a','b']] = dft[['a','b']].astype(np.uint8) + dft = dft.astype({'a': np.float64, 'c': np.uint8}) dft dft.dtypes diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 0bfd755aae40c..35543b3f025d3 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -43,6 +43,16 @@ fixed-width text files, and :func:`read_excel` for parsing Excel files. pd.read_fwf(StringIO(data)).dtypes pd.read_fwf(StringIO(data), dtype={'a':'float64', 'b':'object'}).dtypes +You can now pass a dictionary mapping column names to desired data types for that +column to :meth:`~DataFrame.astype`. + +.. ipython:: python + + dft = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'c': [7, 8, 9]}) + dft = dft.astype({'a': np.float64, 'c': np.uint8}) + dft + dft.dtypes + .. _whatsnew_0200.enhancements.other: Other enhancements From fddbb2ed0537cd32c3e37f61983972e1957399d4 Mon Sep 17 00:00:00 2001 From: Michael Charlton Date: Thu, 15 Dec 2016 15:39:18 +0000 Subject: [PATCH 2/5] Updates after review DataFrame.astype now allows changing the dtype of a column by passing a dict mapping column name to dtype. --- doc/source/basics.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/doc/source/basics.rst b/doc/source/basics.rst index e7db814483905..58da24d889507 100644 --- a/doc/source/basics.rst +++ b/doc/source/basics.rst @@ -1779,6 +1779,18 @@ Convert a subset of columns to a specified type using :meth:`~DataFrame.astype` dft.loc[:, ['a', 'b']] = dft.loc[:, ['a', 'b']].astype(np.uint8) dft.dtypes + +.. versionadded:: 0.19 + +Convert certain columns to a specific dtype by passing a dict to :meth:`~DataFrame.astype` + +.. ipython:: python + + dft1 = pd.DataFrame({'a': [1,0,1], 'b': [4,5,6], 'c': [7, 8, 9]}) + dft1 = dft1.astype({'a': np.bool, 'c': np.float64}) + dft1 + dft1.dtypes + .. _basics.object_conversion: object conversion From a61ec51dfd32a6d434318a78da36822f531518e6 Mon Sep 17 00:00:00 2001 From: Michael Charlton Date: Thu, 15 Dec 2016 16:31:13 +0000 Subject: [PATCH 3/5] Corrections to docs after review feedback DataFrame.astype now allows setting the type of columns by passing a dict mapping column to dtype. --- doc/source/basics.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/doc/source/basics.rst b/doc/source/basics.rst index e7db814483905..58da24d889507 100644 --- a/doc/source/basics.rst +++ b/doc/source/basics.rst @@ -1779,6 +1779,18 @@ Convert a subset of columns to a specified type using :meth:`~DataFrame.astype` dft.loc[:, ['a', 'b']] = dft.loc[:, ['a', 'b']].astype(np.uint8) dft.dtypes + +.. versionadded:: 0.19 + +Convert certain columns to a specific dtype by passing a dict to :meth:`~DataFrame.astype` + +.. ipython:: python + + dft1 = pd.DataFrame({'a': [1,0,1], 'b': [4,5,6], 'c': [7, 8, 9]}) + dft1 = dft1.astype({'a': np.bool, 'c': np.float64}) + dft1 + dft1.dtypes + .. _basics.object_conversion: object conversion From 0c9078562c771e139d2e8ea3291dd52f41a68719 Mon Sep 17 00:00:00 2001 From: Michael Charlton Date: Thu, 15 Dec 2016 17:22:08 +0000 Subject: [PATCH 4/5] Removing uneeded changes Mistakenly added changes carried out in v0.19 to v0.20 --- doc/source/whatsnew/v0.20.0.txt | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 07e8154f557b3..2855cde95ac2a 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -43,15 +43,27 @@ fixed-width text files, and :func:`read_excel` for parsing Excel files. pd.read_fwf(StringIO(data)).dtypes pd.read_fwf(StringIO(data), dtype={'a':'float64', 'b':'object'}).dtypes -You can now pass a dictionary mapping column names to desired data types for that -column to :meth:`~DataFrame.astype`. +.. _whatsnew_0200.enhancements.groupby_access: + +Groupby Enhancements +^^^^^^^^^^^^^^^^^^^^ + +Strings passed to ``DataFrame.groupby()`` as the ``by`` parameter may now reference either column names or index level names (:issue:`5677`) .. ipython:: python - dft = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'c': [7, 8, 9]}) - dft = dft.astype({'a': np.float64, 'c': np.uint8}) - dft - dft.dtypes + arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], + ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']] + + index = pd.MultiIndex.from_arrays(arrays, names=['first', 'second']) + + df = pd.DataFrame({'A': [1, 1, 1, 1, 2, 2, 3, 3], + 'B': np.arange(8)}, + index=index) + df + + df.groupby(['second', 'A']).sum() + .. _whatsnew_0200.enhancements.other: From b9414a92e27ba9fe70ffba5e4167f0bdb6441791 Mon Sep 17 00:00:00 2001 From: Michael Charlton Date: Mon, 19 Dec 2016 21:55:54 +0000 Subject: [PATCH 5/5] Correcting after review Ability to pass dict mapping column to dtype introduced in v0.19 not v0.20 --- doc/source/basics.rst | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/doc/source/basics.rst b/doc/source/basics.rst index 69326f0530797..2e8abe0a5c329 100644 --- a/doc/source/basics.rst +++ b/doc/source/basics.rst @@ -1757,17 +1757,27 @@ then the more *general* one will be used as the result of the operation. # conversion of dtypes df3.astype('float32').dtypes -.. versionadded:: 0.20.0 Convert a subset of columns to a specified type using :meth:`~DataFrame.astype` .. ipython:: python dft = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'c': [7, 8, 9]}) - dft = dft.astype({'a': np.float64, 'c': np.uint8}) + dft[['a','b']] = dft[['a','b']].astype(np.uint8) dft dft.dtypes +.. versionadded:: 0.19.0 + +Convert certain columns to a specific dtype by passing a dict to :meth:`~DataFrame.astype` + +.. ipython:: python + + dft1 = pd.DataFrame({'a': [1,0,1], 'b': [4,5,6], 'c': [7, 8, 9]}) + dft1 = dft1.astype({'a': np.bool, 'c': np.float64}) + dft1 + dft1.dtypes + .. note:: When trying to convert a subset of columns to a specified type using :meth:`~DataFrame.astype` and :meth:`~DataFrame.loc`, upcasting occurs. @@ -1781,18 +1791,6 @@ Convert a subset of columns to a specified type using :meth:`~DataFrame.astype` dft.loc[:, ['a', 'b']] = dft.loc[:, ['a', 'b']].astype(np.uint8) dft.dtypes - -.. versionadded:: 0.19 - -Convert certain columns to a specific dtype by passing a dict to :meth:`~DataFrame.astype` - -.. ipython:: python - - dft1 = pd.DataFrame({'a': [1,0,1], 'b': [4,5,6], 'c': [7, 8, 9]}) - dft1 = dft1.astype({'a': np.bool, 'c': np.float64}) - dft1 - dft1.dtypes - .. _basics.object_conversion: object conversion