@@ -2037,145 +2037,6 @@ def _sanitize_column(self, key, value):
20372037 def _series (self ):
20382038 return self ._data .get_series_dict ()
20392039
2040- def xs (self , key , axis = 0 , level = None , copy = True , drop_level = True ):
2041- """
2042- Returns a cross-section (row(s) or column(s)) from the DataFrame.
2043- Defaults to cross-section on the rows (axis=0).
2044-
2045- Parameters
2046- ----------
2047- key : object
2048- Some label contained in the index, or partially in a MultiIndex
2049- axis : int, default 0
2050- Axis to retrieve cross-section on
2051- level : object, defaults to first n levels (n=1 or len(key))
2052- In case of a key partially contained in a MultiIndex, indicate
2053- which levels are used. Levels can be referred by label or position.
2054- copy : boolean, default True
2055- Whether to make a copy of the data
2056- drop_level : boolean, default True
2057- If False, returns object with same levels as self.
2058-
2059- Examples
2060- --------
2061- >>> df
2062- A B C
2063- a 4 5 2
2064- b 4 0 9
2065- c 9 7 3
2066- >>> df.xs('a')
2067- A 4
2068- B 5
2069- C 2
2070- Name: a
2071- >>> df.xs('C', axis=1)
2072- a 2
2073- b 9
2074- c 3
2075- Name: C
2076- >>> s = df.xs('a', copy=False)
2077- >>> s['A'] = 100
2078- >>> df
2079- A B C
2080- a 100 5 2
2081- b 4 0 9
2082- c 9 7 3
2083-
2084-
2085- >>> df
2086- A B C D
2087- first second third
2088- bar one 1 4 1 8 9
2089- two 1 7 5 5 0
2090- baz one 1 6 6 8 0
2091- three 2 5 3 5 3
2092- >>> df.xs(('baz', 'three'))
2093- A B C D
2094- third
2095- 2 5 3 5 3
2096- >>> df.xs('one', level=1)
2097- A B C D
2098- first third
2099- bar 1 4 1 8 9
2100- baz 1 6 6 8 0
2101- >>> df.xs(('baz', 2), level=[0, 'third'])
2102- A B C D
2103- second
2104- three 5 3 5 3
2105-
2106- Returns
2107- -------
2108- xs : Series or DataFrame
2109-
2110- """
2111- axis = self ._get_axis_number (axis )
2112- labels = self ._get_axis (axis )
2113- if level is not None :
2114- loc , new_ax = labels .get_loc_level (key , level = level ,
2115- drop_level = drop_level )
2116-
2117- if not copy and not isinstance (loc , slice ):
2118- raise ValueError ('Cannot retrieve view (copy=False)' )
2119-
2120- # level = 0
2121- loc_is_slice = isinstance (loc , slice )
2122- if not loc_is_slice :
2123- indexer = [slice (None )] * 2
2124- indexer [axis ] = loc
2125- indexer = tuple (indexer )
2126- else :
2127- indexer = loc
2128- lev_num = labels ._get_level_number (level )
2129- if labels .levels [lev_num ].inferred_type == 'integer' :
2130- indexer = self .index [loc ]
2131-
2132- # select on the correct axis
2133- if axis == 1 and loc_is_slice :
2134- indexer = slice (None ), indexer
2135- result = self .ix [indexer ]
2136- setattr (result , result ._get_axis_name (axis ), new_ax )
2137- return result
2138-
2139- if axis == 1 :
2140- data = self [key ]
2141- if copy :
2142- data = data .copy ()
2143- return data
2144-
2145- self ._consolidate_inplace ()
2146-
2147- index = self .index
2148- if isinstance (index , MultiIndex ):
2149- loc , new_index = self .index .get_loc_level (key ,
2150- drop_level = drop_level )
2151- else :
2152- loc = self .index .get_loc (key )
2153-
2154- if isinstance (loc , np .ndarray ):
2155- if loc .dtype == np .bool_ :
2156- inds , = loc .nonzero ()
2157- return self .take (inds , axis = axis , convert = False )
2158- else :
2159- return self .take (loc , axis = axis , convert = True )
2160-
2161- if not np .isscalar (loc ):
2162- new_index = self .index [loc ]
2163-
2164- if np .isscalar (loc ):
2165-
2166- new_values , copy = self ._data .fast_2d_xs (loc , copy = copy )
2167- result = Series (new_values , index = self .columns ,
2168- name = self .index [loc ])
2169- result .is_copy = True
2170-
2171- else :
2172- result = self [loc ]
2173- result .index = new_index
2174-
2175- return result
2176-
2177- _xs = xs
2178-
21792040 def lookup (self , row_labels , col_labels ):
21802041 """Label-based "fancy indexing" function for DataFrame.
21812042 Given equal-length arrays of row and column labels, return an
0 commit comments