BUG: DataFrame inplace where doesn't work for mixed datatype frames · Issue #2793 · pandas-dev/pandas (original) (raw)
Noticed the following: DataFrame.where
with inplace=True
only works when DataFrame
is of a single dtype (because it relies on calling np.putmask
on DataFrame.values
which is only a view in the single dtype case)
In [1]: import pandas as p
In [2]: import numpy as np
In [3]: df=p.DataFrame({'a': [1.0, 2.0, 3.0, 4.0], ...: 'b': [4.0, 3.0, 2.0, 1.0]}) # single dtype
In [4]: df.where(df > 2, np.nan) Out[4]: a b 0 NaN 4 1 NaN 3 2 3 NaN 3 4 NaN
In [5]: df.where(df > 2, np.nan, inplace=True)
In [6]: df Out[6]: a b 0 NaN 4 1 NaN 3 2 3 NaN 3 4 NaN
In [7]: df=p.DataFrame({'a': [1, 2, 3, 4], ...: 'b': [4.0, 3.0, 2.0, 1.0]}) # mixed dtypes
In [8]: df.where(df > 2, np.nan) # ok Out[8]: a b 0 NaN 4 1 NaN 3 2 3 NaN 3 4 NaN
In [9]: df.where(df > 2, np.nan, inplace=True) # not ok
In [10]: df Out[10]: a b 0 1 4 1 2 3 2 3 2 3 4 1