选择一系列时间序列数据并执行数据分析

时间:2015-01-19 15:53:42

标签: python pandas time-series

我有一个监控温度,压力和湿度的监控设备。通过这些数据,我得到了测量的日期和时间。测量每5秒进行一次。我想写一个函数,它给出了特定日期和时间范围内的温度,压力和湿度的平均值和标准偏差。理想情况下是这样......

def TempPressHumid(time_start, time_end, data_start, date_end, temp_data, press_data, humid_data)
到目前为止,我有这个:

import pandas as pd
import numpy as np

df = pd.read_csv('TM4CVC.csv', index_col = 0)

temp_data = df['Temperature']
temp_av = np.mean(temp_data)
temp_sd = np.std(temp_data)

humid_data = df['humidity']
humid_av = np.mean(humid_data)
humid_sd = np.std(humid_data)

press_data = df['pressure']
press_av = np.mean(press_data)
press_sd = np.std(press_data)

这可能吗?

谢谢,

乔伊

1 个答案:

答案 0 :(得分:0)

这应该这样做。按时间和日期切片df。您可以将功能更改为仅接受日期,然后使用格式' yyyy-mm-dd hh:mm:ss'如果你只需要一个连续的日期时间而不必每次都选择时间和日期,则将其分割。

import pandas as pd
import numpy as np
import random

def TempPressHumid(time_start, time_end, date_start, date_end, df):

    temp = df[date_start:date_end]
    temp = df.between_time(time_start,time_end)

    out = {'temp_avg':np.mean(temp['temp']),
    'temp_std':np.std(temp['temp']),
    'press_avg':np.mean(temp['press']),
    'press_std':np.std(temp['press']),
    'humid_avg':np.mean(temp['humid']),
    'humid_std':np.std(temp['humid'])}
    print out


df = pd.DataFrame({'temp':[random.randint(50, 80) for x in range(51841)],
    'press':[random.randint(20, 40) for x in range(51841)],
    'humid':[random.randint(20, 80) for x in range(51841)]}, 
    index = pd.date_range(start = '2014-01-01', end = '2014-01-04', freq = '5S'))

TempPressHumid(time_start = '01:00:00', time_end = '23:00:00', date_start = '2014-01-02', date_end = '2014-01-03', df = df)

这将获得2014-01-02和2014-01-03之间凌晨1点到11点之间的所有数据。

相关问题