I have a dataframe pairs where each row is a transaction with corresponding price and date. Now, I need to assign each transaction to a quintile (add a new column), but the quintile bins have been defined for each period frequency (year, quarter or month) and borough separately, so generally what I need is groupby -> qcut.Here is an existing function:def tier_data(pairs, tiers_typed):    '''for each record returns tier    cuts pairs according to their tier (quintile)    Arguments:        pairs -- transaction pairs to operate on        tiers_type -- tiers dataframe    Returns:        pd.Series -- tier for each pair    '''    freqstr = tiers_typed.index.get_level_values(0).freqstr    pairs['tiers'] = pd.Series(pd.np.empty(len(pairs)) * pd.np.nan, index=pairs.index)    global LABELS    freq_date = pairs['date0'].dt.to_period(freqstr)    for (y, b), bins in tiers_typed.iterrows():        mask = (freq_date == y) & (pairs['Borough'] == b)        trs[mask] = pd.cut(pairs.loc[mask, 'price0'], bins, labels=LABELS)    return trsHowever it turns out to be pretty slow... any ideas on how to boost it up?here, price is an integer, date is a date, Borough is a string, bins is a pd.Series, obviously.
pandas time-based qcut
python;performance;datetime;numpy;pandas