Skip to content

Apply fails on empty Series #2874

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

Closed
alansaul opened this issue Feb 14, 2013 · 2 comments
Closed

Apply fails on empty Series #2874

alansaul opened this issue Feb 14, 2013 · 2 comments
Labels
Milestone

Comments

@alansaul
Copy link

I would expect the sum of [] to be giving me 0 as numpy would:

In [18]: import numpy as np

In [19]: np.sum([])
Out[19]: 0.0

but it appears apply cannot handle a lambda with a sum within if its logical selection evaluates to a empty series.

In [15]: a = pd.Series([])

In [16]: a.apply(lambda x: sum(a == x))
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-16-a2ddbb49d4e4> in <module>()
----> 1 a.apply(lambda x: sum(a == x))

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-    packages/pandas/core/series.pyc in apply(self, func, convert_dtype, args, **kwds)
   2295 
   2296         mapped = lib.map_infer(values, f, convert=convert_dtype)
-> 2297         if isinstance(mapped[0], Series):
   2298             from pandas.core.frame import DataFrame
   2299             return DataFrame(mapped.tolist(), index=self.index)

IndexError: index 0 is out of bounds for axis 0 with size 0
@hayd
Copy link
Contributor

hayd commented Mar 4, 2013

This returns Series([], dtype: float64) on the latest pandas, which version of pandas are you using?

I think @changhiskhan fixed this in #2476 (0.10) ? :)

@ghost
Copy link

ghost commented Mar 20, 2013

if the apply func argument is not a ufunc, apply acts like map with a lambda - the result is a series of values,
for an empty series you get an empty series of results.

The exception you show has been fixed.

closing as can't repro.

@ghost ghost closed this as completed Mar 20, 2013
This issue was closed.
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