Skip to content

API: return None when inplace=True. re #1893 #3212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ pandas 0.11.0
- util.testing.assert_frame_equal now checks the column and index names (GH2964_)
- Constructors will now return a more informative ValueError on failures
when invalid shapes are passed
- Methods return None when inplace=True (GH1893_)

**Bug Fixes**

Expand Down Expand Up @@ -251,6 +252,7 @@ pandas 0.11.0

.. _GH622: https://github.com/pydata/pandas/issues/622
.. _GH797: https://github.com/pydata/pandas/issues/797
.. _GH1893: https://github.com/pydata/pandas/issues/1893
.. _GH1978: https://github.com/pydata/pandas/issues/1978
.. _GH2758: https://github.com/pydata/pandas/issues/2758
.. _GH2121: https://github.com/pydata/pandas/issues/2121
Expand Down
56 changes: 12 additions & 44 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2708,16 +2708,9 @@ def set_index(self, keys, drop=True, append=False, inplace=False,

frame.index = index

if inplace:
import warnings
warnings.warn("set_index with inplace=True will return None"
" from pandas 0.11 onward", FutureWarning)
return self
else:
if not inplace:
return frame

return frame if not inplace else None

def reset_index(self, level=None, drop=False, inplace=False, col_level=0,
col_fill=''):
"""
Expand Down Expand Up @@ -2815,12 +2808,7 @@ def _maybe_cast(values):
new_obj.insert(0, name, _maybe_cast(values))

new_obj.index = new_index
if inplace:
import warnings
warnings.warn("reset_index with inplace=True will return None"
" from pandas 0.11 onward", FutureWarning)
return self
else:
if not inplace:
return new_obj

delevel = deprecate('delevel', reset_index)
Expand Down Expand Up @@ -2988,10 +2976,6 @@ def drop_duplicates(self, cols=None, take_last=False, inplace=False):
inds, = (-duplicated).nonzero()
self._data = self._data.take(inds)
self._clear_item_cache()
import warnings
warnings.warn("drop_duplicates with inplace=True will return None"
" from pandas 0.11 onward", FutureWarning)
return self
else:
return self[-duplicated]

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

self._clear_item_cache()
import warnings
warnings.warn("sort/sort_index with inplace=True will return None"
" from pandas 0.11 onward", FutureWarning)
return self
else:
return self.take(indexer, axis=axis, convert=False)

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

self._clear_item_cache()
import warnings
warnings.warn("sortlevel with inplace=True will return None"
" from pandas 0.11 onward", FutureWarning)
return self
else:
return self.take(indexer, axis=axis, convert=False)

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

if inplace:
self._data = new_data
import warnings
warnings.warn("fillna with inplace=True will return None"
" from pandas 0.11 onward", FutureWarning)
return self
else:
return self._constructor(new_data)

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

axis = self._get_axis_number(axis)
if inplace:
import warnings
warnings.warn("replace with inplace=True will return None"
" from pandas 0.11 onward", FutureWarning)

if value is None:
return self._interpolate(to_replace, method, axis, inplace, limit)
Expand All @@ -3397,13 +3365,17 @@ def replace(self, to_replace, value=None, method='pad', axis=0,
new_data = self._data
for c, src in to_replace.iteritems():
if c in value and c in self:
new_data = new_data.replace(src, value[c], filter = [ c ], inplace=inplace)
new_data = new_data.replace(src, value[c],
filter=[ c ],
inplace=inplace)

elif not isinstance(value, (list, np.ndarray)):
new_data = self._data
for k, src in to_replace.iteritems():
if k in self:
new_data = new_data.replace(src, value, filter = [ k ], inplace=inplace)
new_data = new_data.replace(src, value,
filter = [ k ],
inplace=inplace)
else:
raise ValueError('Fill value must be scalar or dict or Series')

Expand All @@ -3430,7 +3402,9 @@ def replace(self, to_replace, value=None, method='pad', axis=0,
new_data = self._data
for k, v in value.iteritems():
if k in self:
new_data = new_data.replace(to_replace, v, filter = [ k ], inplace=inplace)
new_data = new_data.replace(to_replace, v,
filter=[ k ],
inplace=inplace)

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

if inplace:
self._data = new_data
return self
else:
return self._constructor(new_data)

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

if inplace:
import warnings
warnings.warn("rename with inplace=True will return None"
" from pandas 0.11 onward", FutureWarning)
return self
else:
if not inplace:
return result

def _rename_index_inplace(self, mapper):
Expand Down
34 changes: 6 additions & 28 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def convert_to_array(values):

# 2 datetimes or 2 timedeltas
if (is_timedelta_lhs and is_timedelta_rhs) or (is_datetime_lhs and is_datetime_rhs):

dtype = 'timedelta64[ns]'

# we may have to convert to object unfortunately here
Expand Down Expand Up @@ -601,7 +601,7 @@ def _is_mixed_type(self):
def _slice(self, slobj, axis=0, raise_on_error=False):
if raise_on_error:
_check_slice_bounds(slobj, self.values)

return self._constructor(self.values[slobj], index=self.index[slobj])

def __getitem__(self, key):
Expand Down Expand Up @@ -1047,11 +1047,6 @@ def reset_index(self, level=None, drop=False, name=None, inplace=False):
self.index = new_index
# set name if it was passed, otherwise, keep the previous name
self.name = name or self.name
import warnings
warnings.warn("Series.reset_index with inplace=True will "
"return None from pandas 0.11 onward",
FutureWarning)
return self
else:
return Series(self.values.copy(), index=new_index,
name=self.name)
Expand Down Expand Up @@ -2615,13 +2610,8 @@ def fillna(self, value=None, method=None, inplace=False,
-------
filled : Series
"""
if inplace:
import warnings
warnings.warn("Series.fillna with inplace=True will return None"
" from pandas 0.11 onward", FutureWarning)

if not self._can_hold_na:
return self.copy() if not inplace else self
return self.copy() if not inplace else None

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

if inplace:
return self
else:
if not inplace:
return result

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

if inplace:
import warnings
warnings.warn("Series.replace with inplace=True will return None"
" from pandas 0.11 onward", FutureWarning)
return self
else:
if not inplace:
return result

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

if inplace:
import warnings
warnings.warn("Series.rename with inplace=True will return None"
" from pandas 0.11 onward", FutureWarning)
return self
else:
if not inplace:
return result

@property
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -9427,15 +9427,15 @@ def test_strange_column_corruption_issue(self):
self.assertTrue(first == second == 0)

def test_inplace_return_self(self):
# re #1893, TODO: remove in 0.11
# re #1893

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

def _check_f(base, f):
result = f(base)
self.assertTrue(result is base)
self.assertTrue(result is None)

# -----DataFrame-----

Expand Down