Skip to content

Commit 9d1d6f6

Browse files
Fix PR01 errors for melt, option_context, read_fwf, reset_option (#57806)
* Fix PR01 errors for melt, option_context, read_fwf, reset_option * removed shared docstring and fixed PR02 error in pandas.DataFrame.melt
1 parent ba64039 commit 9d1d6f6

File tree

5 files changed

+251
-124
lines changed

5 files changed

+251
-124
lines changed

ci/code_checks.sh

+1-5
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,7 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
490490
pandas.errors.AbstractMethodError\
491491
pandas.errors.UndefinedVariableError\
492492
pandas.get_option\
493-
pandas.io.formats.style.Styler.to_excel\
494-
pandas.melt\
495-
pandas.option_context\
496-
pandas.read_fwf\
497-
pandas.reset_option # There should be no backslash in the final line, please keep this comment in the last ignored function
493+
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
498494
RET=$(($RET + $?)) ; echo $MSG "DONE"
499495

500496
MSG='Partially validate docstrings (PR07)' ; echo $MSG

pandas/core/frame.py

+121-1
Original file line numberDiff line numberDiff line change
@@ -9682,7 +9682,6 @@ def unstack(
96829682

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

9685-
@Appender(_shared_docs["melt"] % {"caller": "df.melt(", "other": "melt"})
96869685
def melt(
96879686
self,
96889687
id_vars=None,
@@ -9692,6 +9691,127 @@ def melt(
96929691
col_level: Level | None = None,
96939692
ignore_index: bool = True,
96949693
) -> DataFrame:
9694+
"""
9695+
Unpivot DataFrame from wide to long format, optionally leaving identifiers set.
9696+
9697+
This function is useful to massage a DataFrame into a format where one
9698+
or more columns are identifier variables (`id_vars`), while all other
9699+
columns, considered measured variables (`value_vars`), are "unpivoted" to
9700+
the row axis, leaving just two non-identifier columns, 'variable' and
9701+
'value'.
9702+
9703+
Parameters
9704+
----------
9705+
id_vars : scalar, tuple, list, or ndarray, optional
9706+
Column(s) to use as identifier variables.
9707+
value_vars : scalar, tuple, list, or ndarray, optional
9708+
Column(s) to unpivot. If not specified, uses all columns that
9709+
are not set as `id_vars`.
9710+
var_name : scalar, default None
9711+
Name to use for the 'variable' column. If None it uses
9712+
``frame.columns.name`` or 'variable'.
9713+
value_name : scalar, default 'value'
9714+
Name to use for the 'value' column, can't be an existing column label.
9715+
col_level : scalar, optional
9716+
If columns are a MultiIndex then use this level to melt.
9717+
ignore_index : bool, default True
9718+
If True, original index is ignored. If False, original index is retained.
9719+
Index labels will be repeated as necessary.
9720+
9721+
Returns
9722+
-------
9723+
DataFrame
9724+
Unpivoted DataFrame.
9725+
9726+
See Also
9727+
--------
9728+
melt : Identical method.
9729+
pivot_table : Create a spreadsheet-style pivot table as a DataFrame.
9730+
DataFrame.pivot : Return reshaped DataFrame organized
9731+
by given index / column values.
9732+
DataFrame.explode : Explode a DataFrame from list-like
9733+
columns to long format.
9734+
9735+
Notes
9736+
-----
9737+
Reference :ref:`the user guide <reshaping.melt>` for more examples.
9738+
9739+
Examples
9740+
--------
9741+
>>> df = pd.DataFrame(
9742+
... {
9743+
... "A": {0: "a", 1: "b", 2: "c"},
9744+
... "B": {0: 1, 1: 3, 2: 5},
9745+
... "C": {0: 2, 1: 4, 2: 6},
9746+
... }
9747+
... )
9748+
>>> df
9749+
A B C
9750+
0 a 1 2
9751+
1 b 3 4
9752+
2 c 5 6
9753+
9754+
>>> df.melt(id_vars=["A"], value_vars=["B"])
9755+
A variable value
9756+
0 a B 1
9757+
1 b B 3
9758+
2 c B 5
9759+
9760+
>>> df.melt(id_vars=["A"], value_vars=["B", "C"])
9761+
A variable value
9762+
0 a B 1
9763+
1 b B 3
9764+
2 c B 5
9765+
3 a C 2
9766+
4 b C 4
9767+
5 c C 6
9768+
9769+
The names of 'variable' and 'value' columns can be customized:
9770+
9771+
>>> df.melt(
9772+
... id_vars=["A"],
9773+
... value_vars=["B"],
9774+
... var_name="myVarname",
9775+
... value_name="myValname",
9776+
... )
9777+
A myVarname myValname
9778+
0 a B 1
9779+
1 b B 3
9780+
2 c B 5
9781+
9782+
Original index values can be kept around:
9783+
9784+
>>> df.melt(id_vars=["A"], value_vars=["B", "C"], ignore_index=False)
9785+
A variable value
9786+
0 a B 1
9787+
1 b B 3
9788+
2 c B 5
9789+
0 a C 2
9790+
1 b C 4
9791+
2 c C 6
9792+
9793+
If you have multi-index columns:
9794+
9795+
>>> df.columns = [list("ABC"), list("DEF")]
9796+
>>> df
9797+
A B C
9798+
D E F
9799+
0 a 1 2
9800+
1 b 3 4
9801+
2 c 5 6
9802+
9803+
>>> df.melt(col_level=0, id_vars=["A"], value_vars=["B"])
9804+
A variable value
9805+
0 a B 1
9806+
1 b B 3
9807+
2 c B 5
9808+
9809+
>>> df.melt(id_vars=[("A", "D")], value_vars=[("B", "E")])
9810+
(A, D) variable_0 variable_1 value
9811+
0 a B E 1
9812+
1 b B E 3
9813+
2 c B E 5
9814+
"""
96959815
return melt(
96969816
self,
96979817
id_vars=id_vars,

pandas/core/reshape/melt.py

+124-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
import numpy as np
77

8-
from pandas.util._decorators import Appender
9-
108
from pandas.core.dtypes.common import is_list_like
119
from pandas.core.dtypes.concat import concat_compat
1210
from pandas.core.dtypes.missing import notna
@@ -15,7 +13,6 @@
1513
from pandas.core.indexes.api import MultiIndex
1614
from pandas.core.reshape.concat import concat
1715
from pandas.core.reshape.util import tile_compat
18-
from pandas.core.shared_docs import _shared_docs
1916
from pandas.core.tools.numeric import to_numeric
2017

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

4239

43-
@Appender(_shared_docs["melt"] % {"caller": "pd.melt(df, ", "other": "DataFrame.melt"})
4440
def melt(
4541
frame: DataFrame,
4642
id_vars=None,
@@ -50,6 +46,130 @@ def melt(
5046
col_level=None,
5147
ignore_index: bool = True,
5248
) -> DataFrame:
49+
"""
50+
Unpivot a DataFrame from wide to long format, optionally leaving identifiers set.
51+
52+
This function is useful to massage a DataFrame into a format where one
53+
or more columns are identifier variables (`id_vars`), while all other
54+
columns, considered measured variables (`value_vars`), are "unpivoted" to
55+
the row axis, leaving just two non-identifier columns, 'variable' and
56+
'value'.
57+
58+
Parameters
59+
----------
60+
frame : DataFrame
61+
The DataFrame to unpivot.
62+
id_vars : scalar, tuple, list, or ndarray, optional
63+
Column(s) to use as identifier variables.
64+
value_vars : scalar, tuple, list, or ndarray, optional
65+
Column(s) to unpivot. If not specified, uses all columns that
66+
are not set as `id_vars`.
67+
var_name : scalar, default None
68+
Name to use for the 'variable' column. If None it uses
69+
``frame.columns.name`` or 'variable'.
70+
value_name : scalar, default 'value'
71+
Name to use for the 'value' column, can't be an existing column label.
72+
col_level : scalar, optional
73+
If columns are a MultiIndex then use this level to melt.
74+
ignore_index : bool, default True
75+
If True, original index is ignored. If False, the original index is retained.
76+
Index labels will be repeated as necessary.
77+
78+
Returns
79+
-------
80+
DataFrame
81+
Unpivoted DataFrame.
82+
83+
See Also
84+
--------
85+
DataFrame.melt : Identical method.
86+
pivot_table : Create a spreadsheet-style pivot table as a DataFrame.
87+
DataFrame.pivot : Return reshaped DataFrame organized
88+
by given index / column values.
89+
DataFrame.explode : Explode a DataFrame from list-like
90+
columns to long format.
91+
92+
Notes
93+
-----
94+
Reference :ref:`the user guide <reshaping.melt>` for more examples.
95+
96+
Examples
97+
--------
98+
>>> df = pd.DataFrame(
99+
... {
100+
... "A": {0: "a", 1: "b", 2: "c"},
101+
... "B": {0: 1, 1: 3, 2: 5},
102+
... "C": {0: 2, 1: 4, 2: 6},
103+
... }
104+
... )
105+
>>> df
106+
A B C
107+
0 a 1 2
108+
1 b 3 4
109+
2 c 5 6
110+
111+
>>> pd.melt(df, id_vars=["A"], value_vars=["B"])
112+
A variable value
113+
0 a B 1
114+
1 b B 3
115+
2 c B 5
116+
117+
>>> pd.melt(df, id_vars=["A"], value_vars=["B", "C"])
118+
A variable value
119+
0 a B 1
120+
1 b B 3
121+
2 c B 5
122+
3 a C 2
123+
4 b C 4
124+
5 c C 6
125+
126+
The names of 'variable' and 'value' columns can be customized:
127+
128+
>>> pd.melt(
129+
... df,
130+
... id_vars=["A"],
131+
... value_vars=["B"],
132+
... var_name="myVarname",
133+
... value_name="myValname",
134+
... )
135+
A myVarname myValname
136+
0 a B 1
137+
1 b B 3
138+
2 c B 5
139+
140+
Original index values can be kept around:
141+
142+
>>> pd.melt(df, id_vars=["A"], value_vars=["B", "C"], ignore_index=False)
143+
A variable value
144+
0 a B 1
145+
1 b B 3
146+
2 c B 5
147+
0 a C 2
148+
1 b C 4
149+
2 c C 6
150+
151+
If you have multi-index columns:
152+
153+
>>> df.columns = [list("ABC"), list("DEF")]
154+
>>> df
155+
A B C
156+
D E F
157+
0 a 1 2
158+
1 b 3 4
159+
2 c 5 6
160+
161+
>>> pd.melt(df, col_level=0, id_vars=["A"], value_vars=["B"])
162+
A variable value
163+
0 a B 1
164+
1 b B 3
165+
2 c B 5
166+
167+
>>> pd.melt(df, id_vars=[("A", "D")], value_vars=[("B", "E")])
168+
(A, D) variable_0 variable_1 value
169+
0 a B E 1
170+
1 b B E 3
171+
2 c B E 5
172+
"""
53173
if value_name in frame.columns:
54174
raise ValueError(
55175
f"value_name ({value_name}) cannot match an element in "

0 commit comments

Comments
 (0)