Skip to content
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

apply fails when some data is None #1243

Closed
mirage007 opened this issue May 15, 2012 · 1 comment
Closed

apply fails when some data is None #1243

mirage007 opened this issue May 15, 2012 · 1 comment
Labels
Milestone

Comments

@mirage007
Copy link

In [91]: from pandas import DataFrame

In [92]: a = DataFrame([1,2, None, 3, 4])

In [93]: a
Out[93]:
0
0 1
1 2
2 None
3 3
4 4

In [94]: a.apply(lambda x: str(x[0]), axis=1)
Out[94]:
0 1
1 2
2 None
3 3
4 4

In [95]: a.apply(lambda x: int(x[0]), axis=1)
Out[95]:
0 1
1 2
2 NaN
3 NaN
4 NaN


I was expecting elements 3 and 4 to still evaluate properly in this case to something like this:

0 1
1 2
2 NaN
3 3
4 4

i am using windows xp 32 bit with pandas 0.7.1 on python 2.7.2

@wesm
Copy link
Member

wesm commented May 19, 2012

You spotted a bug that I fixed earlier today. This should have raised an exception:

In [4]: a.apply(lambda x: int(x[0]), axis=1)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/wesm/code/pandas/<ipython-input-4-77655858a2f4> in <module>()
----> 1 a.apply(lambda x: int(x[0]), axis=1)

/home/wesm/code/pandas/pandas/core/frame.pyc in apply(self, func, axis, broadcast, raw, args, **kwds)
   3346                     return self._apply_raw(f, axis)
   3347                 else:
-> 3348                     return self._apply_standard(f, axis)
   3349             else:
   3350                 return self._apply_broadcast(f, axis)

/home/wesm/code/pandas/pandas/core/frame.pyc in _apply_standard(self, func, axis, ignore_failures)
   3401             try:
   3402                 for k, v in series_gen:
-> 3403                     results[k] = func(v)
   3404             except Exception, e:
   3405                 try:

/home/wesm/code/pandas/<ipython-input-4-77655858a2f4> in <lambda>(x)
----> 1 a.apply(lambda x: int(x[0]), axis=1)

TypeError: ("int() argument must be a string or a number, not 'NoneType'", 'occurred at index 2')

I'm unsure about propagating NAs silently. Going to table it for future work

@wesm wesm closed this as completed May 19, 2012
wesm added a commit that referenced this issue May 19, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants