This is the closest link I've found: https://stats.stackexchange.com/questions/5305/how-to-re-sample-an-xts-time-series-in-r
But I don't see anything about the different ways to aggregate the data (like mean, count, anonymous function) which you can do in pandas.
For my program, I'm trying to have a dataframe be resampled every 2 minutes and take the average of the 2 values at each interval. Thanks!
If you use data.table
and lubridate
it might look something like this
library(data.table)
library(lubridate)
#sample data
dt<-data.table(ts=seq(from=ymd('2015-01-01'), to=ymd('2015-07-01'),by='mins'), datum=runif(260641,0,100))
if you wanted to get the data from minute to hourly means you could do
dt[,mean(datum),by=floor_date(ts,"hour")]
if you had a bunch of columns and you wanted all of them to be averaged you could do
dt[,lapply(.SD,mean),by=floor_date(ts,"hour")]
You can replace mean
for any function you'd like. You can replace "hour" with "second", "minute", "hour", "day", "week", "month", "year". Well you can't go from minute to seconds as that would require magic but you can go from micro seconds to seconds anyway.
It is not possible to convert a series from a lower periodicity to a higher periodicity - e.g. weekly to daily or daily to 5 minute bars, as that would require magic.
-Jeffrey Ryan from xts manual.
I never learned xts so I don't know the syntax to do it with xts objects but that line is famous (or at least as famous as a line from a manual can be)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With