Skip to content

Commit 36043e8

Browse files
committed
REF: Series inplace methods return None. #1893
1 parent 7e53266 commit 36043e8

File tree

4 files changed

+50
-32
lines changed

4 files changed

+50
-32
lines changed

pandas/core/series.py

+18-20
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ def where(self, cond, other=nan, inplace=False):
594594
ser = self if inplace else self.copy()
595595
if not isinstance(other, (list, tuple, np.ndarray)):
596596
ser._set_with(~cond, other)
597-
return ser
597+
return None if inplace else ser
598598

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

604604
np.putmask(ser, ~cond, other)
605605

606-
return ser
606+
return None if inplace else ser
607607

608608
def mask(self, cond):
609609
"""
@@ -903,7 +903,7 @@ def reset_index(self, level=None, drop=False, name=None, inplace=False):
903903
self.index = new_index
904904
# set name if it was passed, otherwise, keep the previous name
905905
self.name = name or self.name
906-
return self
906+
return
907907
else:
908908
return Series(self.values.copy(), index=new_index,
909909
name=self.name)
@@ -2246,7 +2246,7 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
22462246
return Series(mapped, index=self.index, name=self.name)
22472247

22482248
def align(self, other, join='outer', level=None, copy=True,
2249-
fill_value=None, method=None, inplace=False, limit=None):
2249+
fill_value=None, method=None, limit=None):
22502250
"""
22512251
Align two Series object with the specified join method
22522252
@@ -2419,7 +2419,7 @@ def fillna(self, value=None, method=None, inplace=False,
24192419
filled : Series
24202420
"""
24212421
if not self._can_hold_na:
2422-
return self.copy() if not inplace else self
2422+
return self.copy() if not inplace else None
24232423

24242424
if value is not None:
24252425
if method is not None:
@@ -2445,7 +2445,7 @@ def fillna(self, value=None, method=None, inplace=False,
24452445
else:
24462446
result = Series(values, index=self.index, name=self.name)
24472447

2448-
return result
2448+
return result if not inplace else None
24492449

24502450
def ffill(self, inplace=False, limit=None):
24512451
return self.fillna(method='ffill', inplace=inplace, limit=limit)
@@ -2492,7 +2492,6 @@ def replace(self, to_replace, value=None, method='pad', inplace=False,
24922492
def _rep_one(s, to_rep, v): # replace single value
24932493
mask = com.mask_missing(s.values, to_rep)
24942494
np.putmask(s.values, mask, v)
2495-
return s
24962495

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

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

25182516
if np.isscalar(to_replace):
25192517
to_replace = [to_replace]
25202518

25212519
if isinstance(to_replace, dict):
2522-
return _rep_dict(result, to_replace)
2523-
2524-
if isinstance(to_replace, (list, np.ndarray)):
2520+
_rep_dict(result, to_replace)
2521+
elif isinstance(to_replace, (list, np.ndarray)):
25252522

25262523
if isinstance(value, (list, np.ndarray)): # check same length
25272524
vl, rl = len(value), len(to_replace)
25282525
if vl == rl:
2529-
return _rep_dict(result, dict(zip(to_replace, value)))
2530-
raise ValueError('Got %d to replace but %d values' % (rl, vl))
2526+
_rep_dict(result, dict(zip(to_replace, value)))
2527+
else:
2528+
raise ValueError('Got %d to replace but %d values'
2529+
% (rl, vl))
25312530

25322531
elif value is not None: # otherwise all replaced with same value
2533-
2534-
return _rep_one(result, to_replace, value)
2535-
2532+
_rep_one(result, to_replace, value)
25362533
else: # method
25372534
if method is None: # pragma: no cover
25382535
raise ValueError('must specify a fill method')
@@ -2544,10 +2541,11 @@ def _rep_dict(rs, to_rep): # replace {[src] -> dest}
25442541
if not inplace:
25452542
result = Series(result.values, index=self.index,
25462543
name=self.name)
2547-
return result
2544+
else:
2545+
raise ValueError('Unrecognized to_replace type %s' %
2546+
type(to_replace))
25482547

2549-
raise ValueError('Unrecognized to_replace type %s' %
2550-
type(to_replace))
2548+
return result if not inplace else None
25512549

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

2898-
return result
2896+
return result if not inplace else None
28992897

29002898
@property
29012899
def weekday(self):

pandas/src/parser/tokenizer.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/*
22
3-
Copyright (c) 2012, Lambda Foundry, Inc.
3+
Copyright (c) 2012, Lambda Foundry, Inc., except where noted
4+
5+
Incorporates components of WarrenWeckesser/textreader, licensed under 3-clause
6+
BSD
47
58
See LICENSE for the license
69

pandas/src/parser/tokenizer.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/*
22
3-
Copyright (c) 2012, Lambda Foundry, Inc.
3+
Copyright (c) 2012, Lambda Foundry, Inc., except where noted
4+
5+
Incorporates components of WarrenWeckesser/textreader, licensed under 3-clause
6+
BSD
47
58
See LICENSE for the license
69

pandas/tests/test_series.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,10 @@ def test_where_inplace(self):
974974
cond = s > 0
975975

976976
rs = s.copy()
977-
rs.where(cond, inplace=True)
977+
978+
res = rs.where(cond, inplace=True)
979+
self.assertTrue(res is None)
980+
978981
assert_series_equal(rs.dropna(), s[cond])
979982
assert_series_equal(rs, s.where(cond))
980983

@@ -2883,7 +2886,9 @@ def test_rename(self):
28832886
def test_rename_inplace(self):
28842887
renamer = lambda x: x.strftime('%Y%m%d')
28852888
expected = renamer(self.ts.index[0])
2886-
self.ts.rename(renamer, inplace=True)
2889+
res = self.ts.rename(renamer, inplace=True)
2890+
2891+
self.assertTrue(res is None)
28872892
self.assertEqual(self.ts.index[0], expected)
28882893

28892894
def test_preserveRefs(self):
@@ -2900,7 +2905,10 @@ def test_ne(self):
29002905
def test_pad_nan(self):
29012906
x = TimeSeries([np.nan, 1., np.nan, 3., np.nan],
29022907
['z', 'a', 'b', 'c', 'd'], dtype=float)
2903-
x.fillna(method='pad', inplace=True)
2908+
2909+
res = x.fillna(method='pad', inplace=True)
2910+
self.assertTrue(res is None)
2911+
29042912
expected = TimeSeries([np.nan, 1.0, 1.0, 3.0, 3.0],
29052913
['z', 'a', 'b', 'c', 'd'], dtype=float)
29062914
assert_series_equal(x[1:], expected[1:])
@@ -2950,7 +2958,7 @@ def test_isin(self):
29502958

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

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

2989-
y2 = y.fillna(value=0, inplace=True)
2990-
self.assert_(y is y2)
2997+
res = y.fillna(value=0, inplace=True)
2998+
self.assert_(res is None)
29912999

29923000
expected = x.fillna(value=0)
2993-
assert_series_equal(y2, expected)
3001+
assert_series_equal(y, expected)
29943002

29953003
def test_fillna_invalid_method(self):
29963004
try:
@@ -3016,8 +3024,10 @@ def test_replace(self):
30163024

30173025
# replace list with a single value
30183026
rs = ser.replace([np.nan], -1, inplace=True)
3027+
self.assertTrue(rs is None)
3028+
30193029
exp = ser.fillna(-1)
3020-
assert_series_equal(rs, exp)
3030+
assert_series_equal(ser, exp)
30213031

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

30623072
# replace inplace
3063-
ser.replace([np.nan, 'foo', 'bar'], -1, inplace=True)
3073+
res = ser.replace([np.nan, 'foo', 'bar'], -1, inplace=True)
3074+
3075+
self.assertTrue(res is None)
3076+
30643077
self.assert_((ser[:5] == -1).all())
30653078
self.assert_((ser[6:10] == -1).all())
30663079
self.assert_((ser[20:30] == -1).all())
@@ -3314,7 +3327,8 @@ def test_reset_index(self):
33143327
#check inplace
33153328
s = ser.reset_index(drop=True)
33163329
s2 = ser
3317-
s2.reset_index(drop=True, inplace=True)
3330+
res = s2.reset_index(drop=True, inplace=True)
3331+
self.assertTrue(res is None)
33183332
assert_series_equal(s, s2)
33193333

33203334
#level

0 commit comments

Comments
 (0)