Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python, sorting on date field, field may sometimes be null

Tags:

python

sorting

I am having a hard time coming up with a slick way to handle this sort. I have data coming back from a database read. I want to sort on the accoutingdate. However, accoutingdate may sometimes be null. I am currently doing the following:

results = sorted(results, key=operator.itemgetter('accountingdate'), reverse=True)

But, this bombs with "TypeError: can't compare datetime.date to NoneType" due to some accoutingdates being null.

What is the "most correct" or "most Pythonic" way to handle this?

like image 854
Wes Avatar asked Feb 01 '10 18:02

Wes


People also ask

Can you sort by date in python?

To sort a Python date string list using the sort function, you'll have to convert the dates in objects and apply the sort on them. For this you can use the key named attribute of the sort function and provide it a lambda that creates a datetime object for each date and compares them based on this date object.

What is sort () in Python?

Python sorted() Function The sorted() function returns a sorted list of the specified iterable object. You can specify ascending or descending order. Strings are sorted alphabetically, and numbers are sorted numerically. Note: You cannot sort a list that contains BOTH string values AND numeric values.

Is Python sort () stable?

sort() and sorted() in python are “stable sorts”, meaning that it'll preserve the existing order when faced with a tie.


2 Answers

Using a key= function is definitely right, you just have to decide how you want to treat the None values -- pick a datetime value that you want to treat as the equivalent of None for sorting purposes. E.g.:

import datetime mindate = datetime.date(datetime.MINYEAR, 1, 1)  def getaccountingdate(x):   return x['accountingdate'] or mindate  results = sorted(results, key=getaccountingdate, reverse=True) 

Just see how much simpler this is than defining a cmp function instead -- and if you do some benchmarking you'll find it's also significantly faster! There's no upside at all in using a cmp function instead of this key function, and it would be a bad design choice to do so.

like image 158
Alex Martelli Avatar answered Nov 07 '22 09:11

Alex Martelli


You could use a custom sorting function that treats None specially:

def nonecmp(a, b):   if a is None and b is None:     return 0   if a is None:     return -1   if b is None:     return 1   return cmp(a, b)  results = sorted(results, cmp=nonecmp, ...) 

This treats None as being smaller than all datetime objects.

like image 38
sth Avatar answered Nov 07 '22 11:11

sth