Skip to content

Fixturize tests/frame/test_asof.py #25628

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

Merged
merged 2 commits into from
Mar 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions pandas/tests/frame/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ def datetime_frame():
return DataFrame(tm.getTimeSeriesData())


@pytest.fixture
def date_range_frame():
"""
Fixture for DataFrame of ints with date_range index
Copy link
Contributor

Choose a reason for hiding this comment

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

not in love with this as a generalized fixture, if you put in test_asof would be ok

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So far, the requirement was always that this should be on tests/frame/conftest to be reusable for other test modules. But I don't care either way - can put it into test_asof.py as well.


Columns are ['A', 'B'].
"""
N = 50
rng = date_range('1/1/1990', periods=N, freq='53s')
return DataFrame({'A': np.arange(N), 'B': np.arange(N)}, index=rng)


@pytest.fixture
def float_string_frame():
"""
Expand Down
43 changes: 17 additions & 26 deletions pandas/tests/frame/test_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@
from pandas import DataFrame, Series, Timestamp, date_range, to_datetime
import pandas.util.testing as tm

from .common import TestData

class TestFrameAsof():

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

def test_basic(self):
df = self.df.copy()
def test_basic(self, date_range_frame):
df = date_range_frame
N = 50
df.loc[15:30, 'A'] = np.nan
dates = date_range('1/1/1990', periods=self.N * 3,
freq='25s')
dates = date_range('1/1/1990', periods=N * 3, freq='25s')

result = df.asof(dates)
assert result.notna().all(1).all()
Expand All @@ -35,11 +28,9 @@ def test_basic(self):
rs = result[mask]
assert (rs == 14).all(1).all()

def test_subset(self):
def test_subset(self, date_range_frame):
N = 10
rng = date_range('1/1/1990', periods=N, freq='53s')
df = DataFrame({'A': np.arange(N), 'B': np.arange(N)},
index=rng)
df = date_range_frame.iloc[:N].copy()
df.loc[4:8, 'A'] = np.nan
dates = date_range('1/1/1990', periods=N * 3,
freq='25s')
Expand All @@ -54,20 +45,18 @@ def test_subset(self):
expected = df.asof(dates)
tm.assert_frame_equal(result, expected)

# B gives self.df.asof
# B gives df.asof
result = df.asof(dates, subset='B')
expected = df.resample('25s', closed='right').ffill().reindex(dates)
expected.iloc[20:] = 9

tm.assert_frame_equal(result, expected)

def test_missing(self):
def test_missing(self, date_range_frame):
# GH 15118
# no match found - `where` value before earliest date in index
N = 10
rng = date_range('1/1/1990', periods=N, freq='53s')
df = DataFrame({'A': np.arange(N), 'B': np.arange(N)},
index=rng)
df = date_range_frame.iloc[:N].copy()
result = df.asof('1989-12-31')

expected = Series(index=['A', 'B'], name=Timestamp('1989-12-31'))
Expand All @@ -78,22 +67,24 @@ def test_missing(self):
columns=['A', 'B'], dtype='float64')
tm.assert_frame_equal(result, expected)

def test_all_nans(self):
def test_all_nans(self, date_range_frame):
# GH 15713
# DataFrame is all nans
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)
N = 150
rng = date_range_frame.index
dates = date_range('1/1/1990', periods=N, freq='25s')
result = DataFrame(np.nan, index=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,
dates = date_range('1/1/1990', periods=N, freq='25s')
result = DataFrame(np.nan, index=rng,
columns=['A', 'B', 'C']).asof(dates)
expected = DataFrame(np.nan, index=dates, columns=['A', 'B', 'C'])
tm.assert_frame_equal(result, expected)
Expand Down