Reindex or reordering group

I am looking for a clean way to reorder indexes in a group.
Code example:

import numpy as np import pandas as pd mydates = pd.date_range('1/1/2012', periods=1000, freq='D') myts = pd.Series(np.random.randn(len(mydates)), index=mydates) grouped = myts.groupby(lambda x: x.timetuple()[7]) mymin = grouped.min() mymax = grouped.max() 

The above gives me what I want, the aggregate statistics on the Julian day of the year, but I would like to change the order of the group, so the last half (183 days) is placed before the first half. Using a regular numpy array:

 myindex = np.arange(1,367) myindex = np.concatenate((myindex[183:],myindex[:183])) 

But I can not do this with groupby, as it causes an implementation error.

Note. This is a cross-entry from google-groups . I also read on comp.lang.python, unfortunately, people tend to ignore some posts, for example. from google groups.

Thanks in advance,
Bevan

+4
source share
2 answers

Why not just reindex the result?

 In [7]: mymin.reindex(myindex) Out[7]: 184 -0.788140 185 -2.206314 186 0.284884 187 -2.197727 188 -0.714634 189 -1.082745 190 -0.789286 191 -1.489837 192 -1.278941 193 -0.795507 194 -0.661476 195 0.582994 196 -1.634310 197 0.104332 198 -0.602378 ... 169 -1.150616 170 -0.315325 171 -2.233139 172 -1.081528 173 -1.316668 174 -0.963783 175 -0.215260 176 -2.723446 177 -0.493480 178 -0.706771 179 -2.082051 180 -1.066649 181 -1.455419 182 -0.332383 183 -1.277424 
+6
source

I am not aware of the specific Panda function for this, but you can consider the np.roll () function:

 myindex = np.arange(1,367) myindex = np.roll(myindex, int(len(myindex)/2.)) 
0
source

Source: https://habr.com/ru/post/1443562/


All Articles