categorical, argsort iloc
, , kind='mergesort', mergesort "" .
mcats = [4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3]
nudf.iloc[pd.Categorical(nudf.date.dt.month, mcats, True).argsort(kind='mergesort')]
date level_1 0
4 2017-04-01 02:00:00 2.0 63.0
5 2017-04-01 02:30:00 2.5 60.0
0 2016-02-01 00:00:00 0.0 74.0
1 2016-02-01 00:30:00 0.5 72.0
2 2016-03-01 01:00:00 1.0 70.0
3 2016-03-01 01:30:00 1.5 64.0
mcats = [4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3]
nudf = nudf.assign(month=pd.Categorical(nudf.date.dt.month, mcats, True))
nudf.sort_values('month', kind='mergesort')
date level_1 0 month
4 2017-04-01 02:00:00 2.0 63.0 4
5 2017-04-01 02:30:00 2.5 60.0 4
0 2016-02-01 00:00:00 0.0 74.0 2
1 2016-02-01 00:30:00 0.5 72.0 2
2 2016-03-01 01:00:00 1.0 70.0 3
3 2016-03-01 01:30:00 1.5 64.0 3
, ,
mcats = [4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3]
nudf = nudf.assign(month=pd.Categorical(nudf.date.dt.month, mcats, True))
nudf.sort_values(['month', 'date'])
argsort np.lexsort, , .
mcats = [4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3]
nudf.iloc[np.lexsort(
[nudf.date, pd.Categorical(nudf.date.dt.month, mcats, True)]
)]