Efficiently checking date ranges in python
<p>I have a pandas.DataFrame ("loans") with about 250,000 rows of loan data.  For each record, I have the start date ("origination_date") and end date ("end_date").  I want to identify, for a particular 30 day period, how many loans were in existence.  I've successfully managed to do that, but it seems to be very slow:</p>

<pre><code>start = datetime.datetime(2005, 11, 1)
dates = [start + datetime.timedelta(days=int(30*i)) for i in np.arange(130)]
def in_date(loan_dates, date=dates[0]):
    orig = loan_dates[0]
    end = loan_dates[1]
    return(orig &lt;= date and end &gt; date)
def count_active(current_date):
    loan_filter = loans.ix[:, ('origination_date', 'end_date')].apply(
            in_date, date=current_date, axis=1)
    return(np.count_nonzero(loan_filter))
active_loans = [count_active(date) for date in dates]
</code></pre>

<p>My question is: is there a faster way to do this?  This takes 30 minutes or so in my iPython notebook, and it seems like a calculation like this shouldn't be too resource-intensive...</p>

<p>I'm fairly new to python, so I apologize if this is a silly question.  I've searched around StackOverflow and Google, but I haven't found any relevant answers.</p>