Skip to content

Commit a3cc1e6

Browse files
pajachietPingviinituutti
authored andcommitted
BUG: fix df.where(cond) when cond is empty (pandas-dev#21947)
1 parent a6e8a68 commit a3cc1e6

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

doc/source/whatsnew/v0.24.0.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ Removal of prior version deprecations/changes
10471047
Performance Improvements
10481048
~~~~~~~~~~~~~~~~~~~~~~~~
10491049

1050-
- Slicing Series and Dataframes with an monotonically increasing :class:`CategoricalIndex`
1050+
- Slicing Series and DataFrames with an monotonically increasing :class:`CategoricalIndex`
10511051
is now very fast and has speed comparable to slicing with an ``Int64Index``.
10521052
The speed increase is both when indexing by label (using .loc) and position(.iloc) (:issue:`20395`)
10531053
Slicing a monotonically increasing :class:`CategoricalIndex` itself (i.e. ``ci[1000:2000]``)
@@ -1150,7 +1150,7 @@ Timezones
11501150
- Fixed bug where :meth:`DataFrame.describe` and :meth:`Series.describe` on tz-aware datetimes did not show `first` and `last` result (:issue:`21328`)
11511151
- Bug in :class:`DatetimeIndex` comparisons failing to raise ``TypeError`` when comparing timezone-aware ``DatetimeIndex`` against ``np.datetime64`` (:issue:`22074`)
11521152
- Bug in ``DataFrame`` assignment with a timezone-aware scalar (:issue:`19843`)
1153-
- Bug in :func:`Dataframe.asof` that raised a ``TypeError`` when attempting to compare tz-naive and tz-aware timestamps (:issue:`21194`)
1153+
- Bug in :func:`DataFrame.asof` that raised a ``TypeError`` when attempting to compare tz-naive and tz-aware timestamps (:issue:`21194`)
11541154
- Bug when constructing a :class:`DatetimeIndex` with :class:`Timestamp`s constructed with the ``replace`` method across DST (:issue:`18785`)
11551155
- Bug when setting a new value with :meth:`DataFrame.loc` with a :class:`DatetimeIndex` with a DST transition (:issue:`18308`, :issue:`20724`)
11561156
- Bug in :meth:`DatetimeIndex.unique` that did not re-localize tz-aware dates correctly (:issue:`21737`)
@@ -1313,6 +1313,7 @@ Reshaping
13131313
- Bug in :func:`pandas.concat` when joining resampled DataFrames with timezone aware index (:issue:`13783`)
13141314
- Bug in :meth:`Series.combine_first` with ``datetime64[ns, tz]`` dtype which would return tz-naive result (:issue:`21469`)
13151315
- Bug in :meth:`Series.where` and :meth:`DataFrame.where` with ``datetime64[ns, tz]`` dtype (:issue:`21546`)
1316+
- Bug in :meth:`DataFrame.where` with an empty DataFrame and empty ``cond`` having non-bool dtype (:issue:`21947`)
13161317
- Bug in :meth:`Series.mask` and :meth:`DataFrame.mask` with ``list`` conditionals (:issue:`21891`)
13171318
- Bug in :meth:`DataFrame.replace` raises RecursionError when converting OutOfBounds ``datetime64[ns, tz]`` (:issue:`20380`)
13181319
- :func:`pandas.core.groupby.GroupBy.rank` now raises a ``ValueError`` when an invalid value is passed for argument ``na_option`` (:issue:`22124`)

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8142,7 +8142,7 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
81428142
# This is a single-dimensional object.
81438143
if not is_bool_dtype(cond):
81448144
raise ValueError(msg.format(dtype=cond.dtype))
8145-
else:
8145+
elif not cond.empty:
81468146
for dt in cond.dtypes:
81478147
if not is_bool_dtype(dt):
81488148
raise ValueError(msg.format(dtype=dt))

pandas/tests/frame/test_indexing.py

+8
Original file line numberDiff line numberDiff line change
@@ -2877,6 +2877,14 @@ def test_where_none(self):
28772877
'on mixed-type'):
28782878
df.where(~isna(df), None, inplace=True)
28792879

2880+
def test_where_empty_df_and_empty_cond_having_non_bool_dtypes(self):
2881+
# see gh-21947
2882+
df = pd.DataFrame(columns=["a"])
2883+
cond = df.applymap(lambda x: x > 0)
2884+
2885+
result = df.where(cond)
2886+
tm.assert_frame_equal(result, df)
2887+
28802888
def test_where_align(self):
28812889

28822890
def create():

0 commit comments

Comments
 (0)