Skip to content

Commit

Permalink
REF: Series inplace methods return None. #1893
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Dec 8, 2012
1 parent 7e53266 commit 36043e8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 32 deletions.
38 changes: 18 additions & 20 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ def where(self, cond, other=nan, inplace=False):
ser = self if inplace else self.copy()
if not isinstance(other, (list, tuple, np.ndarray)):
ser._set_with(~cond, other)
return ser
return None if inplace else ser

if isinstance(other, Series):
other = other.reindex(ser.index)
Expand All @@ -603,7 +603,7 @@ def where(self, cond, other=nan, inplace=False):

np.putmask(ser, ~cond, other)

return ser
return None if inplace else ser

def mask(self, cond):
"""
Expand Down Expand Up @@ -903,7 +903,7 @@ 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
return self
return
else:
return Series(self.values.copy(), index=new_index,
name=self.name)
Expand Down Expand Up @@ -2246,7 +2246,7 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
return Series(mapped, index=self.index, name=self.name)

def align(self, other, join='outer', level=None, copy=True,
fill_value=None, method=None, inplace=False, limit=None):
fill_value=None, method=None, limit=None):
"""
Align two Series object with the specified join method
Expand Down Expand Up @@ -2419,7 +2419,7 @@ def fillna(self, value=None, method=None, inplace=False,
filled : Series
"""
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 @@ -2445,7 +2445,7 @@ def fillna(self, value=None, method=None, inplace=False,
else:
result = Series(values, index=self.index, name=self.name)

return result
return result if not inplace else None

def ffill(self, inplace=False, limit=None):
return self.fillna(method='ffill', inplace=inplace, limit=limit)
Expand Down Expand Up @@ -2492,7 +2492,6 @@ def replace(self, to_replace, value=None, method='pad', inplace=False,
def _rep_one(s, to_rep, v): # replace single value
mask = com.mask_missing(s.values, to_rep)
np.putmask(s.values, mask, v)
return s

def _rep_dict(rs, to_rep): # replace {[src] -> dest}

Expand All @@ -2513,26 +2512,24 @@ def _rep_dict(rs, to_rep): # replace {[src] -> dest}
else: # if no risk of clobbering then simple
for d, sset in dd.iteritems():
_rep_one(rs, sset, d)
return rs

if np.isscalar(to_replace):
to_replace = [to_replace]

if isinstance(to_replace, dict):
return _rep_dict(result, to_replace)

if isinstance(to_replace, (list, np.ndarray)):
_rep_dict(result, to_replace)
elif isinstance(to_replace, (list, np.ndarray)):

if isinstance(value, (list, np.ndarray)): # check same length
vl, rl = len(value), len(to_replace)
if vl == rl:
return _rep_dict(result, dict(zip(to_replace, value)))
raise ValueError('Got %d to replace but %d values' % (rl, vl))
_rep_dict(result, dict(zip(to_replace, value)))
else:
raise ValueError('Got %d to replace but %d values'
% (rl, vl))

elif value is not None: # otherwise all replaced with same value

return _rep_one(result, to_replace, value)

_rep_one(result, to_replace, value)
else: # method
if method is None: # pragma: no cover
raise ValueError('must specify a fill method')
Expand All @@ -2544,10 +2541,11 @@ def _rep_dict(rs, to_rep): # replace {[src] -> dest}
if not inplace:
result = Series(result.values, index=self.index,
name=self.name)
return result
else:
raise ValueError('Unrecognized to_replace type %s' %
type(to_replace))

raise ValueError('Unrecognized to_replace type %s' %
type(to_replace))
return result if not inplace else None

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

return result
return result if not inplace else None

@property
def weekday(self):
Expand Down
5 changes: 4 additions & 1 deletion pandas/src/parser/tokenizer.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/*
Copyright (c) 2012, Lambda Foundry, Inc.
Copyright (c) 2012, Lambda Foundry, Inc., except where noted
Incorporates components of WarrenWeckesser/textreader, licensed under 3-clause
BSD
See LICENSE for the license
Expand Down
5 changes: 4 additions & 1 deletion pandas/src/parser/tokenizer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/*
Copyright (c) 2012, Lambda Foundry, Inc.
Copyright (c) 2012, Lambda Foundry, Inc., except where noted
Incorporates components of WarrenWeckesser/textreader, licensed under 3-clause
BSD
See LICENSE for the license
Expand Down
34 changes: 24 additions & 10 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,10 @@ def test_where_inplace(self):
cond = s > 0

rs = s.copy()
rs.where(cond, inplace=True)

res = rs.where(cond, inplace=True)
self.assertTrue(res is None)

assert_series_equal(rs.dropna(), s[cond])
assert_series_equal(rs, s.where(cond))

Expand Down Expand Up @@ -2883,7 +2886,9 @@ def test_rename(self):
def test_rename_inplace(self):
renamer = lambda x: x.strftime('%Y%m%d')
expected = renamer(self.ts.index[0])
self.ts.rename(renamer, inplace=True)
res = self.ts.rename(renamer, inplace=True)

self.assertTrue(res is None)
self.assertEqual(self.ts.index[0], expected)

def test_preserveRefs(self):
Expand All @@ -2900,7 +2905,10 @@ def test_ne(self):
def test_pad_nan(self):
x = TimeSeries([np.nan, 1., np.nan, 3., np.nan],
['z', 'a', 'b', 'c', 'd'], dtype=float)
x.fillna(method='pad', inplace=True)

res = x.fillna(method='pad', inplace=True)
self.assertTrue(res is None)

expected = TimeSeries([np.nan, 1.0, 1.0, 3.0, 3.0],
['z', 'a', 'b', 'c', 'd'], dtype=float)
assert_series_equal(x[1:], expected[1:])
Expand Down Expand Up @@ -2950,7 +2958,7 @@ def test_isin(self):

def test_fillna_int(self):
s = Series(np.random.randint(-100, 100, 50))
self.assert_(s.fillna(method='ffill', inplace=True) is s)
self.assert_(s.fillna(method='ffill', inplace=True) is None)
assert_series_equal(s.fillna(method='ffill', inplace=False), s)

#-------------------------------------------------------------------------------
Expand Down Expand Up @@ -2986,11 +2994,11 @@ def test_fillna_inplace(self):
x = Series([nan, 1., nan, 3., nan],['z','a','b','c','d'])
y = x.copy()

y2 = y.fillna(value=0, inplace=True)
self.assert_(y is y2)
res = y.fillna(value=0, inplace=True)
self.assert_(res is None)

expected = x.fillna(value=0)
assert_series_equal(y2, expected)
assert_series_equal(y, expected)

def test_fillna_invalid_method(self):
try:
Expand All @@ -3016,8 +3024,10 @@ def test_replace(self):

# replace list with a single value
rs = ser.replace([np.nan], -1, inplace=True)
self.assertTrue(rs is None)

exp = ser.fillna(-1)
assert_series_equal(rs, exp)
assert_series_equal(ser, exp)

rs = ser.replace(0., np.nan)
ser[ser == 0.] = np.nan
Expand Down Expand Up @@ -3060,7 +3070,10 @@ def test_replace(self):
assert_almost_equal(rs4[4], ser[5])

# replace inplace
ser.replace([np.nan, 'foo', 'bar'], -1, inplace=True)
res = ser.replace([np.nan, 'foo', 'bar'], -1, inplace=True)

self.assertTrue(res is None)

self.assert_((ser[:5] == -1).all())
self.assert_((ser[6:10] == -1).all())
self.assert_((ser[20:30] == -1).all())
Expand Down Expand Up @@ -3314,7 +3327,8 @@ def test_reset_index(self):
#check inplace
s = ser.reset_index(drop=True)
s2 = ser
s2.reset_index(drop=True, inplace=True)
res = s2.reset_index(drop=True, inplace=True)
self.assertTrue(res is None)
assert_series_equal(s, s2)

#level
Expand Down

0 comments on commit 36043e8

Please sign in to comment.