Have max and min functions ignore None
On *Tue Dec 29 06:22:58 EST 2015, Stephen D'Aprano wrote:*
So that's three perfectly reasonable behaviours: max(x, None) is an error and should raise; max(x, None) ignores None and returns x; max(x, None) is unknown or missing and returns None (or some other sentinel representing NA/Missing/Unknown).
For comparison's sake, SQL ignores NULL when doing MAX: e.g. select max(val) from (values (1),(null)) v(val) returns 1 In Python, None is sorta-kinda a bit like NULL in SQL, so one could make the argument that None should be handled similarly in min and max. OTOH I wouldn't want to see Python implement 3-valued logic. -- Gerald Britton, MCSE-DP, MVP LinkedIn Profile: http://ca.linkedin.com/in/geraldbritton
If None gets ignored, what about iterables that mix numbers and strings? A comprehension handles the simple case relatively succinctly and also handles more complex cases. max(x for x in iterable if isinstance(x, (int, float))) If you prefer is_number = lambda obj: isinstance(obj, numbers.Number) max(filter(is_number, iterable)) On Tue, Dec 29, 2015 at 6:02 PM Gerald Britton <gerald.britton@gmail.com> wrote:
On *Tue Dec 29 06:22:58 EST 2015, Stephen D'Aprano wrote:*
So that's three perfectly reasonable behaviours: max(x, None) is an error and should raise; max(x, None) ignores None and returns x; max(x, None) is unknown or missing and returns None (or some other sentinel representing NA/Missing/Unknown).
For comparison's sake, SQL ignores NULL when doing MAX:
e.g.
select max(val) from (values (1),(null)) v(val)
returns
1
In Python, None is sorta-kinda a bit like NULL in SQL, so one could make the argument that None should be handled similarly in min and max. OTOH I wouldn't want to see Python implement 3-valued logic.
-- Gerald Britton, MCSE-DP, MVP LinkedIn Profile: http://ca.linkedin.com/in/geraldbritton _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
On 30 December 2015 at 12:21, Michael Selik <mike@selik.org> wrote:
If None gets ignored, what about iterables that mix numbers and strings?
That's part of why I like Steven's suggestion of putting a capability along these lines in the statistics module: that reduces the input domain to numeric types, so the statistical analysis functions in NumPy and Pandas and the data aggregation functions in SQL become better behavioural guides. By contrast, the builtin min() and max() work with arbitrary (potentially heterogeneous) iterables, so special casing None (or NaN) doesn't make sense the way it does in strictly numerical analysis (or analysis with constrained types). Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
participants (3)
-
Gerald Britton
-
Michael Selik
-
Nick Coghlan