@@ -1775,9 +1775,8 @@ def __getitem__(self, key):
1775
elif isinstance (self .columns , MultiIndex ):
1775
elif isinstance (self .columns , MultiIndex ):
1776
return self ._getitem_multilevel (key )
1776
return self ._getitem_multilevel (key )
1777
elif isinstance (key , DataFrame ):
1777
elif isinstance (key , DataFrame ):
1778
- values = key .values
1778
+ if key .values .dtype == bool :
1779
- if values .dtype == bool :
1779
+ return self .where (key )
1780
- return self .values [values ]
1781
else :
1780
else :
1782
raise ValueError ('Cannot index using non-boolean DataFrame' )
1781
raise ValueError ('Cannot index using non-boolean DataFrame' )
1783
else :
1782
else :
@@ -1871,11 +1870,6 @@ def __setitem__(self, key, value):
1871
# support boolean setting with DataFrame input, e.g.
1870
# support boolean setting with DataFrame input, e.g.
1872
# df[df > df2] = 0
1871
# df[df > df2] = 0
1873
if isinstance (key , DataFrame ):
1872
if isinstance (key , DataFrame ):
1874
- if not (key .index .equals (self .index ) and
1875
- key .columns .equals (self .columns )):
1876
- raise PandasError ('Can only index with like-indexed '
1877
- 'DataFrame objects' )
1878
-
1879
self ._boolean_set (key , value )
1873
self ._boolean_set (key , value )
1880
elif isinstance (key , (np .ndarray , list )):
1874
elif isinstance (key , (np .ndarray , list )):
1881
return self ._set_item_multiple (key , value )
1875
return self ._set_item_multiple (key , value )
@@ -1884,18 +1878,13 @@ def __setitem__(self, key, value):
1884
self ._set_item (key , value )
1878
self ._set_item (key , value )
1885
1879
1886
def _boolean_set (self , key , value ):
1880
def _boolean_set (self , key , value ):
1887
- mask = key .values
1881
+ if key .values .dtype != np .bool_ :
1888
- if mask .dtype != np .bool_ :
1889
raise ValueError ('Must pass DataFrame with boolean values only' )
1882
raise ValueError ('Must pass DataFrame with boolean values only' )
1890
1883
1891
if self ._is_mixed_type :
1884
if self ._is_mixed_type :
1892
raise ValueError ('Cannot do boolean setting on mixed-type frame' )
1885
raise ValueError ('Cannot do boolean setting on mixed-type frame' )
1893
1886
1894
- if isinstance (value , DataFrame ):
1887
+ self .where (key , value , inplace = True )
1895
- assert (value ._indexed_same (self ))
1896
- np .putmask (self .values , mask , value .values )
1897
- else :
1898
- self .values [mask ] = value
1899
1888
1900
def _set_item_multiple (self , keys , value ):
1889
def _set_item_multiple (self , keys , value ):
1901
if isinstance (value , DataFrame ):
1890
if isinstance (value , DataFrame ):
@@ -4878,7 +4867,7 @@ def combineMult(self, other):
4878
"""
4867
"""
4879
return self .mul (other , fill_value = 1. )
4868
return self .mul (other , fill_value = 1. )
4880
4869
4881
- def where (self , cond , other ):
4870
+ def where (self , cond , other = NA , inplace = False ):
4882
"""
4871
"""
4883
Return a DataFrame with the same shape as self and whose corresponding
4872
Return a DataFrame with the same shape as self and whose corresponding
4884
entries are from self where cond is True and otherwise are from other.
4873
entries are from self where cond is True and otherwise are from other.
@@ -4893,6 +4882,9 @@ def where(self, cond, other):
4893
-------
4882
-------
4894
wh: DataFrame
4883
wh: DataFrame
4895
"""
4884
"""
4885
+ if not hasattr (cond ,'shape' ):
4886
+ raise ValueError ('where requires an ndarray like object for its condition' )
4887
+
4896
if isinstance (cond , np .ndarray ):
4888
if isinstance (cond , np .ndarray ):
4897
if cond .shape != self .shape :
4889
if cond .shape != self .shape :
4898
raise ValueError ('Array onditional must be same shape as self' )
4890
raise ValueError ('Array onditional must be same shape as self' )
@@ -4905,13 +4897,17 @@ def where(self, cond, other):
4905
if isinstance (other , DataFrame ):
4897
if isinstance (other , DataFrame ):
4906
_ , other = self .align (other , join = 'left' , fill_value = NA )
4898
_ , other = self .align (other , join = 'left' , fill_value = NA )
4907
4899
4900
+ if inplace :
4901
+ np .putmask (self .values , cond , other )
4902
+ return self
4903
+
4908
rs = np .where (cond , self , other )
4904
rs = np .where (cond , self , other )
4909
return self ._constructor (rs , self .index , self .columns )
4905
return self ._constructor (rs , self .index , self .columns )
4910
-
4906
+
4911
def mask (self , cond ):
4907
def mask (self , cond ):
4912
"""
4908
"""
4913
Returns copy of self whose values are replaced with nan if the
4909
Returns copy of self whose values are replaced with nan if the
4914
- corresponding entry in cond is False
4910
+ inverted condition is True
4915
4911
4916
Parameters
4912
Parameters
4917
----------
4913
----------
@@ -4921,7 +4917,7 @@ def mask(self, cond):
4921
-------
4917
-------
4922
wh: DataFrame
4918
wh: DataFrame
4923
"""
4919
"""
4924
- return self .where (cond , NA )
4920
+ return self .where (~ cond , NA )
4925
4921
4926
_EMPTY_SERIES = Series ([])
4922
_EMPTY_SERIES = Series ([])
4927
4923
0 commit comments