4

グループ内のインデックスを並べ替えるクリーンな方法を探しています。
コード例:

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()

上記は、私が望むもの、年のユリウス日の集計統計を提供しますが、グループを並べ替えて、後半 (183 日) が前半の前に配置されるようにします。通常の numpy 配列の場合:

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

しかし、実装されていないエラーが発生する groupby でこれを行うことはできません。

注: これはgoogle-groupsからのクロス ポストです。また、私はcomp.lang.pythonを読んでいますが、残念ながら、Googleグループなどからの投稿を無視する傾向があります。

前もってありがとう、
ベヴァン

4

2 に答える 2

6

結果のインデックスを再作成しないのはなぜですか?

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
于 2012-11-02T14:46:13.087 に答える
0

このための特定の Pandas 関数を認識していませんが、 np.roll() 関数を検討できます。

myindex = np.arange(1,367)
myindex = np.roll(myindex, int(len(myindex)/2.))
于 2012-11-02T07:15:40.027 に答える