Skip to content

Commit 96148be

Browse files
committed
BUG: DataFrame.where does not handle Series slice correctly (fixes pandas-dev#10218)
1 parent bc7d48f commit 96148be

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

pandas/core/generic.py

+2
Original file line numberDiff line numberDiff line change
@@ -3439,6 +3439,8 @@ def where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
34393439
try_cast=False, raise_on_error=True):
34403440

34413441
if isinstance(cond, NDFrame):
3442+
if isinstance(cond, pd.Series) and isinstance(self, pd.DataFrame):
3443+
cond = self._getitem_array(cond)
34423444
cond = cond.reindex(**self._construct_axes_dict())
34433445
else:
34443446
if not hasattr(cond, 'shape'):

pandas/tests/test_frame.py

+7
Original file line numberDiff line numberDiff line change
@@ -9846,6 +9846,13 @@ def _check_set(df, cond, check_dtypes = True):
98469846
cond = (df >= 0)[1:]
98479847
_check_set(df, cond)
98489848

9849+
# GH 10218
9850+
# test DataFrame.where with Series slicing
9851+
df = DataFrame({'a': range(3), 'b': range(4, 7)})
9852+
result = df.where(df['a'] == 1)
9853+
expected = df[df['a'] == 1].reindex(df.index)
9854+
assert_frame_equal(result, expected)
9855+
98499856
def test_where_bug(self):
98509857

98519858
# GH 2793

0 commit comments

Comments
 (0)