-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move apply_filter_query function to models.util module for easy shari…
…ng between model classes.
- Loading branch information
Showing
5 changed files
with
84 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import pandas as pd | ||
import pytest | ||
from pandas.util import testing as pdt | ||
|
||
from .. import util | ||
|
||
|
||
@pytest.fixture | ||
def test_df(): | ||
return pd.DataFrame( | ||
{'col1': range(5), | ||
'col2': range(5, 10)}, | ||
index=['a', 'b', 'c', 'd', 'e']) | ||
|
||
|
||
def test_apply_filter_query(test_df): | ||
filters = ['col1 < 3', 'col2 > 6'] | ||
filtered = util.apply_filter_query(test_df, filters) | ||
expected = pd.DataFrame( | ||
{'col1': [2], 'col2': [7]}, | ||
index=['c']) | ||
pdt.assert_frame_equal(filtered, expected) | ||
|
||
|
||
def test_apply_filter_query_empty(test_df): | ||
filters = ['col1 < 1', 'col2 > 8'] | ||
filtered = util.apply_filter_query(test_df, filters) | ||
expected = pd.DataFrame( | ||
{'col1': [], 'col2': []}, | ||
index=[]) | ||
pdt.assert_frame_equal(filtered, expected) | ||
|
||
|
||
def test_apply_filter_query_or(test_df): | ||
filters = ['col1 < 1 or col2 > 8'] | ||
filtered = util.apply_filter_query(test_df, filters) | ||
expected = pd.DataFrame( | ||
{'col1': [0, 4], 'col2': [5, 9]}, | ||
index=['a', 'e']) | ||
pdt.assert_frame_equal(filtered, expected) | ||
|
||
|
||
def test_apply_filter_query_no_filter(test_df): | ||
filters = [] | ||
filtered = util.apply_filter_query(test_df, filters) | ||
expected = test_df | ||
pdt.assert_frame_equal(filtered, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
def apply_filter_query(df, filters=None): | ||
""" | ||
Use the DataFrame.query method to filter a table down to the | ||
desired rows. | ||
Parameters | ||
---------- | ||
df : pandas.DataFrame | ||
filters : list of str, optional | ||
List of filters to apply. Will be joined together with | ||
' and ' and passed to DataFrame.query. | ||
If not supplied no filtering will be done. | ||
Returns | ||
------- | ||
filtered_df : pandas.DataFrame | ||
""" | ||
if filters: | ||
query = ' and '.join(filters) | ||
return df.query(query) | ||
else: | ||
return df |