@@ -328,24 +328,27 @@ def select_rows(
328
328
329
329
330
330
@pf .register_dataframe_method
331
+ @pf .register_series_method
331
332
@deprecated_alias (rows = "index" )
332
333
def select (
333
- df : pd .DataFrame ,
334
+ df : pd .DataFrame | pd . Series ,
334
335
* args : tuple ,
335
336
index : Any = None ,
336
337
columns : Any = None ,
337
338
axis : str = "columns" ,
338
339
invert : bool = False ,
339
- ) -> pd .DataFrame :
340
- """Method-chainable selection of rows and columns.
340
+ ) -> pd .DataFrame | pd . Series :
341
+ """Method-chainable selection of rows and/or columns.
341
342
342
343
It accepts a string, shell-like glob strings `(*string*)`,
343
344
regex, slice, array-like object, or a list of the previous options.
344
345
345
346
Selection on a MultiIndex on a level, or multiple levels,
346
347
is possible with a dictionary.
347
348
348
- This method does not mutate the original DataFrame.
349
+ This method does not mutate the original DataFrame or Series.
350
+
351
+ If the pandas object is a Series, selection is possible only on the index.
349
352
350
353
Selection can be inverted with the `DropLabel` class.
351
354
@@ -366,6 +369,8 @@ def select(
366
369
- 0.26.0
367
370
- Added variable `args`, `invert` and `axis` parameters.
368
371
- `rows` keyword deprecated in favour of `index`.
372
+ - 0.31.0
373
+ - Add support for pd.Series.
369
374
370
375
Examples:
371
376
>>> import pandas as pd
@@ -429,9 +434,8 @@ def select(
429
434
ValueError: If args and index/columns are provided.
430
435
431
436
Returns:
432
- A pandas DataFrame with the specified rows and/or columns selected.
437
+ A pandas DataFrame or Series with the specified rows and/or columns selected.
433
438
""" # noqa: E501
434
-
435
439
if args :
436
440
check ("invert" , invert , [bool ])
437
441
if (index is not None ) or (columns is not None ):
@@ -851,16 +855,19 @@ def _index_converter(arr, index):
851
855
852
856
853
857
def _select (
854
- df : pd .DataFrame ,
858
+ df : pd .DataFrame | pd . Series ,
855
859
invert : bool = False ,
856
860
rows = None ,
857
861
columns = None ,
858
- ) -> pd .DataFrame :
862
+ ) -> pd .DataFrame | pd . Series :
859
863
"""
860
864
Index DataFrame on the index or columns.
865
+ If it is a Series, indexing is only on the index.
861
866
862
- Returns a DataFrame.
867
+ Returns a DataFrame or Series .
863
868
"""
869
+ if (columns is not None ) and isinstance (df , pd .Series ):
870
+ raise ValueError ("columns axis is not supported for pd.Series.select" )
864
871
if rows is None :
865
872
row_indexer = slice (None )
866
873
else :
@@ -870,6 +877,8 @@ def _select(
870
877
row_indexer [outcome ] = False
871
878
else :
872
879
row_indexer = outcome
880
+ if isinstance (df , pd .Series ):
881
+ return df .iloc [row_indexer ]
873
882
if columns is None :
874
883
column_indexer = slice (None )
875
884
else :
0 commit comments