pandas icon indicating copy to clipboard operation
pandas copied to clipboard

ENH: Support scalar condition in `Series.where` and `DataFrame.where`

Open randolf-scholz opened this issue 2 years ago • 3 comments
trafficstars

Feature Type

  • [x] Changing existing functionality in pandas

Problem Description

For compatibility reasons with numpy.where it would be nice to allow scalar booleans as a condition to Series.where and DataFrame.where. Currently

import pandas as pd
s = pd.Series([1,2])
s.where(True)

raises ValueError: Array conditional must be same shape as self

Feature Description

Allow scalar boolean as input to

  • Series.where
  • Series.mask
  • DataFrame.where
  • DataFrame.mask

Alternative Solutions

idk.

Additional Context

Regarding documentation, I am not sure anything needs to be updated since both Series.where and DataFrame.where specify array-like as a valid input type, and scalars are considered array-like.

randolf-scholz avatar Jun 28 '23 11:06 randolf-scholz

Sure, seems reasonable to me.

lithomas1 avatar Jun 28 '23 16:06 lithomas1

@lithomas1 The PR has passed all tests. Currently I added to the changelog of 2.1.0, should I use 2.0.3 instead?

randolf-scholz avatar Jun 28 '23 18:06 randolf-scholz

No, I don't think this should be backported, 2.1.0 seems fine to me (its gonna come out around in a month or so).

lithomas1 avatar Jun 28 '23 18:06 lithomas1