有沒有方法可以近似熊貓時間序列的週期?對於R,xts
對象具有一個名爲periodicity
的方法,該方法完全適用於此目的。有沒有一個實施的方法來做到這一點?如何近似熊貓時間的週期系列
例如,我們可以從時間序列中推斷出沒有指定頻率的頻率嗎?
import pandas.io.data as web
aapl = web.get_data_yahoo("AAPL")
<class 'pandas.tseries.index.DatetimeIndex'>
[2010-01-04 00:00:00, ..., 2013-12-19 00:00:00]
Length: 999, Freq: None, Timezone: None
該系列的頻率可合理地近似爲每日。
更新:
我想可能是有幫助的,顯示的r實施週期方法的源代碼。
function (x, ...)
{
if (timeBased(x) || !is.xts(x))
x <- try.xts(x, error = "'x' needs to be timeBased or xtsible")
p <- median(diff(.index(x)))
if (is.na(p))
stop("can not calculate periodicity of 1 observation")
units <- "days"
scale <- "yearly"
label <- "year"
if (p < 60) {
units <- "secs"
scale <- "seconds"
label <- "second"
}
else if (p < 3600) {
units <- "mins"
scale <- "minute"
label <- "minute"
p <- p/60L
}
else if (p < 86400) {
units <- "hours"
scale <- "hourly"
label <- "hour"
}
else if (p == 86400) {
scale <- "daily"
label <- "day"
}
else if (p <= 604800) {
scale <- "weekly"
label <- "week"
}
else if (p <= 2678400) {
scale <- "monthly"
label <- "month"
}
else if (p <= 7948800) {
scale <- "quarterly"
label <- "quarter"
}
structure(list(difftime = structure(p, units = units, class = "difftime"),
frequency = p, start = start(x), end = end(x), units = units,
scale = scale, label = label), class = "periodicity")
}
我覺得這條線是關鍵,這一點我不太明白 p <- median(diff(.index(x)))
傅里葉變換可能會有幫助嗎? – Paul