-
-
Couldn't load subscription status.
- Fork 19.2k
Description
I came across some inconsistencies in how first_valid_index() works on empty Series and DataFrames (and last_valid_index()).
>>> print(pd.DataFrame().first_valid_index())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/will/.virtualenvs/dev3/lib/python3.4/site-packages/pandas/core/frame.py", line 3332, in first_valid_index
return self.index[self.count(1) > 0][0]
File "/home/will/.virtualenvs/dev3/lib/python3.4/site-packages/pandas/core/index.py", line 915, in __getitem__
return getitem(key)
IndexError: index 0 is out of bounds for axis 0 with size 0
>>> print(pd.Series().first_valid_index())
None
>>> print(pd.SparseDataFrame([]).first_valid_index())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/will/.virtualenvs/dev3/lib/python3.4/site-packages/pandas/core/frame.py", line 3332, in first_valid_index
return self.index[self.count(1) > 0][0]
File "/home/will/.virtualenvs/dev3/lib/python3.4/site-packages/pandas/core/ops.py", line 919, in f
res = self._combine_const(other, func, raise_on_error=False)
TypeError: _combine_const() got an unexpected keyword argument 'raise_on_error'
>>> print(pd.SparseSeries([]).first_valid_index())
NoneIs there a reason for this different behavior? Note also that the DataFrame/SparseDataFrame exceptions are different. Here's the DataFrame and Series methods. The Series method has a if len(self) == 0 check. I can make a PR if you think one of these should be changed and tell me which way you want to go.
For what it's worth, I came across this issue trying to use this function in a list comprehension to get a list of minimum times from a handful of Series, DataFrames, and SparseDataFrames that may or may not be empty. (Although it just occurred to me that I could, of course, put an if empty check in my list comprehension.)