我正在试图弄清楚如何处理具有不均匀周期长度的pandas中的时间序列数据。我正在研究的第一个例子是如何计算过去15天的移动平均线。以下是数据示例(时间为UTC)
index date_time data
46701 1/06/2016 19:27 15.00
46702 1/06/2016 19:28 18.25
46703 1/06/2016 19:30 16.50
46704 1/06/2016 19:33 17.20
46705 1/06/2016 19:34 18.18
我不确定我是否应该只填写数据,所以甚至1分钟的增量,或者如果有更聪明的方式...如果有人有建议,将非常感激
谢谢 - KC
答案 0 :(得分:3)
你可以这样做。
bfill
(使用下一个有效值的后填充),但另一种策略可能更合适,如ffill
(传播最后一个有效值的前向填充)。 注意:rolling
中引入了pd.rolling_mean
的此语法。但是,可以使用# Test data
d = {'data': [15.0, 18.25, 16.5, 17.199999999999999, 18.18],
'date_time': ['1/06/2016 19:27',
'1/06/2016 19:28',
'1/06/2016 19:30',
'1/06/2016 19:33',
'1/06/2016 19:34'],
'index': [46701, 46702, 46703, 46704, 46705]}
df = DataFrame(d)
df['date_time'] = pd.to_datetime(df['date_time'])
# Setting the date as the index
df.set_index('date_time', inplace=True)
# Resampling data
df = df.resample('1T').bfill()
# Performing moving average
df['moving'] = df['data'].rolling(window=3, center=True).mean()
df.plot(y=['data', 'moving'])
df
data index moving
date_time
2016-01-06 19:27:00 15.00 46701 NaN
2016-01-06 19:28:00 18.25 46702 16.583333
2016-01-06 19:29:00 16.50 46703 17.083333
2016-01-06 19:30:00 16.50 46703 16.733333
2016-01-06 19:31:00 17.20 46704 16.966667
2016-01-06 19:32:00 17.20 46704 17.200000
2016-01-06 19:33:00 17.20 46704 17.526667
2016-01-06 19:34:00 18.18 46705 NaN
在先前版本中执行相同的操作。
# Random data parameters
num_sample = (0, 100)
nb_sample = 1000
start_date = '2016-06-02'
freq = '2T'
random_state = np.random.RandomState(0)
# Generating random data
df = pd.DataFrame({'data': random_state.randint(num_sample[0], num_sample[1], nb_sample)},
index=random_state.choice(
pd.date_range(start=pd.to_datetime(start_date), periods=nb_sample * 3,
freq=freq),
nb_sample))
# Removing duplicate index
df = df.groupby(df.index).first()
# Removing data for closed periods
df.loc[(df.index.hour >= 22) | (df.index.hour <= 7),'data'] = np.nan
# Resampling
df = df.resample('1T').ffill()
# Moving average by hours
df['avg'] = df['data'].rolling(window=60).mean()
ax = df.plot(kind='line', subplots=True)
以下是缺少数据的示例。
<?php
$name = @trim(stripslashes($_POST['name']));
$from = @trim(stripslashes($_POST['email']));
$subject = @trim(stripslashes($_POST['subject']));
$message = @trim(stripslashes($_POST['message']));
$to = 'valid-address@valid-domain.com';
$headers = "MIME-Version: 1.0\n".
"Content-Type: text/plain; charset=\"iso-8859-1\"\n".
"From: $name\n".
"Reply-To: $from\n".
"Subject: $subject\n".
"X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
echo
// Back to previous page
"<script>
alert('Dziękuje za przesłanie wiadomości. Postaram się odpowiedzieć możliwie szybko.');
window.history.go(-1);
</script>";
die;
?>