平均只有半小时的数据

时间:2012-05-22 16:01:00

标签: python pandas

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

目前我有:

# 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的情况吗?

是否有一个清洁工是按分钟进行上述分组?

暂且不说:truncate是否需要成为数据中的现有索引?我收到以下错误(在上面的一组不同数据上):

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)

1 个答案:

答案 0 :(得分: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)