Skip to content

Commit

Permalink
Fix allow_None for param.Series and param.DataFrame (#473)
Browse files Browse the repository at this point in the history
* Fix allow_None for param.Series and param.DataFrame

* Fixed tests
  • Loading branch information
philippjfr authored May 13, 2021
1 parent 79bf066 commit aedad15
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
12 changes: 9 additions & 3 deletions param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,6 @@ def __init__(self, default=None, rows=None, columns=None, ordered=None, **params
super(DataFrame,self).__init__(pdDFrame, default=default, **params)
self._validate(self.default)


def _length_bounds_check(self, bounds, length, name):
message = '{name} length {length} does not match declared bounds of {bounds}'
if not isinstance(bounds, tuple):
Expand All @@ -1493,6 +1492,9 @@ def _validate(self, val):
if isinstance(self.columns, set) and self.ordered is True:
raise ValueError('Columns cannot be ordered when specified as a set')

if self.allow_None and val is None:
return

if self.columns is None:
pass
elif (isinstance(self.columns, tuple) and len(self.columns)==2
Expand Down Expand Up @@ -1548,15 +1550,19 @@ def _length_bounds_check(self, bounds, length, name):
if failure:
raise ValueError(message.format(name=name,length=length, bounds=bounds))

def __init__(self, default=None, rows=None, **params):
def __init__(self, default=None, rows=None, allow_None=False, **params):
from pandas import Series as pdSeries
self.rows = rows
super(Series,self).__init__(pdSeries, allow_None=True, default=default, **params)
super(Series,self).__init__(pdSeries, default=default, allow_None=allow_None,
**params)
self._validate(self.default)

def _validate(self, val):
super(Series, self)._validate(val)

if self.allow_None and val is None:
return

if self.rows is not None:
self._length_bounds_check(self.rows, len(val), 'Row')

Expand Down
44 changes: 44 additions & 0 deletions tests/API1/testpandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ def test_dataframe_positional_argument(self):
class Test(param.Parameterized):
df = param.DataFrame(valid_df)

def test_dataframe_allow_none(self):
class Test(param.Parameterized):
df = param.DataFrame(default=None, rows=3)

test = Test()
self.assertIs(test.df, None)

def test_dataframe_allow_none_constructor(self):
class Test(param.Parameterized):
df = param.DataFrame(allow_None=True, rows=3)

test = Test(df=None)
self.assertIs(test.df, None)

def test_dataframe_allow_none_set_value(self):
class Test(param.Parameterized):
df = param.DataFrame(allow_None=True, rows=3)

test = Test()
test.df = None
self.assertIs(test.df, None)

def test_empty_dataframe_param_invalid_set(self):
empty = pandas.DataFrame()
class Test(param.Parameterized):
Expand Down Expand Up @@ -176,6 +198,28 @@ def test_series_unordered_row_tuple_invalid(self):
class Test(param.Parameterized):
series = param.Series(default=invalid_series, rows=(5,7))

def test_series_allow_none(self):
class Test(param.Parameterized):
series = param.Series(default=None, rows=3)

test = Test()
self.assertIs(test.series, None)

def test_series_allow_none_constructor(self):
class Test(param.Parameterized):
series = param.Series(allow_None=True, rows=3)

test = Test(series=None)
self.assertIs(test.series, None)

def test_series_allow_none_set_value(self):
class Test(param.Parameterized):
series = param.Series(allow_None=True, rows=3)

test = Test()
test.series = None
self.assertIs(test.series, None)

if __name__ == "__main__":
import nose
nose.runmodule()

0 comments on commit aedad15

Please sign in to comment.