Skip to content

Commit 20b5218

Browse files
committed
ENH: add convert_dtype option to Series.apply to be able to leave result as dtype=object, close #1414
1 parent 37bb53e commit 20b5218

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

RELEASE.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ pandas 0.8.0
121121
- Exclude "nuisance" columns automatically in GroupBy.transform (#1364)
122122
- Support functions-as-strings in GroupBy.transform (#1362)
123123
- Use index name as xlabel/ylabel in plots (#1415)
124+
- Add ``convert_dtype`` option to Series.apply to be able to leave data as
125+
dtype=object (#1414)
124126

125127
**API Changes**
126128

pandas/core/series.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,14 +1987,17 @@ def map(self, arg):
19871987
mapped = lib.map_infer(self.values, arg)
19881988
return Series(mapped, index=self.index, name=self.name)
19891989

1990-
def apply(self, func):
1990+
def apply(self, func, convert_dtype=True):
19911991
"""
19921992
Invoke function on values of Series. Can be ufunc or Python function
19931993
expecting only single values
19941994
19951995
Parameters
19961996
----------
19971997
func : function
1998+
convert_dtype : boolean, default True
1999+
Try to find better dtype for elementwise function results. If
2000+
False, leave as dtype=object
19982001
19992002
See also
20002003
--------
@@ -2012,7 +2015,7 @@ def apply(self, func):
20122015
raise ValueError('Must yield array')
20132016
return result
20142017
except Exception:
2015-
mapped = lib.map_infer(self.values, func)
2018+
mapped = lib.map_infer(self.values, func, convert=convert_dtype)
20162019
return Series(mapped, index=self.index, name=self.name)
20172020

20182021
def align(self, other, join='outer', level=None, copy=True,

pandas/src/inference.pyx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ def maybe_convert_bool(ndarray[object] arr):
617617
return result.view(np.bool_)
618618

619619

620-
def map_infer(ndarray arr, object f):
620+
def map_infer(ndarray arr, object f, bint convert=1):
621621
'''
622622
Substitute for np.vectorize with pandas-friendly dtype inference
623623
@@ -647,8 +647,11 @@ def map_infer(ndarray arr, object f):
647647

648648
result[i] = val
649649

650-
return maybe_convert_objects(result, try_float=0,
651-
convert_datetime=0)
650+
if convert:
651+
return maybe_convert_objects(result, try_float=0,
652+
convert_datetime=0)
653+
654+
return result
652655

653656
def to_object_array(list rows):
654657
cdef:

pandas/tests/test_series.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,6 +2219,13 @@ def test_apply_same_length_inference_bug(self):
22192219
expected = s.map(f)
22202220
assert_series_equal(result, expected)
22212221

2222+
def test_apply_dont_convert_dtype(self):
2223+
s = Series(np.random.randn(10))
2224+
2225+
f = lambda x: x if x > 0 else np.nan
2226+
result = s.apply(f, convert_dtype=False)
2227+
self.assert_(result.dtype == object)
2228+
22222229
def test_align(self):
22232230
def _check_align(a, b, how='left', fill=None):
22242231
aa, ab = a.align(b, join=how, fill_value=fill)

0 commit comments

Comments
 (0)