Skip to content

BUG 12800 fixed inconsistent behavior of last_valid_index and first_valid_index #12834

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.18.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,4 @@ Bug Fixes
- Bug in ``.describe()`` resets categorical columns information (:issue:`11558`)
- Bug where ``loffset`` argument was not applied when calling ``resample().count()`` on a timeseries (:issue:`12725`)
- ``pd.read_excel()`` now accepts path objects (e.g. ``pathlib.Path``, ``py.path.local``) for the file path, in line with other ``read_*`` functions (:issue:`12655`)
- Bug in ``DataFrame`` inconsistent behavior ``last_valid_index()``, ``first_valid_index`` (:issue:`12800`)
6 changes: 6 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3766,12 +3766,18 @@ def first_valid_index(self):
"""
Return label for first non-NA/null value
"""
if len(self) == 0:
return None

return self.index[self.count(1) > 0][0]

def last_valid_index(self):
"""
Return label for last non-NA/null value
"""
if len(self) == 0:
return None

return self.index[self.count(1) > 0][-1]

# ----------------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/frame/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,9 @@ def test_first_last_valid(self):

index = frame.last_valid_index()
self.assertEqual(index, frame.index[-6])

# GH #12800
def test_empty_first_last(self):
empty_frame = DataFrame()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue number as a comment (and below)

self.assertIsNone(empty_frame.last_valid_index())
self.assertIsNone(empty_frame.first_valid_index())
12 changes: 11 additions & 1 deletion pandas/tests/series/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@


class TestSeriesTimeSeries(TestData, tm.TestCase):

_multiprocess_can_split_ = True

def test_shift(self):
Expand Down Expand Up @@ -222,6 +221,7 @@ def test_asof(self):

def test_getitem_setitem_datetimeindex(self):
from pandas import date_range

N = 50
# testing with timezone, GH #2785
rng = date_range('1/1/1990', periods=N, freq='H', tz='US/Eastern')
Expand Down Expand Up @@ -304,6 +304,7 @@ def test_getitem_setitem_datetime_tz_pytz(self):
from pytz import timezone as tz

from pandas import date_range

N = 50
# testing with timezone, GH #2785
rng = date_range('1/1/1990', periods=N, freq='H', tz='US/Eastern')
Expand Down Expand Up @@ -343,6 +344,7 @@ def test_getitem_setitem_datetime_tz_dateutil(self):
x) # handle special case for utc in dateutil

from pandas import date_range

N = 50
# testing with timezone, GH #2785
rng = date_range('1/1/1990', periods=N, freq='H', tz='US/Eastern')
Expand Down Expand Up @@ -372,6 +374,7 @@ def test_getitem_setitem_datetime_tz_dateutil(self):

def test_getitem_setitem_periodindex(self):
from pandas import period_range

N = 50
rng = period_range('1/1/1990', periods=N, freq='H')
ts = Series(np.random.randn(N), index=rng)
Expand Down Expand Up @@ -460,6 +463,7 @@ def test_asof_periodindex(self):

def test_asof_more(self):
from pandas import date_range

s = Series([nan, nan, 1, 2, nan, nan, 3, 4, 5],
index=date_range('1/1/2000', periods=9))

Expand Down Expand Up @@ -617,3 +621,9 @@ def test_timeseries_coercion(self):
self.assertTrue(ser.is_time_series)
self.assertTrue(ser.index.is_all_dates)
self.assertIsInstance(ser.index, DatetimeIndex)

# GH #12800
def test_empty_first_last(self):
empty_frame = Series()
self.assertIsNone(empty_frame.last_valid_index())
self.assertIsNone(empty_frame.first_valid_index())