Skip to content

Commit c6eec25

Browse files
JennaVergeynstjorisvandenbossche
authored andcommitted
DOC: update and seperate the Series.drop and Dataframe.drop docstring (#20120)
1 parent ec7587e commit c6eec25

File tree

3 files changed

+214
-66
lines changed

3 files changed

+214
-66
lines changed

pandas/core/frame.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3227,6 +3227,129 @@ def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True,
32273227
method=method, level=level, copy=copy,
32283228
limit=limit, fill_value=fill_value)
32293229

3230+
def drop(self, labels=None, axis=0, index=None, columns=None,
3231+
level=None, inplace=False, errors='raise'):
3232+
"""
3233+
Drop specified labels from rows or columns.
3234+
3235+
Remove rows or columns by specifying label names and corresponding
3236+
axis, or by specifying directly index or column names. When using a
3237+
multi-index, labels on different levels can be removed by specifying
3238+
the level.
3239+
3240+
Parameters
3241+
----------
3242+
labels : single label or list-like
3243+
Index or column labels to drop.
3244+
axis : {0 or 'index', 1 or 'columns'}, default 0
3245+
Whether to drop labels from the index (0 or 'index') or
3246+
columns (1 or 'columns').
3247+
index, columns : single label or list-like
3248+
Alternative to specifying axis (``labels, axis=1``
3249+
is equivalent to ``columns=labels``).
3250+
3251+
.. versionadded:: 0.21.0
3252+
level : int or level name, optional
3253+
For MultiIndex, level from which the labels will be removed.
3254+
inplace : bool, default False
3255+
If True, do operation inplace and return None.
3256+
errors : {'ignore', 'raise'}, default 'raise'
3257+
If 'ignore', suppress error and only existing labels are
3258+
dropped.
3259+
3260+
Returns
3261+
-------
3262+
dropped : pandas.DataFrame
3263+
3264+
See Also
3265+
--------
3266+
DataFrame.loc : Label-location based indexer for selection by label.
3267+
DataFrame.dropna : Return DataFrame with labels on given axis omitted
3268+
where (all or any) data are missing
3269+
DataFrame.drop_duplicates : Return DataFrame with duplicate rows
3270+
removed, optionally only considering certain columns
3271+
Series.drop : Return Series with specified index labels removed.
3272+
3273+
Raises
3274+
------
3275+
KeyError
3276+
If none of the labels are found in the selected axis
3277+
3278+
Examples
3279+
--------
3280+
>>> df = pd.DataFrame(np.arange(12).reshape(3,4),
3281+
... columns=['A', 'B', 'C', 'D'])
3282+
>>> df
3283+
A B C D
3284+
0 0 1 2 3
3285+
1 4 5 6 7
3286+
2 8 9 10 11
3287+
3288+
Drop columns
3289+
3290+
>>> df.drop(['B', 'C'], axis=1)
3291+
A D
3292+
0 0 3
3293+
1 4 7
3294+
2 8 11
3295+
3296+
>>> df.drop(columns=['B', 'C'])
3297+
A D
3298+
0 0 3
3299+
1 4 7
3300+
2 8 11
3301+
3302+
Drop a row by index
3303+
3304+
>>> df.drop([0, 1])
3305+
A B C D
3306+
2 8 9 10 11
3307+
3308+
Drop columns and/or rows of MultiIndex DataFrame
3309+
3310+
>>> midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],
3311+
... ['speed', 'weight', 'length']],
3312+
... labels=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
3313+
... [0, 1, 2, 0, 1, 2, 0, 1, 2]])
3314+
>>> df = pd.DataFrame(index=midx, columns=['big', 'small'],
3315+
... data=[[45, 30], [200, 100], [1.5, 1], [30, 20],
3316+
... [250, 150], [1.5, 0.8], [320, 250],
3317+
... [1, 0.8], [0.3,0.2]])
3318+
>>> df
3319+
big small
3320+
lama speed 45.0 30.0
3321+
weight 200.0 100.0
3322+
length 1.5 1.0
3323+
cow speed 30.0 20.0
3324+
weight 250.0 150.0
3325+
length 1.5 0.8
3326+
falcon speed 320.0 250.0
3327+
weight 1.0 0.8
3328+
length 0.3 0.2
3329+
3330+
>>> df.drop(index='cow', columns='small')
3331+
big
3332+
lama speed 45.0
3333+
weight 200.0
3334+
length 1.5
3335+
falcon speed 320.0
3336+
weight 1.0
3337+
length 0.3
3338+
3339+
>>> df.drop(index='length', level=1)
3340+
big small
3341+
lama speed 45.0 30.0
3342+
weight 200.0 100.0
3343+
cow speed 30.0 20.0
3344+
weight 250.0 150.0
3345+
falcon speed 320.0 250.0
3346+
weight 1.0 0.8
3347+
"""
3348+
return super(DataFrame, self).drop(labels=labels, axis=axis,
3349+
index=index, columns=columns,
3350+
level=level, inplace=inplace,
3351+
errors=errors)
3352+
32303353
@rewrite_axis_style_signature('mapper', [('copy', True),
32313354
('inplace', False),
32323355
('level', None)])

pandas/core/generic.py

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2836,73 +2836,7 @@ def reindex_like(self, other, method=None, copy=True, limit=None,
28362836

28372837
def drop(self, labels=None, axis=0, index=None, columns=None, level=None,
28382838
inplace=False, errors='raise'):
2839-
"""
2840-
Return new object with labels in requested axis removed.
28412839

2842-
Parameters
2843-
----------
2844-
labels : single label or list-like
2845-
Index or column labels to drop.
2846-
axis : int or axis name
2847-
Whether to drop labels from the index (0 / 'index') or
2848-
columns (1 / 'columns').
2849-
index, columns : single label or list-like
2850-
Alternative to specifying `axis` (``labels, axis=1`` is
2851-
equivalent to ``columns=labels``).
2852-
2853-
.. versionadded:: 0.21.0
2854-
level : int or level name, default None
2855-
For MultiIndex
2856-
inplace : bool, default False
2857-
If True, do operation inplace and return None.
2858-
errors : {'ignore', 'raise'}, default 'raise'
2859-
If 'ignore', suppress error and existing labels are dropped.
2860-
2861-
Returns
2862-
-------
2863-
dropped : type of caller
2864-
2865-
Raises
2866-
------
2867-
KeyError
2868-
If none of the labels are found in the selected axis
2869-
2870-
Examples
2871-
--------
2872-
>>> df = pd.DataFrame(np.arange(12).reshape(3,4),
2873-
columns=['A', 'B', 'C', 'D'])
2874-
>>> df
2875-
A B C D
2876-
0 0 1 2 3
2877-
1 4 5 6 7
2878-
2 8 9 10 11
2879-
2880-
Drop columns
2881-
2882-
>>> df.drop(['B', 'C'], axis=1)
2883-
A D
2884-
0 0 3
2885-
1 4 7
2886-
2 8 11
2887-
2888-
>>> df.drop(columns=['B', 'C'])
2889-
A D
2890-
0 0 3
2891-
1 4 7
2892-
2 8 11
2893-
2894-
Drop a row by index
2895-
2896-
>>> df.drop([0, 1])
2897-
A B C D
2898-
2 8 9 10 11
2899-
2900-
Notes
2901-
-----
2902-
Specifying both `labels` and `index` or `columns` will raise a
2903-
ValueError.
2904-
2905-
"""
29062840
inplace = validate_bool_kwarg(inplace, 'inplace')
29072841

29082842
if labels is not None:

pandas/core/series.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3122,6 +3122,97 @@ def rename(self, index=None, **kwargs):
31223122
def reindex(self, index=None, **kwargs):
31233123
return super(Series, self).reindex(index=index, **kwargs)
31243124

3125+
def drop(self, labels=None, axis=0, index=None, columns=None,
3126+
level=None, inplace=False, errors='raise'):
3127+
"""
3128+
Return Series with specified index labels removed.
3129+
3130+
Remove elements of a Series based on specifying the index labels.
3131+
When using a multi-index, labels on different levels can be removed
3132+
by specifying the level.
3133+
3134+
Parameters
3135+
----------
3136+
labels : single label or list-like
3137+
Index labels to drop.
3138+
axis : 0, default 0
3139+
Redundant for application on Series.
3140+
index, columns : None
3141+
Redundant for application on Series, but index can be used instead
3142+
of labels.
3143+
3144+
.. versionadded:: 0.21.0
3145+
level : int or level name, optional
3146+
For MultiIndex, level for which the labels will be removed.
3147+
inplace : bool, default False
3148+
If True, do operation inplace and return None.
3149+
errors : {'ignore', 'raise'}, default 'raise'
3150+
If 'ignore', suppress error and only existing labels are dropped.
3151+
3152+
Returns
3153+
-------
3154+
dropped : pandas.Series
3155+
3156+
See Also
3157+
--------
3158+
Series.reindex : Return only specified index labels of Series.
3159+
Series.dropna : Return series without null values.
3160+
Series.drop_duplicates : Return Series with duplicate values removed.
3161+
DataFrame.drop : Drop specified labels from rows or columns.
3162+
3163+
Raises
3164+
------
3165+
KeyError
3166+
If none of the labels are found in the index.
3167+
3168+
Examples
3169+
--------
3170+
>>> s = pd.Series(data=np.arange(3), index=['A','B','C'])
3171+
>>> s
3172+
A 0
3173+
B 1
3174+
C 2
3175+
dtype: int64
3176+
3177+
Drop labels B en C
3178+
3179+
>>> s.drop(labels=['B','C'])
3180+
A 0
3181+
dtype: int64
3182+
3183+
Drop 2nd level label in MultiIndex Series
3184+
3185+
>>> midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],
3186+
... ['speed', 'weight', 'length']],
3187+
... labels=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
3188+
... [0, 1, 2, 0, 1, 2, 0, 1, 2]])
3189+
>>> s = pd.Series([45, 200, 1.2, 30, 250, 1.5, 320, 1, 0.3],
3190+
... index=midx)
3191+
>>> s
3192+
lama speed 45.0
3193+
weight 200.0
3194+
length 1.2
3195+
cow speed 30.0
3196+
weight 250.0
3197+
length 1.5
3198+
falcon speed 320.0
3199+
weight 1.0
3200+
length 0.3
3201+
dtype: float64
3202+
3203+
>>> s.drop(labels='weight', level=1)
3204+
lama speed 45.0
3205+
length 1.2
3206+
cow speed 30.0
3207+
length 1.5
3208+
falcon speed 320.0
3209+
length 0.3
3210+
dtype: float64
3211+
"""
3212+
return super(Series, self).drop(labels=labels, axis=axis, index=index,
3213+
columns=columns, level=level,
3214+
inplace=inplace, errors=errors)
3215+
31253216
@Appender(generic._shared_docs['fillna'] % _shared_doc_kwargs)
31263217
def fillna(self, value=None, method=None, axis=None, inplace=False,
31273218
limit=None, downcast=None, **kwargs):

0 commit comments

Comments
 (0)