Skip to content

Fix PR01 errors for melt, option_context, read_fwf, reset_option #57806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -490,11 +490,7 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.errors.AbstractMethodError\
pandas.errors.UndefinedVariableError\
pandas.get_option\
pandas.io.formats.style.Styler.to_excel\
pandas.melt\
pandas.option_context\
pandas.read_fwf\
pandas.reset_option # There should be no backslash in the final line, please keep this comment in the last ignored function
pandas.io.formats.style.Styler.to_excel # There should be no backslash in the final line, please keep this comment in the last ignored function
RET=$(($RET + $?)) ; echo $MSG "DONE"

MSG='Partially validate docstrings (PR07)' ; echo $MSG
Expand Down
122 changes: 121 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -9682,7 +9682,6 @@ def unstack(

return result.__finalize__(self, method="unstack")

@Appender(_shared_docs["melt"] % {"caller": "df.melt(", "other": "melt"})
def melt(
self,
id_vars=None,
Expand All @@ -9692,6 +9691,127 @@ def melt(
col_level: Level | None = None,
ignore_index: bool = True,
) -> DataFrame:
"""
Unpivot DataFrame from wide to long format, optionally leaving identifiers set.

This function is useful to massage a DataFrame into a format where one
or more columns are identifier variables (`id_vars`), while all other
columns, considered measured variables (`value_vars`), are "unpivoted" to
the row axis, leaving just two non-identifier columns, 'variable' and
'value'.

Parameters
----------
id_vars : scalar, tuple, list, or ndarray, optional
Column(s) to use as identifier variables.
value_vars : scalar, tuple, list, or ndarray, optional
Column(s) to unpivot. If not specified, uses all columns that
are not set as `id_vars`.
var_name : scalar, default None
Name to use for the 'variable' column. If None it uses
``frame.columns.name`` or 'variable'.
value_name : scalar, default 'value'
Name to use for the 'value' column, can't be an existing column label.
col_level : scalar, optional
If columns are a MultiIndex then use this level to melt.
ignore_index : bool, default True
If True, original index is ignored. If False, original index is retained.
Index labels will be repeated as necessary.

Returns
-------
DataFrame
Unpivoted DataFrame.

See Also
--------
melt : Identical method.
pivot_table : Create a spreadsheet-style pivot table as a DataFrame.
DataFrame.pivot : Return reshaped DataFrame organized
by given index / column values.
DataFrame.explode : Explode a DataFrame from list-like
columns to long format.

Notes
-----
Reference :ref:`the user guide <reshaping.melt>` for more examples.

Examples
--------
>>> df = pd.DataFrame(
... {
... "A": {0: "a", 1: "b", 2: "c"},
... "B": {0: 1, 1: 3, 2: 5},
... "C": {0: 2, 1: 4, 2: 6},
... }
... )
>>> df
A B C
0 a 1 2
1 b 3 4
2 c 5 6

>>> df.melt(id_vars=["A"], value_vars=["B"])
A variable value
0 a B 1
1 b B 3
2 c B 5

>>> df.melt(id_vars=["A"], value_vars=["B", "C"])
A variable value
0 a B 1
1 b B 3
2 c B 5
3 a C 2
4 b C 4
5 c C 6

The names of 'variable' and 'value' columns can be customized:

>>> df.melt(
... id_vars=["A"],
... value_vars=["B"],
... var_name="myVarname",
... value_name="myValname",
... )
A myVarname myValname
0 a B 1
1 b B 3
2 c B 5

Original index values can be kept around:

>>> df.melt(id_vars=["A"], value_vars=["B", "C"], ignore_index=False)
A variable value
0 a B 1
1 b B 3
2 c B 5
0 a C 2
1 b C 4
2 c C 6

If you have multi-index columns:

>>> df.columns = [list("ABC"), list("DEF")]
>>> df
A B C
D E F
0 a 1 2
1 b 3 4
2 c 5 6

>>> df.melt(col_level=0, id_vars=["A"], value_vars=["B"])
A variable value
0 a B 1
1 b B 3
2 c B 5

>>> df.melt(id_vars=[("A", "D")], value_vars=[("B", "E")])
(A, D) variable_0 variable_1 value
0 a B E 1
1 b B E 3
2 c B E 5
"""
return melt(
self,
id_vars=id_vars,
Expand Down
128 changes: 124 additions & 4 deletions pandas/core/reshape/melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import numpy as np

from pandas.util._decorators import Appender

from pandas.core.dtypes.common import is_list_like
from pandas.core.dtypes.concat import concat_compat
from pandas.core.dtypes.missing import notna
Expand All @@ -15,7 +13,6 @@
from pandas.core.indexes.api import MultiIndex
from pandas.core.reshape.concat import concat
from pandas.core.reshape.util import tile_compat
from pandas.core.shared_docs import _shared_docs
from pandas.core.tools.numeric import to_numeric

if TYPE_CHECKING:
Expand All @@ -40,7 +37,6 @@ def ensure_list_vars(arg_vars, variable: str, columns) -> list:
return []


@Appender(_shared_docs["melt"] % {"caller": "pd.melt(df, ", "other": "DataFrame.melt"})
def melt(
frame: DataFrame,
id_vars=None,
Expand All @@ -50,6 +46,130 @@ def melt(
col_level=None,
ignore_index: bool = True,
) -> DataFrame:
"""
Unpivot a DataFrame from wide to long format, optionally leaving identifiers set.

This function is useful to massage a DataFrame into a format where one
or more columns are identifier variables (`id_vars`), while all other
columns, considered measured variables (`value_vars`), are "unpivoted" to
the row axis, leaving just two non-identifier columns, 'variable' and
'value'.

Parameters
----------
frame : DataFrame
The DataFrame to unpivot.
id_vars : scalar, tuple, list, or ndarray, optional
Column(s) to use as identifier variables.
value_vars : scalar, tuple, list, or ndarray, optional
Column(s) to unpivot. If not specified, uses all columns that
are not set as `id_vars`.
var_name : scalar, default None
Name to use for the 'variable' column. If None it uses
``frame.columns.name`` or 'variable'.
value_name : scalar, default 'value'
Name to use for the 'value' column, can't be an existing column label.
col_level : scalar, optional
If columns are a MultiIndex then use this level to melt.
ignore_index : bool, default True
If True, original index is ignored. If False, the original index is retained.
Index labels will be repeated as necessary.

Returns
-------
DataFrame
Unpivoted DataFrame.

See Also
--------
DataFrame.melt : Identical method.
pivot_table : Create a spreadsheet-style pivot table as a DataFrame.
DataFrame.pivot : Return reshaped DataFrame organized
by given index / column values.
DataFrame.explode : Explode a DataFrame from list-like
columns to long format.

Notes
-----
Reference :ref:`the user guide <reshaping.melt>` for more examples.

Examples
--------
>>> df = pd.DataFrame(
... {
... "A": {0: "a", 1: "b", 2: "c"},
... "B": {0: 1, 1: 3, 2: 5},
... "C": {0: 2, 1: 4, 2: 6},
... }
... )
>>> df
A B C
0 a 1 2
1 b 3 4
2 c 5 6

>>> pd.melt(df, id_vars=["A"], value_vars=["B"])
A variable value
0 a B 1
1 b B 3
2 c B 5

>>> pd.melt(df, id_vars=["A"], value_vars=["B", "C"])
A variable value
0 a B 1
1 b B 3
2 c B 5
3 a C 2
4 b C 4
5 c C 6

The names of 'variable' and 'value' columns can be customized:

>>> pd.melt(
... df,
... id_vars=["A"],
... value_vars=["B"],
... var_name="myVarname",
... value_name="myValname",
... )
A myVarname myValname
0 a B 1
1 b B 3
2 c B 5

Original index values can be kept around:

>>> pd.melt(df, id_vars=["A"], value_vars=["B", "C"], ignore_index=False)
A variable value
0 a B 1
1 b B 3
2 c B 5
0 a C 2
1 b C 4
2 c C 6

If you have multi-index columns:

>>> df.columns = [list("ABC"), list("DEF")]
>>> df
A B C
D E F
0 a 1 2
1 b 3 4
2 c 5 6

>>> pd.melt(df, col_level=0, id_vars=["A"], value_vars=["B"])
A variable value
0 a B 1
1 b B 3
2 c B 5

>>> pd.melt(df, id_vars=[("A", "D")], value_vars=[("B", "E")])
(A, D) variable_0 variable_1 value
0 a B E 1
1 b B E 3
2 c B E 5
"""
if value_name in frame.columns:
raise ValueError(
f"value_name ({value_name}) cannot match an element in "
Expand Down
Loading