Skip to content

Commit 212a1d1

Browse files
tlentalijreback
authored andcommitted
Fix the docstring of xs in pandas/core/generic.py #22892 (#23913)
1 parent 1c6a968 commit 212a1d1

File tree

1 file changed

+81
-50
lines changed

1 file changed

+81
-50
lines changed

pandas/core/generic.py

+81-50
Original file line numberDiff line numberDiff line change
@@ -3420,71 +3420,102 @@ class max_speed
34203420

34213421
def xs(self, key, axis=0, level=None, drop_level=True):
34223422
"""
3423-
Returns a cross-section (row(s) or column(s)) from the
3424-
Series/DataFrame. Defaults to cross-section on the rows (axis=0).
3423+
Return cross-section from the Series/DataFrame.
3424+
3425+
This method takes a `key` argument to select data at a particular
3426+
level of a MultiIndex.
34253427
34263428
Parameters
34273429
----------
3428-
key : object
3429-
Some label contained in the index, or partially in a MultiIndex
3430-
axis : int, default 0
3431-
Axis to retrieve cross-section on
3430+
key : label or tuple of label
3431+
Label contained in the index, or partially in a MultiIndex.
3432+
axis : {0 or 'index', 1 or 'columns'}, default 0
3433+
Axis to retrieve cross-section on.
34323434
level : object, defaults to first n levels (n=1 or len(key))
34333435
In case of a key partially contained in a MultiIndex, indicate
34343436
which levels are used. Levels can be referred by label or position.
3435-
drop_level : boolean, default True
3437+
drop_level : bool, default True
34363438
If False, returns object with same levels as self.
34373439
3440+
Returns
3441+
-------
3442+
Series or DataFrame
3443+
Cross-section from the original Series or DataFrame
3444+
corresponding to the selected index levels.
3445+
3446+
See Also
3447+
--------
3448+
DataFrame.loc : Access a group of rows and columns
3449+
by label(s) or a boolean array.
3450+
DataFrame.iloc : Purely integer-location based indexing
3451+
for selection by position.
3452+
3453+
Notes
3454+
-----
3455+
`xs` can not be used to set values.
3456+
3457+
MultiIndex Slicers is a generic way to get/set values on
3458+
any level or levels.
3459+
It is a superset of `xs` functionality, see
3460+
:ref:`MultiIndex Slicers <advanced.mi_slicers>`.
3461+
34383462
Examples
34393463
--------
3464+
>>> d = {'num_legs': [4, 4, 2, 2],
3465+
... 'num_wings': [0, 0, 2, 2],
3466+
... 'class': ['mammal', 'mammal', 'mammal', 'bird'],
3467+
... 'animal': ['cat', 'dog', 'bat', 'penguin'],
3468+
... 'locomotion': ['walks', 'walks', 'flies', 'walks']}
3469+
>>> df = pd.DataFrame(data=d)
3470+
>>> df = df.set_index(['class', 'animal', 'locomotion'])
34403471
>>> df
3441-
A B C
3442-
a 4 5 2
3443-
b 4 0 9
3444-
c 9 7 3
3445-
>>> df.xs('a')
3446-
A 4
3447-
B 5
3448-
C 2
3449-
Name: a
3450-
>>> df.xs('C', axis=1)
3451-
a 2
3452-
b 9
3453-
c 3
3454-
Name: C
3472+
num_legs num_wings
3473+
class animal locomotion
3474+
mammal cat walks 4 0
3475+
dog walks 4 0
3476+
bat flies 2 2
3477+
bird penguin walks 2 2
34553478
3456-
>>> df
3457-
A B C D
3458-
first second third
3459-
bar one 1 4 1 8 9
3460-
two 1 7 5 5 0
3461-
baz one 1 6 6 8 0
3462-
three 2 5 3 5 3
3463-
>>> df.xs(('baz', 'three'))
3464-
A B C D
3465-
third
3466-
2 5 3 5 3
3467-
>>> df.xs('one', level=1)
3468-
A B C D
3469-
first third
3470-
bar 1 4 1 8 9
3471-
baz 1 6 6 8 0
3472-
>>> df.xs(('baz', 2), level=[0, 'third'])
3473-
A B C D
3474-
second
3475-
three 5 3 5 3
3479+
Get values at specified index
34763480
3477-
Returns
3478-
-------
3479-
xs : Series or DataFrame
3481+
>>> df.xs('mammal')
3482+
num_legs num_wings
3483+
animal locomotion
3484+
cat walks 4 0
3485+
dog walks 4 0
3486+
bat flies 2 2
34803487
3481-
Notes
3482-
-----
3483-
xs is only for getting, not setting values.
3488+
Get values at several indexes
3489+
3490+
>>> df.xs(('mammal', 'dog'))
3491+
num_legs num_wings
3492+
locomotion
3493+
walks 4 0
3494+
3495+
Get values at specified index and level
3496+
3497+
>>> df.xs('cat', level=1)
3498+
num_legs num_wings
3499+
class locomotion
3500+
mammal walks 4 0
3501+
3502+
Get values at several indexes and levels
3503+
3504+
>>> df.xs(('bird', 'walks'),
3505+
... level=[0, 'locomotion'])
3506+
num_legs num_wings
3507+
animal
3508+
penguin 2 2
3509+
3510+
Get values at specified column and axis
34843511
3485-
MultiIndex Slicers is a generic way to get/set values on any level or
3486-
levels. It is a superset of xs functionality, see
3487-
:ref:`MultiIndex Slicers <advanced.mi_slicers>`
3512+
>>> df.xs('num_wings', axis=1)
3513+
class animal locomotion
3514+
mammal cat walks 0
3515+
dog walks 0
3516+
bat flies 2
3517+
bird penguin walks 2
3518+
Name: num_wings, dtype: int64
34883519
"""
34893520
axis = self._get_axis_number(axis)
34903521
labels = self._get_axis(axis)

0 commit comments

Comments
 (0)