Skip to content

Commit 6a2ece9

Browse files
ludusrussojorisvandenbossche
authored andcommitted
DOC: update the Series.reset_index DocString (#20107)
1 parent bf9e4f3 commit 6a2ece9

File tree

1 file changed

+92
-35
lines changed

1 file changed

+92
-35
lines changed

pandas/core/series.py

Lines changed: 92 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,55 +1071,112 @@ def _set_value(self, label, value, takeable=False):
10711071

10721072
def reset_index(self, level=None, drop=False, name=None, inplace=False):
10731073
"""
1074-
Analogous to the :meth:`pandas.DataFrame.reset_index` function, see
1075-
docstring there.
1074+
Generate a new DataFrame or Series with the index reset.
1075+
1076+
This is useful when the index needs to be treated as a column, or
1077+
when the index is meaningless and needs to be reset to the default
1078+
before another operation.
10761079
10771080
Parameters
10781081
----------
1079-
level : int, str, tuple, or list, default None
1080-
Only remove the given levels from the index. Removes all levels by
1081-
default
1082-
drop : boolean, default False
1083-
Do not try to insert index into dataframe columns
1084-
name : object, default None
1085-
The name of the column corresponding to the Series values
1086-
inplace : boolean, default False
1087-
Modify the Series in place (do not create a new object)
1082+
level : int, str, tuple, or list, default optional
1083+
For a Series with a MultiIndex, only remove the specified levels
1084+
from the index. Removes all levels by default.
1085+
drop : bool, default False
1086+
Just reset the index, without inserting it as a column in
1087+
the new DataFrame.
1088+
name : object, optional
1089+
The name to use for the column containing the original Series
1090+
values. Uses ``self.name`` by default. This argument is ignored
1091+
when `drop` is True.
1092+
inplace : bool, default False
1093+
Modify the Series in place (do not create a new object).
10881094
10891095
Returns
1090-
----------
1091-
resetted : DataFrame, or Series if drop == True
1096+
-------
1097+
Series or DataFrame
1098+
When `drop` is False (the default), a DataFrame is returned.
1099+
The newly created columns will come first in the DataFrame,
1100+
followed by the original Series values.
1101+
When `drop` is True, a `Series` is returned.
1102+
In either case, if ``inplace=True``, no value is returned.
1103+
1104+
See Also
1105+
--------
1106+
DataFrame.reset_index: Analogous function for DataFrame.
10921107
10931108
Examples
10941109
--------
1095-
>>> s = pd.Series([1, 2, 3, 4], index=pd.Index(['a', 'b', 'c', 'd'],
1096-
... name = 'idx'))
1110+
1111+
>>> s = pd.Series([1, 2, 3, 4], name='foo',
1112+
... index=pd.Index(['a', 'b', 'c', 'd'], name='idx'))
1113+
1114+
Generate a DataFrame with default index.
1115+
10971116
>>> s.reset_index()
1098-
idx 0
1099-
0 a 1
1100-
1 b 2
1101-
2 c 3
1102-
3 d 4
1103-
1104-
>>> arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo',
1105-
... 'foo', 'qux', 'qux']),
1106-
... np.array(['one', 'two', 'one', 'two', 'one', 'two',
1107-
... 'one', 'two'])]
1117+
idx foo
1118+
0 a 1
1119+
1 b 2
1120+
2 c 3
1121+
3 d 4
1122+
1123+
To specify the name of the new column use `name`.
1124+
1125+
>>> s.reset_index(name='values')
1126+
idx values
1127+
0 a 1
1128+
1 b 2
1129+
2 c 3
1130+
3 d 4
1131+
1132+
To generate a new Series with the default set `drop` to True.
1133+
1134+
>>> s.reset_index(drop=True)
1135+
0 1
1136+
1 2
1137+
2 3
1138+
3 4
1139+
Name: foo, dtype: int64
1140+
1141+
To update the Series in place, without generating a new one
1142+
set `inplace` to True. Note that it also requires ``drop=True``.
1143+
1144+
>>> s.reset_index(inplace=True, drop=True)
1145+
>>> s
1146+
0 1
1147+
1 2
1148+
2 3
1149+
3 4
1150+
Name: foo, dtype: int64
1151+
1152+
The `level` parameter is interesting for Series with a multi-level
1153+
index.
1154+
1155+
>>> arrays = [np.array(['bar', 'bar', 'baz', 'baz']),
1156+
... np.array(['one', 'two', 'one', 'two'])]
11081157
>>> s2 = pd.Series(
1109-
... range(8),
1158+
... range(4), name='foo',
11101159
... index=pd.MultiIndex.from_arrays(arrays,
11111160
... names=['a', 'b']))
1161+
1162+
To remove a specific level from the Index, use `level`.
1163+
11121164
>>> s2.reset_index(level='a')
1113-
a 0
1165+
a foo
11141166
b
1115-
one bar 0
1116-
two bar 1
1117-
one baz 2
1118-
two baz 3
1119-
one foo 4
1120-
two foo 5
1121-
one qux 6
1122-
two qux 7
1167+
one bar 0
1168+
two bar 1
1169+
one baz 2
1170+
two baz 3
1171+
1172+
If `level` is not set, all levels are removed from the Index.
1173+
1174+
>>> s2.reset_index()
1175+
a b foo
1176+
0 bar one 0
1177+
1 bar two 1
1178+
2 baz one 2
1179+
3 baz two 3
11231180
"""
11241181
inplace = validate_bool_kwarg(inplace, 'inplace')
11251182
if drop:

0 commit comments

Comments
 (0)