Skip to content

Commit

Permalink
Added tests for non-default indexes, scalar and multiple inputs, and …
Browse files Browse the repository at this point in the history
…results preserve columns
  • Loading branch information
Carlos Souza committed Mar 23, 2017
1 parent 6b745af commit 70c958f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -930,3 +930,5 @@ Bug Fixes
- Bug in ``pd.melt()`` where passing a tuple value for ``value_vars`` caused a ``TypeError`` (:issue:`15348`)
- Bug in ``.eval()`` which caused multiline evals to fail with local variables not on the first line (:issue:`15342`)
- Bug in ``pd.read_msgpack`` which did not allow to load dataframe with an index of type ``CategoricalIndex`` (:issue:`15487`)

- Bug in ``Series.asof`` which raised an error if the series contained all ``nans`` (:issue:`15713`)
6 changes: 3 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3972,11 +3972,11 @@ def asof(self, where, subset=None):
where = Index(where) if is_list else Index([where])

nulls = self.isnull() if is_series else self[subset].isnull().any(1)
if nulls.values.all():
if nulls.all():
if is_series:
return pd.Series([np.nan])
return pd.Series(np.nan, index=where)
else:
return pd.DataFrame([np.nan])
return pd.DataFrame(np.nan, index=where, columns=self.columns)

locs = self.index.asof_locs(where, ~(nulls.values))

Expand Down
22 changes: 20 additions & 2 deletions pandas/tests/frame/test_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class TestFrameAsof(TestData, tm.TestCase):

def setUp(self):
self.N = N = 50
rng = date_range('1/1/1990', periods=N, freq='53s')
self.rng = date_range('1/1/1990', periods=N, freq='53s')
self.df = DataFrame({'A': np.arange(N), 'B': np.arange(N)},
index=rng)
index=self.rng)

def test_basic(self):

Expand Down Expand Up @@ -86,3 +86,21 @@ def test_all_nans(self):
result = DataFrame([np.nan]).asof([0])
expected = DataFrame([np.nan])
tm.assert_frame_equal(result, expected)

# testing non-default indexes, multiple inputs
dates = date_range('1/1/1990', periods=self.N * 3, freq='25s')
result = DataFrame(np.nan, index=self.rng, columns=['A']).asof(dates)
expected = DataFrame(np.nan, index=dates, columns=['A'])
tm.assert_frame_equal(result, expected)

# testing multiple columns
dates = date_range('1/1/1990', periods=self.N * 3, freq='25s')
result = DataFrame(np.nan, index=self.rng, columns=['A', 'B', 'C']).asof(dates)
expected = DataFrame(np.nan, index=dates, columns=['A', 'B', 'C'])
tm.assert_frame_equal(result, expected)

# testing scalar input
date = date_range('1/1/1990', periods=self.N * 3, freq='25s')[0]
result = DataFrame(np.nan, index=self.rng, columns=['A']).asof(date)
expected = DataFrame(np.nan, index=[date], columns=['A'])
tm.assert_frame_equal(result, expected)
14 changes: 14 additions & 0 deletions pandas/tests/series/test_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,17 @@ def test_all_nans(self):
result = Series([np.nan]).asof([0])
expected = Series([np.nan])
tm.assert_series_equal(result, expected)

# testing non-default indexes
N = 50
rng = date_range('1/1/1990', periods=N, freq='53s')

dates = date_range('1/1/1990', periods=N * 3, freq='25s')
result = Series(np.nan, index=rng).asof(dates)
expected = Series(np.nan, index=dates)
tm.assert_series_equal(result, expected)

# testing scalar input
date = date_range('1/1/1990', periods=N * 3, freq='25s')[0]
result = Series(np.nan, index=rng).asof(date)
self.assertTrue(result != result)

0 comments on commit 70c958f

Please sign in to comment.