1

我两周前开始学习,现在我有点卡住了。我有 2 个 TimeSeries,看起来像这样:

2011-01-09 00:00:00+00:00    7.430126
2011-01-09 01:00:00+00:00    6.793855
2011-01-09 02:00:00+00:00    6.675949
2011-01-09 03:00:00+00:00    6.756636
2011-01-09 04:00:00+00:00    6.875174
2011-01-09 05:00:00+00:00    5.432611
2011-01-09 06:00:00+00:00    6.059197
2011-01-09 21:00:00+00:00    5.338928
2011-01-09 22:00:00+00:00    5.259672
2011-01-09 23:00:00+00:00    5.247196
2011-01-10 00:00:00+00:00    5.889274
2011-01-10 01:00:00+00:00    6.133871
2011-01-10 02:00:00+00:00    6.111958
2011-01-10 03:00:00+00:00    5.873732
2011-01-10 04:00:00+00:00    5.627684
2011-01-10 05:00:00+00:00    5.265644
2011-01-10 06:00:00+00:00    5.505559
2011-01-10 21:00:00+00:00    3.835050
2011-01-10 22:00:00+00:00    3.879653
2011-01-10 23:00:00+00:00    4.034543
2011-01-11 00:00:00+00:00    4.844272
2011-01-11 01:00:00+00:00    4.670967
2011-01-11 02:00:00+00:00    4.584164
2011-01-11 03:00:00+00:00    4.786821

这是风速测量的数据,我想将其与模型数据进行比较。更具体地说,我想比较夜间(21.00 - 6.00)的风速。所以我定义了一个函数:

def func(model, measure):
    return (model-measure).mean()

此外,我在数据上创建了一个循环:

mean_night = []
start = 7
for a in night:
    mean_night.append(func(model, measure[start:(start+10)]))
    start = start+11
    if start>5378:
            break

问题是我丢失了时间索引并且丢失了一些数据(例如 1 天或 1 周),所以我在使用 DateRange 重新索引它时遇到了麻烦。最后,它应该如下所示:

date    difference_means
2011-01-09    diff_1
2011-01-09    diff_2

等等。我使用熊猫 0.7.1。谢谢你的支持!(对不起我的英语不好:P)

4

3 回答 3

2

pandas 0.8.1 对于每小时采样数据:

In [57]: import pandas

In [58]: import numpy

In [59]: index = pandas.date_range(start='2011-01-09', periods=240, freq='H')

In [60]: s = pandas.Series(np.random.randn(len(index)), index)

In [61]: s_night = s[(s.index.hour >= 21) | (s.index.hour <= 6)]

In [62]: def day_or_night(dates):
   ....:     r = []
   ....:     for date in dates:
   ....:         if (date.hour >= 21) | (date.hour <= 6):
   ....:             d = datetime.datetime(date.year, date.month, date.day)
   ....:             if (date.hour <= 6):
   ....:                 d = d - pandas.offsets.Day()
   ....:             r.append(d)
   ....:         else:
   ....:             r.append('day')
   ....:     return r
   ....:

In [63]: s_night.groupby(day_or_night(s_night.index)).mean()
Out[63]:
2011-01-08    0.652095
2011-01-09    0.004129
2011-01-10    0.457892
2011-01-11   -0.078547
2011-01-12    0.008087
2011-01-13    0.043568
2011-01-14    0.505970
2011-01-15    0.150971
2011-01-16    0.107265
2011-01-17    0.117811
2011-01-18   -0.191193
于 2012-08-21T20:08:28.723 回答
0

您应该升级到 0.8.1 并利用所有新的时间序列功能。请查看http://pandas.pydata.org获取文档。

在最新版本中,结帐功能喜欢between_time在特定时间范围内进行过滤。

于 2012-08-21T16:49:59.667 回答
0

我终于找到了一个可行的解决方案:

hr = dr.map(lambda x: x.hour)
meantime = lambda x: x.replace(hour=0)

datra = pd.DateRange('2011/1/1', '2011/12/31', offset=pd.datetools.day)
rise = pd.TimeSeries(np.cos(((datra.map(lambda x: (x-datetime(x.year,1,1)).total_seconds() / 86400) + 10) / 183. * np.pi)) * -2. + 17., index=datra)
set = pd.TimeSeries(np.cos(((datra.map(lambda x: (x-datetime(x.year,1,1)).total_seconds() / 86400) + 10) / 183. * np.pi)) * 2.5 + 5., index=datra)

i=0
def bias_night(liste, group):
while (i<546):
    if (i<364):
        z = group[dr[hr>unter11[i]]].combine_first(group[dr[hr<auf11[i+1]]]).groupby(meantime).mean()
        liste.append(z[i])
    else:
        z = group[dr[hr>unter11[i-365]]].combine_first(group[dr[hr<auf11[i-365+1]]]).groupby(meantime).mean()
        liste.append(z[i])
    i = i+1
t = group[dr[hr>unter11[364]]].combine_first(group[dr[hr<auf11[0]]]).groupby(meantime).mean()
liste.insert(364, t[364])

liste 是一个空列表, group 是我的 TimeSeries 之一。最后,我只需减去结果列表即可得到我想要的。

2011-01-09   -1.179578
2011-01-10   -0.978171
2011-01-11   -0.335977
2011-01-12    0.080671
2011-01-13   -0.324661
2011-01-14    0.012359
2011-01-15   -0.549079
于 2012-08-22T15:08:07.557 回答