0

我试图获得一段时间序列数据的平均值(超过 24 小时,间隔 30 分钟)。

目前我有:

# Data of interest
START = datetime(2010,2,1,0,0,0)
END = datetime(2010,2,8,23,59,59)

# Function to group by time of day
def minutes(date):
    time = date.strftime("%H:%M:%S")
    dt = datetime.strptime("2009-04-01 " + time, "%Y-%m-%d %H:%M:%S")
    return dt

# Truncate data to desired section
telford = store.get_data("spd_mw").TELR.truncate(START,END)

# Group and average
telford.groupby(minutes).mean()

我试过使用asfreq(datetools.BDay()),但它会删除半小时的数据。是写我自己的 DateOffset 的情况吗?

有没有更简洁的方法来按分钟对上述数据进行分组?

顺便说一句:截断是否需要是数据中的现有索引?我收到以下错误(在与上述不同的一组数据上):

Traceback (most recent call last):
  File "power-weather.py", line 21, in <module>
    egph_temp = store.get_data("weather_EGPH").TemperatureC.truncate(START,END)
  File "/usr/local/lib/python2.6/dist-packages/pandas-0.7.3-py2.6-linux-x86_64.egg/pandas/core/generic.py", line 702, in truncate
    result = self.ix[before:after]
  File "/usr/local/lib/python2.6/dist-packages/pandas-0.7.3-py2.6-linux-x86_64.egg/pandas/core/indexing.py", line 35, in __getitem__
    return self._getitem_axis(key, axis=0)
  File "/usr/local/lib/python2.6/dist-packages/pandas-0.7.3-py2.6-linux-x86_64.egg/pandas/core/indexing.py", line 167, in _getitem_axis
    return self._get_slice_axis(key, axis=axis)
  File "/usr/local/lib/python2.6/dist-packages/pandas-0.7.3-py2.6-linux-x86_64.egg/pandas/core/indexing.py", line 372, in _get_slice_axis
    i, j = labels.slice_locs(start, stop)
  File "/usr/local/lib/python2.6/dist-packages/pandas-0.7.3-py2.6-linux-x86_64.egg/pandas/core/index.py", line 842, in slice_locs
    beg_slice = self.get_loc(start)
  File "/usr/local/lib/python2.6/dist-packages/pandas-0.7.3-py2.6-linux-x86_64.egg/pandas/core/index.py", line 523, in get_loc
    return self._engine.get_loc(key)
  File "engines.pyx", line 101, in pandas._engines.DictIndexEngine.get_loc (pandas/src/engines.c:2498)
  File "engines.pyx", line 108, in pandas._engines.DictIndexEngine.get_loc (pandas/src/engines.c:2460)
KeyError: datetime.datetime(2010, 2, 1, 0, 0)
4

1 回答 1

1

解决了:

def weekdays(date):
    if 0 < int(datetime.strftime(date, "%w")) < 6:
        return True
    return False

def weekends(date):
    if int(datetime.strftime(date, "%w")) == (0 or 6):
        return True
    return False

telford = telford.select(weekdays)
于 2012-05-24T12:54:34.987 回答