From b9be7760257d1ca7a0ec6d86d4f5529a1b211380 Mon Sep 17 00:00:00 2001 From: Anthony Panza <19810086+tpanza@users.noreply.github.com> Date: Wed, 8 Apr 2020 20:09:40 -0700 Subject: [PATCH 1/9] references to lreshape and wide_to_long --- doc/source/reference/frame.rst | 2 ++ doc/source/reference/general_functions.rst | 1 + pandas/core/frame.py | 12 +++++++++ pandas/core/reshape/melt.py | 30 +++++++++++++++++++++- 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/doc/source/reference/frame.rst b/doc/source/reference/frame.rst index cf81540a77d11..ca1dfc2c3ed03 100644 --- a/doc/source/reference/frame.rst +++ b/doc/source/reference/frame.rst @@ -234,6 +234,8 @@ Reshaping, sorting, transposing DataFrame.unstack DataFrame.swapaxes DataFrame.melt + lreshape + wide_to_long DataFrame.explode DataFrame.squeeze DataFrame.to_xarray diff --git a/doc/source/reference/general_functions.rst b/doc/source/reference/general_functions.rst index b5832cb8aa591..75b14004e7f41 100644 --- a/doc/source/reference/general_functions.rst +++ b/doc/source/reference/general_functions.rst @@ -13,6 +13,7 @@ Data manipulations :toctree: api/ melt + lreshape pivot pivot_table crosstab diff --git a/pandas/core/frame.py b/pandas/core/frame.py index d2da52ba7bdd0..53a84aa4fd34b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5913,6 +5913,10 @@ def groupby( duplicate values for one index/column pair. DataFrame.unstack : Pivot based on the index values instead of a column. + lreshape : Reshape long-format data to wide. Generalized inverse of + DataFrame.pivot. + wide_to_long : Wide panel to long format. Less flexible but more + user-friendly than melt. Notes ----- @@ -6067,6 +6071,10 @@ def pivot(self, index=None, columns=None, values=None) -> "DataFrame": -------- DataFrame.pivot : Pivot without aggregation that can handle non-numeric data. + lreshape : Reshape long-format data to wide. Generalized inverse of + DataFrame.pivot. + wide_to_long : Wide panel to long format. Less flexible but more + user-friendly than melt. Examples -------- @@ -6511,7 +6519,11 @@ def unstack(self, level=-1, fill_value=None): See Also -------- %(other)s : Identical method. + wide_to_long : Wide panel to long format. Less flexible but more + user-friendly than melt. pivot_table : Create a spreadsheet-style pivot table as a DataFrame. + lreshape : Reshape long-format data to wide. Generalized inverse of + DataFrame.pivot. DataFrame.pivot : Return reshaped DataFrame organized by given index / column values. DataFrame.explode : Explode a DataFrame from list-like diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index c3e170b0e39c4..a83427f566bec 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -121,7 +121,7 @@ def melt( @deprecate_kwarg(old_arg_name="label", new_arg_name=None) def lreshape(data: DataFrame, groups, dropna: bool = True, label=None) -> DataFrame: """ - Reshape long-format data to wide. Generalized inverse of DataFrame.pivot + Reshape long-format data to wide. Generalized inverse of DataFrame.pivot. Parameters ---------- @@ -150,6 +150,20 @@ def lreshape(data: DataFrame, groups, dropna: bool = True, label=None) -> DataFr Returns ------- reshaped : DataFrame + + See Also + -------- + melt : Unpivot a DataFrame from wide to long format, optionally leaving + identifiers set. + pivot : Create a spreadsheet-style pivot table as a DataFrame. + DataFrame.pivot : Pivot without aggregation that can handle + non-numeric data. + DataFrame.pivot_table : Generalization of pivot that can handle + duplicate values for one index/column pair. + DataFrame.unstack : Pivot based on the index values instead of a + column. + wide_to_long : Wide panel to long format. Less flexible but more + user-friendly than melt. """ if isinstance(groups, dict): keys = list(groups.keys()) @@ -239,6 +253,20 @@ def wide_to_long( A DataFrame that contains each stub name as a variable, with new index (i, j). + See Also + -------- + melt : Unpivot a DataFrame from wide to long format, optionally leaving + identifiers set. + pivot : Create a spreadsheet-style pivot table as a DataFrame. + DataFrame.pivot : Pivot without aggregation that can handle + non-numeric data. + DataFrame.pivot_table : Generalization of pivot that can handle + duplicate values for one index/column pair. + DataFrame.unstack : Pivot based on the index values instead of a + column. + lreshape : Reshape long-format data to wide. Generalized inverse of + DataFrame.pivot. + Notes ----- All extra variables are left untouched. This simply uses From 4035c1851ed38a3d296a323ae2d5a809cfa0c06e Mon Sep 17 00:00:00 2001 From: Anthony Panza <19810086+tpanza@users.noreply.github.com> Date: Wed, 8 Apr 2020 22:06:45 -0700 Subject: [PATCH 2/9] fix description of lreshape. fix format of lreshape docstring. --- pandas/core/frame.py | 6 ++-- pandas/core/reshape/melt.py | 56 ++++++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 53a84aa4fd34b..7cd6f91ba5f26 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5913,7 +5913,7 @@ def groupby( duplicate values for one index/column pair. DataFrame.unstack : Pivot based on the index values instead of a column. - lreshape : Reshape long-format data to wide. Generalized inverse of + lreshape : Reshape wide-format data to format. Generalized inverse of DataFrame.pivot. wide_to_long : Wide panel to long format. Less flexible but more user-friendly than melt. @@ -6071,7 +6071,7 @@ def pivot(self, index=None, columns=None, values=None) -> "DataFrame": -------- DataFrame.pivot : Pivot without aggregation that can handle non-numeric data. - lreshape : Reshape long-format data to wide. Generalized inverse of + lreshape : Reshape wide-format data to format. Generalized inverse of DataFrame.pivot. wide_to_long : Wide panel to long format. Less flexible but more user-friendly than melt. @@ -6522,7 +6522,7 @@ def unstack(self, level=-1, fill_value=None): wide_to_long : Wide panel to long format. Less flexible but more user-friendly than melt. pivot_table : Create a spreadsheet-style pivot table as a DataFrame. - lreshape : Reshape long-format data to wide. Generalized inverse of + lreshape : Reshape wide-format data to format. Generalized inverse of DataFrame.pivot. DataFrame.pivot : Return reshaped DataFrame organized by given index / column values. diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index a83427f566bec..c628061519225 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -121,35 +121,30 @@ def melt( @deprecate_kwarg(old_arg_name="label", new_arg_name=None) def lreshape(data: DataFrame, groups, dropna: bool = True, label=None) -> DataFrame: """ - Reshape long-format data to wide. Generalized inverse of DataFrame.pivot. + Reshape wide-format data to long. Generalized inverse of DataFrame.pivot. + + Similar to ``pd.melt`` but more user-friendly. Accepts a dictionary, + ``groups``, in which each key is a new column name and each value is a + list of old column names that will be "melted" under the new column name as + part of the reshape. Parameters ---------- data : DataFrame + The wide-format DataFrame. groups : dict - {new_name : list_of_columns} - dropna : boolean, default True - - Examples - -------- - >>> data = pd.DataFrame({'hr1': [514, 573], 'hr2': [545, 526], - ... 'team': ['Red Sox', 'Yankees'], - ... 'year1': [2007, 2007], 'year2': [2008, 2008]}) - >>> data - hr1 hr2 team year1 year2 - 0 514 545 Red Sox 2007 2008 - 1 573 526 Yankees 2007 2008 - - >>> pd.lreshape(data, {'year': ['year1', 'year2'], 'hr': ['hr1', 'hr2']}) - team year hr - 0 Red Sox 2007 514 - 1 Yankees 2007 573 - 2 Red Sox 2008 545 - 3 Yankees 2008 526 + {new_name : list_of_columns}. + dropna : bool, default True + Do not include columns whose entries are all NaN. + label : None + Not used. + + .. deprecated:: 1.0.0 Returns ------- - reshaped : DataFrame + DataFrame + Reshaped DataFrame. See Also -------- @@ -164,6 +159,23 @@ def lreshape(data: DataFrame, groups, dropna: bool = True, label=None) -> DataFr column. wide_to_long : Wide panel to long format. Less flexible but more user-friendly than melt. + + Examples + -------- + >>> data = pd.DataFrame({'hr1': [514, 573], 'hr2': [545, 526], + ... 'team': ['Red Sox', 'Yankees'], + ... 'year1': [2007, 2007], 'year2': [2008, 2008]}) + >>> data + hr1 hr2 team year1 year2 + 0 514 545 Red Sox 2007 2008 + 1 573 526 Yankees 2007 2008 + + >>> pd.lreshape(data, {'year': ['year1', 'year2'], 'hr': ['hr1', 'hr2']}) + team year hr + 0 Red Sox 2007 514 + 1 Yankees 2007 573 + 2 Red Sox 2008 545 + 3 Yankees 2008 526 """ if isinstance(groups, dict): keys = list(groups.keys()) @@ -264,7 +276,7 @@ def wide_to_long( duplicate values for one index/column pair. DataFrame.unstack : Pivot based on the index values instead of a column. - lreshape : Reshape long-format data to wide. Generalized inverse of + lreshape : Reshape wide-format data to format. Generalized inverse of DataFrame.pivot. Notes From c638eeabfc2d02c5582cb513d2920d0f5d2c705d Mon Sep 17 00:00:00 2001 From: Anthony Panza <19810086+tpanza@users.noreply.github.com> Date: Wed, 8 Apr 2020 22:16:22 -0700 Subject: [PATCH 3/9] remove whitespace in blank line --- pandas/core/reshape/melt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index c628061519225..42785d5ca4582 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -138,7 +138,7 @@ def lreshape(data: DataFrame, groups, dropna: bool = True, label=None) -> DataFr Do not include columns whose entries are all NaN. label : None Not used. - + .. deprecated:: 1.0.0 Returns From 735a68e5a60d581efd659b201eff06df4404f0e5 Mon Sep 17 00:00:00 2001 From: Anthony Panza <19810086+tpanza@users.noreply.github.com> Date: Wed, 8 Apr 2020 22:35:49 -0700 Subject: [PATCH 4/9] reorder. Put lreshape near the end, next to wide_to_long --- doc/source/reference/general_functions.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/reference/general_functions.rst b/doc/source/reference/general_functions.rst index 75b14004e7f41..6771e4d8ef1b0 100644 --- a/doc/source/reference/general_functions.rst +++ b/doc/source/reference/general_functions.rst @@ -13,7 +13,6 @@ Data manipulations :toctree: api/ melt - lreshape pivot pivot_table crosstab @@ -26,6 +25,7 @@ Data manipulations get_dummies factorize unique + lreshape wide_to_long Top-level missing data From 27747c773c3015c73511df1ae738d3bf61d57ef8 Mon Sep 17 00:00:00 2001 From: Anthony Panza <19810086+tpanza@users.noreply.github.com> Date: Wed, 8 Apr 2020 22:58:03 -0700 Subject: [PATCH 5/9] Add reference to DataFrame.melt in pivot_table's See Also docstring --- pandas/core/frame.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 7cd6f91ba5f26..5522b1b56e0b2 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6071,6 +6071,8 @@ def pivot(self, index=None, columns=None, values=None) -> "DataFrame": -------- DataFrame.pivot : Pivot without aggregation that can handle non-numeric data. + DataFrame.melt: Unpivot a DataFrame from wide to long format, + optionally leaving identifiers set. lreshape : Reshape wide-format data to format. Generalized inverse of DataFrame.pivot. wide_to_long : Wide panel to long format. Less flexible but more From 86ca0e3d27e6ce47a52fccc2ad70585b707513e0 Mon Sep 17 00:00:00 2001 From: Anthony Panza <19810086+tpanza@users.noreply.github.com> Date: Wed, 8 Apr 2020 23:36:05 -0700 Subject: [PATCH 6/9] whatsnew entry for GH33417 --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 5c39377899a20..b2d9b7301a537 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -88,7 +88,7 @@ Other enhancements - :class:`Series.str` now has a `fullmatch` method that matches a regular expression against the entire string in each row of the series, similar to `re.fullmatch` (:issue:`32806`). - :meth:`DataFrame.sample` will now also allow array-like and BitGenerator objects to be passed to ``random_state`` as seeds (:issue:`32503`) - :meth:`MultiIndex.union` will now raise `RuntimeWarning` if the object inside are unsortable, pass `sort=False` to suppress this warning (:issue:`33015`) -- +- Updated :meth:`pandas.lreshape` and :meth:`pandas.wide_to_long` documentation (:issue:`33417`) .. --------------------------------------------------------------------------- From 1b4e267b2159e541fbb9136e68b4e77326487547 Mon Sep 17 00:00:00 2001 From: Anthony Panza <19810086+tpanza@users.noreply.github.com> Date: Sat, 11 Apr 2020 19:24:03 -0700 Subject: [PATCH 7/9] remove references to lreshape --- doc/source/reference/frame.rst | 1 - doc/source/reference/general_functions.rst | 1 - doc/source/whatsnew/v1.1.0.rst | 2 +- pandas/core/frame.py | 6 ------ pandas/core/reshape/melt.py | 2 -- 5 files changed, 1 insertion(+), 11 deletions(-) diff --git a/doc/source/reference/frame.rst b/doc/source/reference/frame.rst index ca1dfc2c3ed03..37519370c7851 100644 --- a/doc/source/reference/frame.rst +++ b/doc/source/reference/frame.rst @@ -234,7 +234,6 @@ Reshaping, sorting, transposing DataFrame.unstack DataFrame.swapaxes DataFrame.melt - lreshape wide_to_long DataFrame.explode DataFrame.squeeze diff --git a/doc/source/reference/general_functions.rst b/doc/source/reference/general_functions.rst index 6771e4d8ef1b0..b5832cb8aa591 100644 --- a/doc/source/reference/general_functions.rst +++ b/doc/source/reference/general_functions.rst @@ -25,7 +25,6 @@ Data manipulations get_dummies factorize unique - lreshape wide_to_long Top-level missing data diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index b2d9b7301a537..41af6ce87a657 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -88,7 +88,7 @@ Other enhancements - :class:`Series.str` now has a `fullmatch` method that matches a regular expression against the entire string in each row of the series, similar to `re.fullmatch` (:issue:`32806`). - :meth:`DataFrame.sample` will now also allow array-like and BitGenerator objects to be passed to ``random_state`` as seeds (:issue:`32503`) - :meth:`MultiIndex.union` will now raise `RuntimeWarning` if the object inside are unsortable, pass `sort=False` to suppress this warning (:issue:`33015`) -- Updated :meth:`pandas.lreshape` and :meth:`pandas.wide_to_long` documentation (:issue:`33417`) +- Updated :meth:`pandas.wide_to_long` documentation (:issue:`33417`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 5522b1b56e0b2..2106e74dbbffe 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5913,8 +5913,6 @@ def groupby( duplicate values for one index/column pair. DataFrame.unstack : Pivot based on the index values instead of a column. - lreshape : Reshape wide-format data to format. Generalized inverse of - DataFrame.pivot. wide_to_long : Wide panel to long format. Less flexible but more user-friendly than melt. @@ -6073,8 +6071,6 @@ def pivot(self, index=None, columns=None, values=None) -> "DataFrame": non-numeric data. DataFrame.melt: Unpivot a DataFrame from wide to long format, optionally leaving identifiers set. - lreshape : Reshape wide-format data to format. Generalized inverse of - DataFrame.pivot. wide_to_long : Wide panel to long format. Less flexible but more user-friendly than melt. @@ -6524,8 +6520,6 @@ def unstack(self, level=-1, fill_value=None): wide_to_long : Wide panel to long format. Less flexible but more user-friendly than melt. pivot_table : Create a spreadsheet-style pivot table as a DataFrame. - lreshape : Reshape wide-format data to format. Generalized inverse of - DataFrame.pivot. DataFrame.pivot : Return reshaped DataFrame organized by given index / column values. DataFrame.explode : Explode a DataFrame from list-like diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index 42785d5ca4582..84eee00836696 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -276,8 +276,6 @@ def wide_to_long( duplicate values for one index/column pair. DataFrame.unstack : Pivot based on the index values instead of a column. - lreshape : Reshape wide-format data to format. Generalized inverse of - DataFrame.pivot. Notes ----- From 17133764ad4e7e52a966c4d6a95117fd06a61228 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Fri, 22 May 2020 15:03:49 +0100 Subject: [PATCH 8/9] comment --- pandas/core/reshape/melt.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index 3330a261dcab3..2b98508af0b9f 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -120,10 +120,9 @@ def lreshape(data: DataFrame, groups, dropna: bool = True, label=None) -> DataFr """ Reshape wide-format data to long. Generalized inverse of DataFrame.pivot. - Similar to ``pd.melt`` but more user-friendly. Accepts a dictionary, - ``groups``, in which each key is a new column name and each value is a - list of old column names that will be "melted" under the new column name as - part of the reshape. + Accepts a dictionary, ``groups``, in which each key is a new column name + and each value is a list of old column names that will be "melted" under + the new column name as part of the reshape. Parameters ---------- From be23734d1a6db355e3aaaa993327901a0cb4168e Mon Sep 17 00:00:00 2001 From: Anthony Panza <19810086+tpanza@users.noreply.github.com> Date: Sun, 21 Jun 2020 13:34:00 -0700 Subject: [PATCH 9/9] remove wide_to_long from list of Reshaping, sorting, transposing DataFrames --- doc/source/reference/frame.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/reference/frame.rst b/doc/source/reference/frame.rst index fc7b21ea724a3..e3dfb552651a0 100644 --- a/doc/source/reference/frame.rst +++ b/doc/source/reference/frame.rst @@ -240,7 +240,6 @@ Reshaping, sorting, transposing DataFrame.unstack DataFrame.swapaxes DataFrame.melt - wide_to_long DataFrame.explode DataFrame.squeeze DataFrame.to_xarray