-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added pandas DataFrame and Series parameter types (#285)
- Loading branch information
1 parent
a97c18b
commit 4a6e61d
Showing
7 changed files
with
308 additions
and
1 deletion.
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
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,170 @@ | ||
""" | ||
Test Parameters based on pandas | ||
""" | ||
import unittest | ||
import os | ||
|
||
import param | ||
from . import API1TestCase | ||
|
||
try: | ||
import pandas | ||
except ImportError: | ||
if os.getenv('PARAM_TEST_PANDAS','0') == '1': | ||
raise ImportError("PARAM_TEST_PANDAS=1 but pandas not available.") | ||
else: | ||
raise unittest.SkipTest("pandas not available") | ||
|
||
|
||
class TestDataFrame(API1TestCase): | ||
|
||
def test_empty_dataframe_param_invalid_set(self): | ||
empty = pandas.DataFrame() | ||
class Test(param.Parameterized): | ||
df = param.DataFrame(default=empty) | ||
|
||
test = Test() | ||
exception = "Parameter 'df' value must be an instance of DataFrame, not '3'" | ||
with self.assertRaisesRegexp(ValueError, exception): | ||
test.df = 3 | ||
|
||
def test_dataframe_unordered_column_set_valid(self): | ||
valid_df = pandas.DataFrame({'a':[1,2], 'b':[2,3], 'c':[4,5]}, columns=['b', 'a', 'c']) | ||
class Test(param.Parameterized): | ||
df = param.DataFrame(default=valid_df, columns={'a', 'b'}) | ||
|
||
|
||
def test_dataframe_unordered_column_set_invalid(self): | ||
valid_df = pandas.DataFrame({'a':[1,2], 'b':[2,3], 'd':[4,5]}, columns=['b', 'a', 'd']) | ||
invalid_df = pandas.DataFrame({'a':[1,2], 'b':[2,3], 'c':[4,5]}, columns=['b', 'a', 'c']) | ||
|
||
class Test(param.Parameterized): | ||
df = param.DataFrame(default=valid_df, columns={'a', 'd'}) | ||
|
||
|
||
test = Test() | ||
self.assertEquals(test.param.params('df').ordered, False) | ||
exception = "Provided DataFrame columns \['b', 'a', 'c'\] does not contain required columns \['a', 'd'\]" | ||
with self.assertRaisesRegexp(ValueError, exception): | ||
test.df = invalid_df | ||
|
||
def test_dataframe_ordered_column_list_valid(self): | ||
valid_df = pandas.DataFrame({'a':[1,2], 'b':[2,3], 'c':[4,5]}, columns=['b', 'a', 'c']) | ||
class Test(param.Parameterized): | ||
test = param.DataFrame(default=valid_df, columns=['b', 'a', 'c']) | ||
|
||
|
||
def test_dataframe_ordered_column_list_invalid(self): | ||
valid_df = pandas.DataFrame({'a':[1,2], 'b':[2,3], 'd':[4,5]}, columns=['b', 'a', 'd']) | ||
invalid_df = pandas.DataFrame({'a':[1,2], 'b':[2,3], 'c':[4,5]}, columns=['a', 'b', 'd']) | ||
|
||
class Test(param.Parameterized): | ||
df = param.DataFrame(default=valid_df, columns=['b', 'a', 'd']) | ||
|
||
test = Test() | ||
self.assertEquals(test.param.params('df').ordered, True) | ||
|
||
exception = "Provided DataFrame columns \['a', 'b', 'd'\] must exactly match \['b', 'a', 'd'\]" | ||
with self.assertRaisesRegexp(ValueError, exception): | ||
test.df = invalid_df | ||
|
||
|
||
def test_dataframe_unordered_column_number_valid_df(self): | ||
valid_df = pandas.DataFrame({'a':[1,2], 'b':[2,3], 'c':[4,5]}, columns=['b', 'a', 'c']) | ||
class Test(param.Parameterized): | ||
df = param.DataFrame(default=valid_df, columns=3) | ||
|
||
def test_dataframe_unordered_column_number_invalid(self): | ||
valid_df = pandas.DataFrame({'a':[1,2], 'b':[2,3], 'c':[4,5]}, columns=['b', 'a', 'c']) | ||
invalid_df = pandas.DataFrame({'a':[1,2], 'b':[2,3]}, columns=['b', 'a']) | ||
class Test(param.Parameterized): | ||
df = param.DataFrame(default=valid_df, columns=3) | ||
|
||
test = Test() | ||
self.assertEquals(test.param.params('df').ordered, None) | ||
|
||
exception = "Column length 2 does not match declared bounds of 3" | ||
with self.assertRaisesRegexp(ValueError, exception): | ||
test.df = invalid_df | ||
|
||
|
||
def test_dataframe_unordered_column_tuple_valid(self): | ||
valid_df = pandas.DataFrame({'a':[1,2], 'b':[2,3], 'c':[4,5]}, columns=['b', 'a', 'c']) | ||
class Test(param.Parameterized): | ||
df = param.DataFrame(default=valid_df, columns=(None,3)) | ||
|
||
def test_dataframe_unordered_column_tuple_invalid(self): | ||
|
||
invalid_df = pandas.DataFrame({'a':[1,2], 'b':[2,3], 'c':[4,5]}, columns=['b', 'a', 'c']) | ||
|
||
exception = "Columns length 3 does not match declared bounds of \(None, 2\)" | ||
with self.assertRaisesRegexp(ValueError, exception): | ||
class Test(param.Parameterized): | ||
df = param.DataFrame(default=invalid_df, columns=(None,2)) | ||
|
||
def test_dataframe_row_number_valid_df(self): | ||
valid_df = pandas.DataFrame({'a':[1,2], 'b':[2,3], 'c':[4,5]}, columns=['b', 'a', 'c']) | ||
class Test(param.Parameterized): | ||
df = param.DataFrame(default=valid_df, rows=2) | ||
|
||
def test_dataframe_row_number_invalid(self): | ||
valid_df = pandas.DataFrame({'a':[1,2], 'b':[2,3]}, columns=['b', 'a']) | ||
invalid_df = pandas.DataFrame({'a':[1,2,4], 'b':[2,3,4]}, columns=['b', 'a']) | ||
class Test(param.Parameterized): | ||
df = param.DataFrame(default=valid_df, rows=2) | ||
|
||
test = Test() | ||
exception = "Row length 3 does not match declared bounds of 2" | ||
with self.assertRaisesRegexp(ValueError, exception): | ||
test.df = invalid_df | ||
|
||
def test_dataframe_unordered_row_tuple_valid(self): | ||
valid_df = pandas.DataFrame({'a':[1,2], 'b':[2,3], 'c':[4,5]}, columns=['b', 'a', 'c']) | ||
class Test(param.Parameterized): | ||
df = param.DataFrame(default=valid_df, rows=(None,3)) | ||
|
||
def test_dataframe_unordered_row_tuple_invalid(self): | ||
|
||
invalid_df = pandas.DataFrame({'a':[1,2], 'b':[2,3], 'c':[4,5]}, columns=['b', 'a', 'c']) | ||
|
||
exception = "Row length 2 does not match declared bounds of \(5, 7\)" | ||
with self.assertRaisesRegexp(ValueError, exception): | ||
class Test(param.Parameterized): | ||
df = param.DataFrame(default=invalid_df, rows=(5,7)) | ||
|
||
|
||
class TestSeries(API1TestCase): | ||
|
||
def test_dataframe_row_number_valid_df(self): | ||
valid_df = pandas.Series([1,2]) | ||
class Test(param.Parameterized): | ||
df = param.Series(default=valid_df, rows=2) | ||
|
||
def test_dataframe_row_number_invalid(self): | ||
valid_df = pandas.Series([1,2]) | ||
invalid_df = pandas.Series([1,2,3]) | ||
class Test(param.Parameterized): | ||
df = param.Series(default=valid_df, rows=2) | ||
|
||
test = Test() | ||
exception = "Row length 3 does not match declared bounds of 2" | ||
with self.assertRaisesRegexp(ValueError, exception): | ||
test.df = invalid_df | ||
|
||
def test_dataframe_unordered_row_tuple_valid(self): | ||
valid_df = pandas.Series([1,2,3]) | ||
class Test(param.Parameterized): | ||
df = param.Series(default=valid_df, rows=(None,3)) | ||
|
||
def test_dataframe_unordered_row_tuple_invalid(self): | ||
|
||
invalid_df = pandas.Series([1,2]) | ||
|
||
exception = "Row length 2 does not match declared bounds of \(5, 7\)" | ||
with self.assertRaisesRegexp(ValueError, exception): | ||
class Test(param.Parameterized): | ||
df = param.Series(default=invalid_df, rows=(5,7)) | ||
|
||
if __name__ == "__main__": | ||
import nose | ||
nose.runmodule() |
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