Skip to content

Commit 428a823

Browse files
authored
Merge dev into pre-commit-ci-update-config
2 parents f86a6dc + 6ced169 commit 428a823

15 files changed

+70
-12
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## [Unreleased]
44

5+
- [ENH] Added support for pd.Series.select - Issue #1394 @samukweku
6+
57
## [v0.30.0] - 2024-12-04
68

79
## [v0.29.2] - 2024-09-28
@@ -28,7 +30,7 @@
2830
- [ENH] Added a `clean_names` method for polars - it can be used to clean the column names, or clean column values . Issue #1343 @samukweku
2931
- [ENH] Improved performance for non-equi joins when using numba - @samukweku PR #1341
3032
- [ENH] pandas Index,Series, DataFrame now supported in the `complete` method. - PR #1369 @samukweku
31-
- [ENH] Improve performance for `first/last` in \`conditional_join, when the join columns in the right dataframe are sorted. - PR #1382 @samukweku
33+
- [ENH] Improve performance for `first/last` in conditional_join, when the join columns in the right dataframe are sorted. - PR #1382 @samukweku
3234

3335
## [v0.27.0] - 2024-03-21
3436

janitor/functions/select.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -328,24 +328,27 @@ def select_rows(
328328

329329

330330
@pf.register_dataframe_method
331+
@pf.register_series_method
331332
@deprecated_alias(rows="index")
332333
def select(
333-
df: pd.DataFrame,
334+
df: pd.DataFrame | pd.Series,
334335
*args: tuple,
335336
index: Any = None,
336337
columns: Any = None,
337338
axis: str = "columns",
338339
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.
341342
342343
It accepts a string, shell-like glob strings `(*string*)`,
343344
regex, slice, array-like object, or a list of the previous options.
344345
345346
Selection on a MultiIndex on a level, or multiple levels,
346347
is possible with a dictionary.
347348
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.
349352
350353
Selection can be inverted with the `DropLabel` class.
351354
@@ -366,6 +369,8 @@ def select(
366369
- 0.26.0
367370
- Added variable `args`, `invert` and `axis` parameters.
368371
- `rows` keyword deprecated in favour of `index`.
372+
- 0.31.0
373+
- Add support for pd.Series.
369374
370375
Examples:
371376
>>> import pandas as pd
@@ -429,9 +434,8 @@ def select(
429434
ValueError: If args and index/columns are provided.
430435
431436
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.
433438
""" # noqa: E501
434-
435439
if args:
436440
check("invert", invert, [bool])
437441
if (index is not None) or (columns is not None):
@@ -851,16 +855,19 @@ def _index_converter(arr, index):
851855

852856

853857
def _select(
854-
df: pd.DataFrame,
858+
df: pd.DataFrame | pd.Series,
855859
invert: bool = False,
856860
rows=None,
857861
columns=None,
858-
) -> pd.DataFrame:
862+
) -> pd.DataFrame | pd.Series:
859863
"""
860864
Index DataFrame on the index or columns.
865+
If it is a Series, indexing is only on the index.
861866
862-
Returns a DataFrame.
867+
Returns a DataFrame or Series.
863868
"""
869+
if (columns is not None) and isinstance(df, pd.Series):
870+
raise ValueError("columns axis is not supported for pd.Series.select")
864871
if rows is None:
865872
row_indexer = slice(None)
866873
else:
@@ -870,6 +877,8 @@ def _select(
870877
row_indexer[outcome] = False
871878
else:
872879
row_indexer = outcome
880+
if isinstance(df, pd.Series):
881+
return df.iloc[row_indexer]
873882
if columns is None:
874883
column_indexer = slice(None)
875884
else:

mkdocs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ plugins:
6363
docstring_style: "google"
6464
docstring_options:
6565
trim_doctest_flags: true
66-
show_if_no_docstring: false
66+
show_if_no_docstring: false
6767
show_root_toc_entry: false
6868
show_root_heading: false
6969
show_submodules: true

mkdocs/api/biology.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# Biology
22

33
::: janitor.biology
4+
options:
5+
filters:
6+
- "!^_"

mkdocs/api/chemistry.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# Chemistry
22

33
::: janitor.chemistry
4+
options:
5+
filters:
6+
- "!^_"

mkdocs/api/engineering.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# Engineering
22

33
::: janitor.engineering
4+
options:
5+
filters:
6+
- "!^_"

mkdocs/api/finance.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# Finance
22

33
::: janitor.finance
4+
options:
5+
filters:
6+
- "!^_"

mkdocs/api/functions.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
::: janitor.functions
44
options:
5+
filters:
6+
- "!^_"
57
members:
68
- add_columns
79
- also

mkdocs/api/io.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# Input/Output (io)
22

33
::: janitor.io
4+
options:
5+
filters:
6+
- "!^_"

mkdocs/api/math.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# Math
22

33
::: janitor.math
4+
options:
5+
filters:
6+
- "!^_"

mkdocs/api/ml.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# Machine Learning
22

33
::: janitor.ml
4+
options:
5+
filters:
6+
- "!^_"

mkdocs/api/polars.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
::: janitor.polars
44
options:
5+
filters:
6+
- "!^_"
57
members:
68
- clean_names
79
- complete

mkdocs/api/timeseries.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# Timeseries
22

33
::: janitor.timeseries
4+
options:
5+
filters:
6+
- "!^_"

mkdocs/api/xarray.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# XArray
22

33
::: janitor.xarray.functions
4+
options:
5+
filters:
6+
- "!^_"

tests/functions/test_select.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import pandas as pd
33
import pytest
4-
from pandas.testing import assert_frame_equal
4+
from pandas.testing import assert_frame_equal, assert_series_equal
55

66
from janitor.functions.select import DropLabel
77

@@ -22,6 +22,22 @@ def dataframe():
2222
)
2323

2424

25+
def test_series_axis(dataframe):
26+
"""Raise if object is a Series and axis is a column"""
27+
with pytest.raises(
28+
ValueError, match="columns axis is not supported for pd.Series.select"
29+
):
30+
dataframe.iloc[:, 0].select("bar", axis="columns")
31+
32+
33+
def test_series_select(dataframe):
34+
"""Test output when selecting on a Series"""
35+
series = dataframe.iloc[:, 0]
36+
actual = series.select(index="bar")
37+
expected = series.loc[["bar"]]
38+
assert_series_equal(actual, expected)
39+
40+
2541
def test_args_and_rows_and_columns(dataframe):
2642
"""
2743
Raise if args and rows/columns are provided.

0 commit comments

Comments
 (0)