Skip to content

Commit d212666

Browse files
committed
Merge pull request pandas-dev#3212 from wesm/inplace-not-return-self
API: return None when inplace=True. closes pandas-dev#1893
2 parents 7916e76 + 8bdcb38 commit d212666

File tree

4 files changed

+22
-74
lines changed

4 files changed

+22
-74
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ pandas 0.11.0
158158
- util.testing.assert_frame_equal now checks the column and index names (GH2964_)
159159
- Constructors will now return a more informative ValueError on failures
160160
when invalid shapes are passed
161+
- Methods return None when inplace=True (GH1893_)
161162

162163
**Bug Fixes**
163164

@@ -259,6 +260,7 @@ pandas 0.11.0
259260

260261
.. _GH622: https://github.com/pydata/pandas/issues/622
261262
.. _GH797: https://github.com/pydata/pandas/issues/797
263+
.. _GH1893: https://github.com/pydata/pandas/issues/1893
262264
.. _GH1978: https://github.com/pydata/pandas/issues/1978
263265
.. _GH2758: https://github.com/pydata/pandas/issues/2758
264266
.. _GH2121: https://github.com/pydata/pandas/issues/2121

pandas/core/frame.py

+12-44
Original file line numberDiff line numberDiff line change
@@ -2708,16 +2708,9 @@ def set_index(self, keys, drop=True, append=False, inplace=False,
27082708

27092709
frame.index = index
27102710

2711-
if inplace:
2712-
import warnings
2713-
warnings.warn("set_index with inplace=True will return None"
2714-
" from pandas 0.11 onward", FutureWarning)
2715-
return self
2716-
else:
2711+
if not inplace:
27172712
return frame
27182713

2719-
return frame if not inplace else None
2720-
27212714
def reset_index(self, level=None, drop=False, inplace=False, col_level=0,
27222715
col_fill=''):
27232716
"""
@@ -2815,12 +2808,7 @@ def _maybe_cast(values):
28152808
new_obj.insert(0, name, _maybe_cast(values))
28162809

28172810
new_obj.index = new_index
2818-
if inplace:
2819-
import warnings
2820-
warnings.warn("reset_index with inplace=True will return None"
2821-
" from pandas 0.11 onward", FutureWarning)
2822-
return self
2823-
else:
2811+
if not inplace:
28242812
return new_obj
28252813

28262814
delevel = deprecate('delevel', reset_index)
@@ -2988,10 +2976,6 @@ def drop_duplicates(self, cols=None, take_last=False, inplace=False):
29882976
inds, = (-duplicated).nonzero()
29892977
self._data = self._data.take(inds)
29902978
self._clear_item_cache()
2991-
import warnings
2992-
warnings.warn("drop_duplicates with inplace=True will return None"
2993-
" from pandas 0.11 onward", FutureWarning)
2994-
return self
29952979
else:
29962980
return self[-duplicated]
29972981

@@ -3147,10 +3131,6 @@ def sort_index(self, axis=0, by=None, ascending=True, inplace=False):
31473131
self._data = self._data.take(indexer)
31483132

31493133
self._clear_item_cache()
3150-
import warnings
3151-
warnings.warn("sort/sort_index with inplace=True will return None"
3152-
" from pandas 0.11 onward", FutureWarning)
3153-
return self
31543134
else:
31553135
return self.take(indexer, axis=axis, convert=False)
31563136

@@ -3194,10 +3174,6 @@ def sortlevel(self, level=0, axis=0, ascending=True, inplace=False):
31943174
self._data = self._data.take(indexer)
31953175

31963176
self._clear_item_cache()
3197-
import warnings
3198-
warnings.warn("sortlevel with inplace=True will return None"
3199-
" from pandas 0.11 onward", FutureWarning)
3200-
return self
32013177
else:
32023178
return self.take(indexer, axis=axis, convert=False)
32033179

@@ -3328,10 +3304,6 @@ def fillna(self, value=None, method=None, axis=0, inplace=False,
33283304

33293305
if inplace:
33303306
self._data = new_data
3331-
import warnings
3332-
warnings.warn("fillna with inplace=True will return None"
3333-
" from pandas 0.11 onward", FutureWarning)
3334-
return self
33353307
else:
33363308
return self._constructor(new_data)
33373309

@@ -3380,10 +3352,6 @@ def replace(self, to_replace, value=None, method='pad', axis=0,
33803352
self._consolidate_inplace()
33813353

33823354
axis = self._get_axis_number(axis)
3383-
if inplace:
3384-
import warnings
3385-
warnings.warn("replace with inplace=True will return None"
3386-
" from pandas 0.11 onward", FutureWarning)
33873355

33883356
if value is None:
33893357
return self._interpolate(to_replace, method, axis, inplace, limit)
@@ -3397,13 +3365,17 @@ def replace(self, to_replace, value=None, method='pad', axis=0,
33973365
new_data = self._data
33983366
for c, src in to_replace.iteritems():
33993367
if c in value and c in self:
3400-
new_data = new_data.replace(src, value[c], filter = [ c ], inplace=inplace)
3368+
new_data = new_data.replace(src, value[c],
3369+
filter=[ c ],
3370+
inplace=inplace)
34013371

34023372
elif not isinstance(value, (list, np.ndarray)):
34033373
new_data = self._data
34043374
for k, src in to_replace.iteritems():
34053375
if k in self:
3406-
new_data = new_data.replace(src, value, filter = [ k ], inplace=inplace)
3376+
new_data = new_data.replace(src, value,
3377+
filter = [ k ],
3378+
inplace=inplace)
34073379
else:
34083380
raise ValueError('Fill value must be scalar or dict or Series')
34093381

@@ -3430,7 +3402,9 @@ def replace(self, to_replace, value=None, method='pad', axis=0,
34303402
new_data = self._data
34313403
for k, v in value.iteritems():
34323404
if k in self:
3433-
new_data = new_data.replace(to_replace, v, filter = [ k ], inplace=inplace)
3405+
new_data = new_data.replace(to_replace, v,
3406+
filter=[ k ],
3407+
inplace=inplace)
34343408

34353409
elif not isinstance(value, (list, np.ndarray)): # NA -> 0
34363410
new_data = self._data.replace(to_replace, value,
@@ -3442,7 +3416,6 @@ def replace(self, to_replace, value=None, method='pad', axis=0,
34423416

34433417
if inplace:
34443418
self._data = new_data
3445-
return self
34463419
else:
34473420
return self._constructor(new_data)
34483421

@@ -3525,12 +3498,7 @@ def rename(self, index=None, columns=None, copy=True, inplace=False):
35253498
if columns is not None:
35263499
result._rename_columns_inplace(columns_f)
35273500

3528-
if inplace:
3529-
import warnings
3530-
warnings.warn("rename with inplace=True will return None"
3531-
" from pandas 0.11 onward", FutureWarning)
3532-
return self
3533-
else:
3501+
if not inplace:
35343502
return result
35353503

35363504
def _rename_index_inplace(self, mapper):

pandas/core/series.py

+6-28
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def convert_to_array(values):
122122

123123
# 2 datetimes or 2 timedeltas
124124
if (is_timedelta_lhs and is_timedelta_rhs) or (is_datetime_lhs and is_datetime_rhs):
125-
125+
126126
dtype = 'timedelta64[ns]'
127127

128128
# we may have to convert to object unfortunately here
@@ -601,7 +601,7 @@ def _is_mixed_type(self):
601601
def _slice(self, slobj, axis=0, raise_on_error=False):
602602
if raise_on_error:
603603
_check_slice_bounds(slobj, self.values)
604-
604+
605605
return self._constructor(self.values[slobj], index=self.index[slobj])
606606

607607
def __getitem__(self, key):
@@ -1047,11 +1047,6 @@ def reset_index(self, level=None, drop=False, name=None, inplace=False):
10471047
self.index = new_index
10481048
# set name if it was passed, otherwise, keep the previous name
10491049
self.name = name or self.name
1050-
import warnings
1051-
warnings.warn("Series.reset_index with inplace=True will "
1052-
"return None from pandas 0.11 onward",
1053-
FutureWarning)
1054-
return self
10551050
else:
10561051
return Series(self.values.copy(), index=new_index,
10571052
name=self.name)
@@ -2615,13 +2610,8 @@ def fillna(self, value=None, method=None, inplace=False,
26152610
-------
26162611
filled : Series
26172612
"""
2618-
if inplace:
2619-
import warnings
2620-
warnings.warn("Series.fillna with inplace=True will return None"
2621-
" from pandas 0.11 onward", FutureWarning)
2622-
26232613
if not self._can_hold_na:
2624-
return self.copy() if not inplace else self
2614+
return self.copy() if not inplace else None
26252615

26262616
if value is not None:
26272617
if method is not None:
@@ -2647,9 +2637,7 @@ def fillna(self, value=None, method=None, inplace=False,
26472637
else:
26482638
result = Series(values, index=self.index, name=self.name)
26492639

2650-
if inplace:
2651-
return self
2652-
else:
2640+
if not inplace:
26532641
return result
26542642

26552643
def ffill(self, inplace=False, limit=None):
@@ -2756,12 +2744,7 @@ def _rep_dict(rs, to_rep): # replace {[src] -> dest}
27562744
raise ValueError('Unrecognized to_replace type %s' %
27572745
type(to_replace))
27582746

2759-
if inplace:
2760-
import warnings
2761-
warnings.warn("Series.replace with inplace=True will return None"
2762-
" from pandas 0.11 onward", FutureWarning)
2763-
return self
2764-
else:
2747+
if not inplace:
27652748
return result
27662749

27672750
def isin(self, values):
@@ -3110,12 +3093,7 @@ def rename(self, mapper, inplace=False):
31103093
result = self if inplace else self.copy()
31113094
result.index = Index([mapper_f(x) for x in self.index], name=self.index.name)
31123095

3113-
if inplace:
3114-
import warnings
3115-
warnings.warn("Series.rename with inplace=True will return None"
3116-
" from pandas 0.11 onward", FutureWarning)
3117-
return self
3118-
else:
3096+
if not inplace:
31193097
return result
31203098

31213099
@property

pandas/tests/test_frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9476,15 +9476,15 @@ def test_strange_column_corruption_issue(self):
94769476
self.assertTrue(first == second == 0)
94779477

94789478
def test_inplace_return_self(self):
9479-
# re #1893, TODO: remove in 0.11
9479+
# re #1893
94809480

94819481
data = DataFrame({'a': ['foo', 'bar', 'baz', 'qux'],
94829482
'b': [0, 0, 1, 1],
94839483
'c': [1, 2, 3, 4]})
94849484

94859485
def _check_f(base, f):
94869486
result = f(base)
9487-
self.assertTrue(result is base)
9487+
self.assertTrue(result is None)
94889488

94899489
# -----DataFrame-----
94909490

0 commit comments

Comments
 (0)