{"action":"created","issue":{"url":"https://api.github.com/repos/pydata/pandas/issues/9752","labels_url":"https://api.github.com/repos/pydata/pandas/issues/9752/labels{/name}","comments_url":"https://api.github.com/repos/pydata/pandas/issues/9752/comments","events_url":"https://api.github.com/repos/pydata/pandas/issues/9752/events","html_url":"https://github.com/pydata/pandas/issues/9752","id":65335324,"number":9752,"title":"BUG: first_valid_index() empty Series and DataFrame inconsistencies","user":{"login":"wholmgren","id":4383303,"avatar_url":"https://avatars.githubusercontent.com/u/4383303?v=3","gravatar_id":"","url":"https://api.github.com/users/wholmgren","html_url":"https://github.com/wholmgren","followers_url":"https://api.github.com/users/wholmgren/followers","following_url":"https://api.github.com/users/wholmgren/following{/other_user}","gists_url":"https://api.github.com/users/wholmgren/gists{/gist_id}","starred_url":"https://api.github.com/users/wholmgren/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wholmgren/subscriptions","organizations_url":"https://api.github.com/users/wholmgren/orgs","repos_url":"https://api.github.com/users/wholmgren/repos","events_url":"https://api.github.com/users/wholmgren/events{/privacy}","received_events_url":"https://api.github.com/users/wholmgren/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/pydata/pandas/labels/API%20Design","name":"API Design","color":"AD7FA8"},{"url":"https://api.github.com/repos/pydata/pandas/labels/Error%20Reporting","name":"Error Reporting","color":"ffa0ff"},{"url":"https://api.github.com/repos/pydata/pandas/labels/Indexing","name":"Indexing","color":"0b02e1"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/pydata/pandas/milestones/29","html_url":"https://github.com/pydata/pandas/milestones/0.16.1","labels_url":"https://api.github.com/repos/pydata/pandas/milestones/29/labels","id":816810,"number":29,"title":"0.16.1","description":"after 0.16.0 of course!","creator":{"login":"jreback","id":953992,"avatar_url":"https://avatars.githubusercontent.com/u/953992?v=3","gravatar_id":"","url":"https://api.github.com/users/jreback","html_url":"https://github.com/jreback","followers_url":"https://api.github.com/users/jreback/followers","following_url":"https://api.github.com/users/jreback/following{/other_user}","gists_url":"https://api.github.com/users/jreback/gists{/gist_id}","starred_url":"https://api.github.com/users/jreback/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jreback/subscriptions","organizations_url":"https://api.github.com/users/jreback/orgs","repos_url":"https://api.github.com/users/jreback/repos","events_url":"https://api.github.com/users/jreback/events{/privacy}","received_events_url":"https://api.github.com/users/jreback/received_events","type":"User","site_admin":false},"open_issues":95,"closed_issues":26,"state":"open","created_at":"2014-10-07T14:39:07Z","updated_at":"2015-04-03T22:42:19Z","due_on":"2015-04-30T07:00:00Z","closed_at":null},"comments":2,"created_at":"2015-03-31T00:35:32Z","updated_at":"2015-04-03T22:56:57Z","closed_at":null,"body":"I came across some inconsistencies in how ``first_valid_index()`` works on empty ``Series`` and ``DataFrames`` (and ``last_valid_index()``).\r\n\r\n```python\r\n>>> print(pd.DataFrame().first_valid_index())\r\nTraceback (most recent call last):\r\n  File \"<stdin>\", line 1, in <module>\r\n  File \"/home/will/.virtualenvs/dev3/lib/python3.4/site-packages/pandas/core/frame.py\", line 3332, in first_valid_index\r\n    return self.index[self.count(1) > 0][0]\r\n  File \"/home/will/.virtualenvs/dev3/lib/python3.4/site-packages/pandas/core/index.py\", line 915, in __getitem__\r\n    return getitem(key)\r\nIndexError: index 0 is out of bounds for axis 0 with size 0\r\n\r\n>>> print(pd.Series().first_valid_index())\r\nNone\r\n\r\n>>> print(pd.SparseDataFrame([]).first_valid_index())\r\nTraceback (most recent call last):\r\n  File \"<stdin>\", line 1, in <module>\r\n  File \"/home/will/.virtualenvs/dev3/lib/python3.4/site-packages/pandas/core/frame.py\", line 3332, in first_valid_index\r\n    return self.index[self.count(1) > 0][0]\r\n  File \"/home/will/.virtualenvs/dev3/lib/python3.4/site-packages/pandas/core/ops.py\", line 919, in f\r\n    res = self._combine_const(other, func, raise_on_error=False)\r\nTypeError: _combine_const() got an unexpected keyword argument 'raise_on_error'\r\n\r\n>>> print(pd.SparseSeries([]).first_valid_index())\r\nNone\r\n```\r\n\r\nIs there a reason for this different behavior? Note also that the ``DataFrame``/``SparseDataFrame`` exceptions are different. Here's the [``DataFrame``](https://github.com/pydata/pandas/blob/671c4b3c52a02ee3736752b2645d08762c0fedaf/pandas/core/frame.py#L3421) and [``Series``](https://github.com/pydata/pandas/blob/a4772028fef7f660cf6d94129a1a9a6f3852e441/pandas/core/series.py#L2375) 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. \r\n\r\nFor 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.)"},"comment":{"url":"https://api.github.com/repos/pydata/pandas/issues/comments/89441527","html_url":"https://github.com/pydata/pandas/issues/9752#issuecomment-89441527","issue_url":"https://api.github.com/repos/pydata/pandas/issues/9752","id":89441527,"user":{"login":"wholmgren","id":4383303,"avatar_url":"https://avatars.githubusercontent.com/u/4383303?v=3","gravatar_id":"","url":"https://api.github.com/users/wholmgren","html_url":"https://github.com/wholmgren","followers_url":"https://api.github.com/users/wholmgren/followers","following_url":"https://api.github.com/users/wholmgren/following{/other_user}","gists_url":"https://api.github.com/users/wholmgren/gists{/gist_id}","starred_url":"https://api.github.com/users/wholmgren/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wholmgren/subscriptions","organizations_url":"https://api.github.com/users/wholmgren/orgs","repos_url":"https://api.github.com/users/wholmgren/repos","events_url":"https://api.github.com/users/wholmgren/events{/privacy}","received_events_url":"https://api.github.com/users/wholmgren/received_events","type":"User","site_admin":false},"created_at":"2015-04-03T22:56:57Z","updated_at":"2015-04-03T22:56:57Z","body":"There is a [test for ``Series``](https://github.com/pydata/pandas/blob/7dfb2791c4a0e6bcceea6b6df9342cc212e454f0/pandas/tests/test_series.py#L6533) that requires it to return ``None`` if empty. ``test_frame`` doesn't have any explicit expectations about this behavior.\r\n\r\nI changed the ``Series.first_valid_index`` behavior to ``if len(self) == 0: raise(IndexError)`` and the only test in ``pandas.tests`` that failed was ``test_series.test_first_last_valid``, which is no surprise since I didn't find any uses of these functions in the test suite other than where they are explicitly tested.\r\n\r\n[``SparseDataFrame._combine_const``](https://github.com/pydata/pandas/blob/899212982af5978a5991dfd9517f3a0212bcdaa8/pandas/sparse/frame.py#L518) does indeed have a different signature than [``DataFrame._combine_const``](https://github.com/pydata/pandas/blob/b2469200b39521c7acd6c17f29aa8e811dfef6da/pandas/core/frame.py#L3200). Not sure how you want to resolve that part of it. "}}