Skip to content

Apply on an empty dataframe exception #2476

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
hayd opened this issue Dec 10, 2012 · 1 comment
Closed

Apply on an empty dataframe exception #2476

hayd opened this issue Dec 10, 2012 · 1 comment
Labels
Milestone

Comments

@hayd
Copy link
Contributor

hayd commented Dec 10, 2012

Apply to a DataFrame using lambda with a index/key (although that is in the DataFrame's index throws an error (a KeyError or IndexError respectively), perhaps it should exit more gracefully?

In [1]: x = pd.DataFrame(index=["a"])

In [2]: x
Out[2]: 
Empty DataFrame
Columns: array([], dtype=object)
Index: array([a], dtype=object)

In [3]: x.apply(lambda x: x["a"], axis=1)
KeyError                                  Traceback (most recent call last)
<ipython-input-3-80540f6c1d18> in <module>()
----> 1 x.apply(lambda x: x["a"], axis=1)

/Library/Python/2.7/site-packages/pandas-0.9.0-py2.7-macosx-10.7-intel.egg/pandas/core/frame.pyc in apply(self, func, axis, broadcast, raw, args, **kwds)
   3750             if not broadcast:
   3751                 if not all(self.shape):
-> 3752                     is_reduction = not isinstance(f(_EMPTY_SERIES),
   3753                                                   np.ndarray)
   3754                     if is_reduction:

<ipython-input-447-80540f6c1d18> in <lambda>(x)
----> 1 x.apply(lambda x: x["a"], axis=1)

/Library/Python/2.7/site-packages/pandas-0.9.0-py2.7-macosx-10.7-intel.egg/pandas/core/series.pyc in __getitem__(self, key)
    462     def __getitem__(self, key):
    463         try:
--> 464             return self.index.get_value(self, key)
    465         except InvalidIndexError:
    466             pass

/Library/Python/2.7/site-packages/pandas-0.9.0-py2.7-macosx-10.7-intel.egg/pandas/core/index.pyc in get_value(self, series, key)
    689                     raise InvalidIndexError(key)
    690                 else:
--> 691                     raise e1
    692             except Exception:  # pragma: no cover
    693                 raise e1

KeyError: 'a'

I am trying to wrangle data from a huge number of Excel sheets (converting each sheet into a DataFrame but need to clean them before they are merged into something more sensible... I wanted to apply functions across these without worrying which are empty...

(same error in 0.9.1.)

@changhiskhan
Copy link
Contributor

Now returns empty DataFrame if passed func fails on empty Series (used to determine whether is reduction)

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