Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add case_when method #56059

Merged
merged 43 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
f48502f
updates
samukweku Nov 19, 2023
40057c7
add test for default if Series
samukweku Nov 19, 2023
4a8be16
Merge remote-tracking branch 'upstream/main' into samukweku/case_when…
samukweku Nov 23, 2023
089bbe6
updates based on feedback
samukweku Nov 23, 2023
bcfd458
Merge remote-tracking branch 'upstream/main' into samukweku/case_when…
samukweku Nov 30, 2023
b95ce55
updates based on feedback
samukweku Nov 30, 2023
acc3fdb
Merge remote-tracking branch 'upstream/main' into samukweku/case_when…
samukweku Nov 30, 2023
8be4349
update typing hints for *args, based on feedback
samukweku Nov 30, 2023
8d08458
Merge remote-tracking branch 'upstream/main' into samukweku/case_when…
samukweku Dec 1, 2023
ec18086
update typehints; add caselist argument - based on feedback
samukweku Dec 1, 2023
0b72fbb
cleanup docstrings
samukweku Dec 1, 2023
0085956
Merge remote-tracking branch 'upstream/main' into samukweku/case_when…
samukweku Dec 15, 2023
a441481
support method only for case_when
samukweku Dec 15, 2023
29ad697
minor update
samukweku Dec 15, 2023
bf740f9
fix test
samukweku Dec 15, 2023
264a675
remove redundant tests
samukweku Dec 15, 2023
2a3035e
cleanup docs
samukweku Dec 15, 2023
5e33304
use singular version - common_dtype
samukweku Dec 22, 2023
5c7c287
Merge remote-tracking branch 'upstream/main' into samukweku/case_when…
samukweku Dec 22, 2023
8569cd1
fix doctest failure
samukweku Dec 23, 2023
bbb5887
fix for whatnew
samukweku Dec 23, 2023
e03e3dc
Update doc/source/whatsnew/v2.2.0.rst
samukweku Dec 23, 2023
283488f
Update v2.2.0.rst
phofl Dec 23, 2023
7a8694c
Update v2.2.0.rst
phofl Dec 23, 2023
f6cf725
Merge remote-tracking branch 'upstream/main' into samukweku/case_when…
samukweku Dec 24, 2023
67dfcaa
improve typing and add test for callable
samukweku Dec 24, 2023
3da7cf2
fix typing error
samukweku Dec 24, 2023
bdc54f6
Update pandas/core/series.py
samukweku Dec 25, 2023
649fb84
Merge branch 'main' into samukweku/case_when_function
rhshadrach Dec 27, 2023
b68d20e
Update doc/source/whatsnew/v2.2.0.rst
samukweku Dec 28, 2023
b4de208
PERF: resolution, is_normalized (#56637)
jbrockmendel Dec 27, 2023
5966bfe
TYP: more simple return types from ruff (#56628)
twoertwein Dec 27, 2023
3e404fa
ENH: Update CFF with publication reference, Zenodo DOI, and other det…
cgobat Dec 27, 2023
21659bc
DOC: Fixup CoW userguide (#56636)
phofl Dec 27, 2023
f6d8cd0
REF: check monotonicity inside _can_use_libjoin (#55342)
jbrockmendel Dec 27, 2023
becc626
DOC: Minor fixups for 2.2.0 whatsnew (#56632)
rhshadrach Dec 27, 2023
918a19e
TYP: Fix some PythonParser and Plotting types (#56643)
twoertwein Dec 27, 2023
5744df2
BUG: Series.to_numpy raising for arrow floats to numpy floats (#56644)
phofl Dec 28, 2023
bc6ba0e
updates based on feedback
samukweku Dec 28, 2023
a0f4797
add to API reference
samukweku Dec 28, 2023
cb7d6e3
fix whitespace
samukweku Dec 28, 2023
c8f0e2e
updates
samukweku Dec 28, 2023
9679b9e
Update series.py
samukweku Jan 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ including other versions of pandas.

Enhancements
~~~~~~~~~~~~
.. _whatsnew_220.enhancements.case_when:

Create a pandas Series based on one or more conditions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The :func:`case_when` function has been added to create a Series object based on one or more conditions. (:issue:`39154`)

.. ipython:: python

import pandas as pd

df = pd.DataFrame(dict(a=[1, 2, 3], b=[4, 5, 6]))
pd.case_when(
(df.a == 1, 'first'), # condition, replacement
(df.a.gt(1) & df.b.eq(5), 'second'), # condition, replacement
default = 'default', # optional
)

.. _whatsnew_220.enhancements.adbc_support:

Expand Down
11 changes: 8 additions & 3 deletions pandas/core/case_when.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
from pandas.core.construction import array as pd_array

if TYPE_CHECKING:
from pandas._typing import Series
from pandas._typing import (
ArrayLike,
Scalar,
Series,
)


def case_when(
*args: tuple[tuple],
default=lib.no_default,
*args: tuple[tuple[tuple[ArrayLike], tuple[ArrayLike | Scalar]]],
samukweku marked this conversation as resolved.
Show resolved Hide resolved
default: ArrayLike | Scalar = lib.no_default,
) -> Series:
"""
Replace values where the conditions are True.
Expand Down Expand Up @@ -189,6 +193,7 @@ def validate_case_when(args: tuple) -> tuple:
"""
Validates the variable arguments for the case_when function.
"""

if not len(args):
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError(
"provide at least one boolean condition, "
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5588,7 +5588,7 @@ def between(

def case_when(
samukweku marked this conversation as resolved.
Show resolved Hide resolved
self,
*args: tuple[tuple],
*args: tuple[tuple[tuple[ArrayLike], tuple[ArrayLike | Scalar]]],
) -> Series:
"""
Replace values where the conditions are True.
Expand Down
Loading