So I'm having a little trouble ordering my Dataframe, I've tried using this question, but haven't managed to get it to work. What I have is a Dataframe nudf
like so:
date level_1 0
0 2016-10-01 00:00:00 0.0 74.00
1 2016-10-01 00:30:00 0.5 72
2 2016-10-01 01:00:00 1.0 70
3 2016-10-01 01:30:00 1.5 64
4 2016-10-01 02:00:00 2.0 63
5 2016-10-01 02:30:00 2.5 60
... ... ... ...
19003 2017-09-31 21:30:00 21.5 129
19004 2017-09-31 22:00:00 22.0 118
19005 2017-09-31 22:30:00 22.5 106
19006 2017-09-31 23:00:00 23.0 84
19007 2017-09-31 23:30:00 23.5 76
And what I would like to do is order the rows by an external month order:
[4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3]
This is the last 12 months, from last month. I'd like to ignore the year and order each month block of rows in accordance with the order above.
For example, given the following rows:
0 2016-02-01 00:00:00 0.0 74.00
1 2016-02-01 00:30:00 0.5 72
2 2016-03-01 01:00:00 1.0 70
3 2016-03-01 01:30:00 1.5 64
4 2017-04-01 02:00:00 2.0 63
5 2017-04-01 02:30:00 2.5 60
The result should be:
4 2017-04-01 02:00:00 2.0 63
5 2017-04-01 02:30:00 2.5 60
0 2016-02-01 00:00:00 0.0 74.00
1 2016-02-01 00:30:00 0.5 72
2 2016-03-01 01:00:00 1.0 70
3 2016-03-01 01:30:00 1.5 64
I've tried:
nudf['month'] = nudf.apply(lambda row: row.date.month, axis=1)
nudf.month = nudf.month.astype("category")
nudf.month.cat.set_categories([x.month for x in reversed(_get_last_x_months(12))], inplace=True)
nudf.sort_values(["month"], inplace=True)
But the day and hour order is not maintained.