Skip to content

Commit f2f5c82

Browse files
committed
add checks to categorical and tweak imports
1 parent 8540826 commit f2f5c82

File tree

6 files changed

+29
-25
lines changed

6 files changed

+29
-25
lines changed

pandas/computation/eval.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def _check_for_locals(expr, stack_level, parser):
148148

149149
def eval(expr, parser='pandas', engine=None, truediv=True,
150150
local_dict=None, global_dict=None, resolvers=(), level=0,
151-
target=None, inplace=None):
151+
target=None, inplace=True):
152152
"""Evaluate a Python expression as a string using various backends.
153153
154154
The following arithmetic operations are supported: ``+``, ``-``, ``*``,
@@ -211,10 +211,6 @@ def eval(expr, parser='pandas', engine=None, truediv=True,
211211
If expression mutates, whether to modify object inplace or return
212212
copy with mutation.
213213
214-
WARNING: inplace=None currently falls back to to True, but
215-
in a future version, will default to False. Use inplace=True
216-
explicitly rather than relying on the default.
217-
218214
Returns
219215
-------
220216
ndarray, numeric scalar, DataFrame, Series
@@ -232,7 +228,7 @@ def eval(expr, parser='pandas', engine=None, truediv=True,
232228
pandas.DataFrame.query
233229
pandas.DataFrame.eval
234230
"""
235-
inplace = com.enforce_bool_type(inplace)
231+
inplace = com._enforce_bool_type(inplace)
236232
first_expr = True
237233
if isinstance(expr, string_types):
238234
exprs = [e for e in expr.splitlines() if e != '']

pandas/core/categorical.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
is_integer_dtype, is_bool,
2424
is_list_like, is_sequence,
2525
is_scalar)
26-
from pandas.core.common import is_null_slice
26+
from pandas.core.common import is_null_slice, _enforce_bool_type
2727

2828
from pandas.core.algorithms import factorize, take_1d
2929
from pandas.core.base import (PandasObject, PandasDelegate,
@@ -615,6 +615,7 @@ def set_ordered(self, value, inplace=False):
615615
Whether or not to set the ordered attribute inplace or return a copy
616616
of this categorical with ordered set to the value
617617
"""
618+
inplace = _enforce_bool_type(inplace)
618619
self._validate_ordered(value)
619620
cat = self if inplace else self.copy()
620621
cat._ordered = value
@@ -631,7 +632,7 @@ def as_ordered(self, inplace=False):
631632
Whether or not to set the ordered attribute inplace or return a copy
632633
of this categorical with ordered set to True
633634
"""
634-
return self.set_ordered(True, inplace=inplace)
635+
return self.set_ordered(True, inplace=_enforce_bool_type(inplace))
635636

636637
def as_unordered(self, inplace=False):
637638
"""
@@ -643,7 +644,7 @@ def as_unordered(self, inplace=False):
643644
Whether or not to set the ordered attribute inplace or return a copy
644645
of this categorical with ordered set to False
645646
"""
646-
return self.set_ordered(False, inplace=inplace)
647+
return self.set_ordered(False, inplace=_enforce_bool_type(inplace))
647648

648649
def _get_ordered(self):
649650
""" Gets the ordered attribute """
@@ -702,6 +703,7 @@ def set_categories(self, new_categories, ordered=None, rename=False,
702703
remove_categories
703704
remove_unused_categories
704705
"""
706+
inplace = _enforce_bool_type(inplace)
705707
new_categories = self._validate_categories(new_categories)
706708
cat = self if inplace else self.copy()
707709
if rename:
@@ -754,6 +756,7 @@ def rename_categories(self, new_categories, inplace=False):
754756
remove_unused_categories
755757
set_categories
756758
"""
759+
inplace = _enforce_bool_type(inplace)
757760
cat = self if inplace else self.copy()
758761
cat.categories = new_categories
759762
if not inplace:
@@ -794,6 +797,7 @@ def reorder_categories(self, new_categories, ordered=None, inplace=False):
794797
remove_unused_categories
795798
set_categories
796799
"""
800+
inplace = _enforce_bool_type(inplace)
797801
if set(self._categories) != set(new_categories):
798802
raise ValueError("items in new_categories are not the same as in "
799803
"old categories")
@@ -832,6 +836,7 @@ def add_categories(self, new_categories, inplace=False):
832836
remove_unused_categories
833837
set_categories
834838
"""
839+
inplace = _enforce_bool_type(inplace)
835840
if not is_list_like(new_categories):
836841
new_categories = [new_categories]
837842
already_included = set(new_categories) & set(self._categories)
@@ -877,6 +882,7 @@ def remove_categories(self, removals, inplace=False):
877882
remove_unused_categories
878883
set_categories
879884
"""
885+
inplace = _enforce_bool_type(inplace)
880886
if not is_list_like(removals):
881887
removals = [removals]
882888

@@ -917,6 +923,7 @@ def remove_unused_categories(self, inplace=False):
917923
remove_categories
918924
set_categories
919925
"""
926+
inplace = _enforce_bool_type(inplace)
920927
cat = self if inplace else self.copy()
921928
idx, inv = np.unique(cat._codes, return_inverse=True)
922929

@@ -1316,6 +1323,7 @@ def sort_values(self, inplace=False, ascending=True, na_position='last'):
13161323
[NaN, NaN, 5.0, 2.0, 2.0]
13171324
Categories (2, int64): [2, 5]
13181325
"""
1326+
inplace = _enforce_bool_type(inplace)
13191327
if na_position not in ['last', 'first']:
13201328
raise ValueError('invalid na_position: {!r}'.format(na_position))
13211329

pandas/core/frame.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,7 +2220,7 @@ def query(self, expr, inplace=False, **kwargs):
22202220
else:
22212221
return new_data
22222222

2223-
def eval(self, expr, inplace=None, **kwargs):
2223+
def eval(self, expr, inplace=True, **kwargs):
22242224
"""Evaluate an expression in the context of the calling DataFrame
22252225
instance.
22262226
@@ -2232,10 +2232,6 @@ def eval(self, expr, inplace=None, **kwargs):
22322232
If the expression contains an assignment, whether to return a new
22332233
DataFrame or mutate the existing.
22342234
2235-
WARNING: inplace=None currently falls back to to True, but
2236-
in a future version, will default to False. Use inplace=True
2237-
explicitly rather than relying on the default.
2238-
22392235
.. versionadded:: 0.18.0
22402236
22412237
kwargs : dict

pandas/core/internals.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def fillna(self, value, limit=None, inplace=False, downcast=None,
361361
""" fillna on the block with the value. If we fail, then convert to
362362
ObjectBlock and try again
363363
"""
364-
inplace = com.enforce_bool_type(inplace)
364+
inplace = _enforce_bool_type(inplace)
365365

366366
if not self._can_hold_na:
367367
if inplace:
@@ -622,7 +622,7 @@ def replace(self, to_replace, value, inplace=False, filter=None,
622622
compatibility.
623623
"""
624624

625-
inplace = com.enforce_bool_type(inplace)
625+
inplace = _enforce_bool_type(inplace)
626626
original_to_replace = to_replace
627627
mask = isnull(self.values)
628628

@@ -896,7 +896,7 @@ def interpolate(self, method='pad', axis=0, index=None, values=None,
896896
fill_value=None, coerce=False, downcast=None, mgr=None,
897897
**kwargs):
898898

899-
inplace = com.enforce_bool_type(inplace)
899+
inplace = _enforce_bool_type(inplace)
900900

901901
def check_int_bool(self, inplace):
902902
# Only FloatBlocks will contain NaNs.
@@ -945,7 +945,7 @@ def _interpolate_with_fill(self, method='pad', axis=0, inplace=False,
945945
downcast=None, mgr=None):
946946
""" fillna but using the interpolate machinery """
947947

948-
inplace = com.enforce_bool_type(inplace)
948+
inplace = _enforce_bool_type(inplace)
949949

950950
# if we are coercing, then don't force the conversion
951951
# if the block can't hold the type
@@ -973,7 +973,7 @@ def _interpolate(self, method=None, index=None, values=None,
973973
mgr=None, **kwargs):
974974
""" interpolate using scipy wrappers """
975975

976-
inplace = com.enforce_bool_type(inplace)
976+
inplace = _enforce_bool_type(inplace)
977977
data = self.values if inplace else self.values.copy()
978978

979979
# only deal with floats
@@ -1495,7 +1495,7 @@ def putmask(self, mask, new, align=True, inplace=False, axis=0,
14951495
-------
14961496
a new block(s), the result of the putmask
14971497
"""
1498-
inplace = com.enforce_bool_type(inplace)
1498+
inplace = _enforce_bool_type(inplace)
14991499

15001500
# use block's copy logic.
15011501
# .values may be an Index which does shallow copy by default
@@ -1783,7 +1783,7 @@ def should_store(self, value):
17831783

17841784
def replace(self, to_replace, value, inplace=False, filter=None,
17851785
regex=False, mgr=None):
1786-
inplace = com.enforce_bool_type(inplace)
1786+
inplace = _enforce_bool_type(inplace)
17871787
to_replace_values = np.atleast_1d(to_replace)
17881788
if not np.can_cast(to_replace_values, bool):
17891789
return self
@@ -1965,7 +1965,7 @@ def replace(self, to_replace, value, inplace=False, filter=None,
19651965
def _replace_single(self, to_replace, value, inplace=False, filter=None,
19661966
regex=False, convert=True, mgr=None):
19671967

1968-
inplace = com.enforce_bool_type(inplace)
1968+
inplace = _enforce_bool_type(inplace)
19691969

19701970
# to_replace is regex compilable
19711971
to_rep_re = regex and is_re_compilable(to_replace)
@@ -3190,7 +3190,7 @@ def replace_list(self, src_list, dest_list, inplace=False, regex=False,
31903190
mgr=None):
31913191
""" do a list replace """
31923192

3193-
inplace = com.enforce_bool_type(inplace)
3193+
inplace = _enforce_bool_type(inplace)
31943194

31953195
if mgr is None:
31963196
mgr = self

pandas/core/series.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2354,7 +2354,11 @@ def align(self, other, join='outer', axis=None, level=None, copy=True,
23542354

23552355
@Appender(generic._shared_docs['rename'] % _shared_doc_kwargs)
23562356
def rename(self, index=None, **kwargs):
2357-
kwargs['inplace'] = _enforce_bool_type(kwargs['inplace'])
2357+
if 'inplace' in kwargs:
2358+
kwargs['inplace'] = _enforce_bool_type(kwargs['inplace'])
2359+
else:
2360+
kwargs['inplace'] = False
2361+
23582362
non_mapping = is_scalar(index) or (is_list_like(index) and
23592363
not is_dict_like(index))
23602364
if non_mapping:

pandas/tests/frame/test_missing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def test_fillna(self):
208208
# empty frame (GH #2778)
209209
df = DataFrame(columns=['x'])
210210
for m in ['pad', 'backfill']:
211-
df.x.fillna(method=m, inplace=1)
211+
df.x.fillna(method=m, inplace=True)
212212
df.x.fillna(method=m)
213213

214214
# with different dtype (GH3386)

0 commit comments

Comments
 (0)