From 75cb343f9e1f3b7c25ef4377da1622e8c931551a Mon Sep 17 00:00:00 2001 From: andre Date: Sat, 10 Mar 2018 16:00:10 -0300 Subject: [PATCH 1/7] DOC: DataFrame.pivot --- pandas/core/frame.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index a66d00fff9714..1a960493cdd0a 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4341,27 +4341,31 @@ def last_valid_index(self): def pivot(self, index=None, columns=None, values=None): """ + Return reshaped DataFrame summarized by given index / column values. + Reshape data (produce a "pivot" table) based on column values. Uses unique values from index / columns to form axes of the resulting - DataFrame. + DataFrame. This function don't support data aggregation, multiple + values will result in hierarchically indexed columns. Parameters ---------- index : string or object, optional Column name to use to make new frame's index. If None, uses existing index. - columns : string or object - Column name to use to make new frame's columns + columns : string or object, optional + Column name to use to make new frame's columns. values : string or object, optional Column name to use for populating new frame's values. If not specified, all remaining columns will be used and the result will - have hierarchically indexed columns + have hierarchically indexed columns. Returns ------- - pivoted : DataFrame + DataFrame + Returns reshaped DataFrame. - See also + See Also -------- DataFrame.pivot_table : generalization of pivot that can handle duplicate values for one index/column pair @@ -4377,8 +4381,8 @@ def pivot(self, index=None, columns=None, values=None): -------- >>> df = pd.DataFrame({'foo': ['one','one','one','two','two','two'], - 'bar': ['A', 'B', 'C', 'A', 'B', 'C'], - 'baz': [1, 2, 3, 4, 5, 6]}) + ... 'bar': ['A', 'B', 'C', 'A', 'B', 'C'], + ... 'baz': [1, 2, 3, 4, 5, 6]}) >>> df foo bar baz 0 one A 1 @@ -4389,16 +4393,16 @@ def pivot(self, index=None, columns=None, values=None): 5 two C 6 >>> df.pivot(index='foo', columns='bar', values='baz') - A B C + bar A B C + foo one 1 2 3 two 4 5 6 >>> df.pivot(index='foo', columns='bar')['baz'] - A B C + bar A B C + foo one 1 2 3 two 4 5 6 - - """ from pandas.core.reshape.reshape import pivot return pivot(self, index=index, columns=columns, values=values) From 0d1efb8e6f93a7b071ba1fdd16ed54b7197bb4d4 Mon Sep 17 00:00:00 2001 From: andre Date: Sat, 10 Mar 2018 16:35:24 -0300 Subject: [PATCH 2/7] DOC: grammar --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 1a960493cdd0a..207f441c460bc 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4345,7 +4345,7 @@ def pivot(self, index=None, columns=None, values=None): Reshape data (produce a "pivot" table) based on column values. Uses unique values from index / columns to form axes of the resulting - DataFrame. This function don't support data aggregation, multiple + DataFrame. This function does not support data aggregation, multiple values will result in hierarchically indexed columns. Parameters From e880c47e86c12b44cf8e3d27d0f1403743108733 Mon Sep 17 00:00:00 2001 From: amuta Date: Sat, 10 Mar 2018 19:40:29 -0300 Subject: [PATCH 3/7] Fixing pivot --- pandas/core/frame.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 207f441c460bc..2ea673c2fede2 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4341,22 +4341,22 @@ def last_valid_index(self): def pivot(self, index=None, columns=None, values=None): """ - Return reshaped DataFrame summarized by given index / column values. + Return reshaped DataFrame organized by given index / column values. Reshape data (produce a "pivot" table) based on column values. Uses - unique values from index / columns to form axes of the resulting + unique values from specified `index` / `columns` to form axes of the resulting DataFrame. This function does not support data aggregation, multiple values will result in hierarchically indexed columns. Parameters ---------- index : string or object, optional - Column name to use to make new frame's index. If None, uses + Column to use to make new frame's index. If None, uses existing index. - columns : string or object, optional - Column name to use to make new frame's columns. + columns : string or object + Column to use to make new frame's columns. values : string or object, optional - Column name to use for populating new frame's values. If not + Column to use for populating new frame's values. If not specified, all remaining columns will be used and the result will have hierarchically indexed columns. @@ -4368,14 +4368,14 @@ def pivot(self, index=None, columns=None, values=None): See Also -------- DataFrame.pivot_table : generalization of pivot that can handle - duplicate values for one index/column pair + duplicate values for one index/column pair. DataFrame.unstack : pivot based on the index values instead of a - column + column. Notes ----- For finer-tuned control, see hierarchical indexing documentation along - with the related stack/unstack methods + with the related stack/unstack methods. Examples -------- From 3dbafa12c5122a3c7802938d54042f19ac8c222b Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Mon, 12 Mar 2018 15:42:23 -0500 Subject: [PATCH 4/7] Added Raises and example --- pandas/core/frame.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2ea673c2fede2..d19f4be154eab 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4346,7 +4346,7 @@ def pivot(self, index=None, columns=None, values=None): Reshape data (produce a "pivot" table) based on column values. Uses unique values from specified `index` / `columns` to form axes of the resulting DataFrame. This function does not support data aggregation, multiple - values will result in hierarchically indexed columns. + values will result in a MultiIndex in the columns. Parameters ---------- @@ -4365,6 +4365,12 @@ def pivot(self, index=None, columns=None, values=None): DataFrame Returns reshaped DataFrame. + Raises + ------ + ValueError: + When there are any `index`, `columns` combinations with multiple + values. + See Also -------- DataFrame.pivot_table : generalization of pivot that can handle @@ -4379,8 +4385,8 @@ def pivot(self, index=None, columns=None, values=None): Examples -------- - - >>> df = pd.DataFrame({'foo': ['one','one','one','two','two','two'], + >>> df = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two', + ... 'two'], ... 'bar': ['A', 'B', 'C', 'A', 'B', 'C'], ... 'baz': [1, 2, 3, 4, 5, 6]}) >>> df @@ -4403,6 +4409,26 @@ def pivot(self, index=None, columns=None, values=None): foo one 1 2 3 two 4 5 6 + + A ValueError is raised if there are any duplicates. + + >>> af = pd.DataFrame({"foo": ['one', 'one', 'two', 'two'], + ... "bar": ['A', 'A', 'B', 'C'], + ... "baz": [1, 2, 3, 4]}) + >>> af + foo bar baz + 0 one A 1 + 1 one A 2 + 2 two B 3 + 3 two C 4 + + Notice that the first two rows are the same for our `index` + and `columns` arguments. + + >>> af.pivot(index='foo', columns='bar', values='baz') + Traceback (most recent call last): + ... + ValueError: Index contains duplicate entries, cannot reshape """ from pandas.core.reshape.reshape import pivot return pivot(self, index=index, columns=columns, values=values) From b13cbd085a5323c8536b56a55daec8ce9251096c Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Mon, 12 Mar 2018 15:43:49 -0500 Subject: [PATCH 5/7] Say what to do --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index d19f4be154eab..9572aed93370a 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4369,7 +4369,7 @@ def pivot(self, index=None, columns=None, values=None): ------ ValueError: When there are any `index`, `columns` combinations with multiple - values. + values. `DataFrame.pivot_table` when you need to aggregate. See Also -------- From fb80f042ac7aeefca97f430d264ca8576a780517 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Mon, 12 Mar 2018 15:45:26 -0500 Subject: [PATCH 6/7] Refer to user guide --- pandas/core/frame.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 9572aed93370a..17647d97eecb7 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4346,7 +4346,8 @@ def pivot(self, index=None, columns=None, values=None): Reshape data (produce a "pivot" table) based on column values. Uses unique values from specified `index` / `columns` to form axes of the resulting DataFrame. This function does not support data aggregation, multiple - values will result in a MultiIndex in the columns. + values will result in a MultiIndex in the columns. See the + :ref:`User Guide ` for more on reshaping. Parameters ---------- From d454a30b6db888111d0ce4bd2bdb2c49abd1bf2d Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Mon, 12 Mar 2018 15:47:20 -0500 Subject: [PATCH 7/7] Fixed name --- pandas/core/frame.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 17647d97eecb7..bc224eba02b9e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4413,10 +4413,10 @@ def pivot(self, index=None, columns=None, values=None): A ValueError is raised if there are any duplicates. - >>> af = pd.DataFrame({"foo": ['one', 'one', 'two', 'two'], + >>> df = pd.DataFrame({"foo": ['one', 'one', 'two', 'two'], ... "bar": ['A', 'A', 'B', 'C'], ... "baz": [1, 2, 3, 4]}) - >>> af + >>> df foo bar baz 0 one A 1 1 one A 2 @@ -4426,7 +4426,7 @@ def pivot(self, index=None, columns=None, values=None): Notice that the first two rows are the same for our `index` and `columns` arguments. - >>> af.pivot(index='foo', columns='bar', values='baz') + >>> df.pivot(index='foo', columns='bar', values='baz') Traceback (most recent call last): ... ValueError: Index contains duplicate entries, cannot reshape