You Are What You Decide: A Journey in Automation of Our Selves. |
It is very convenient to collect and slice various data streams using MongoDB and Pandas (mentioned here). However, since I recently had been reading this wonderful Rob J Hyndman's R course on forecasting, I realized that I need to be able to be able to convert Pandas DataFrames to R's xts
and ts
objects. I found these great slides by Jared P. Lander, and here is how:
from pandas import DataFrame, read_csv # Read some DataFrame with TimeSeries data (I take the AAPL stocks) X = read_csv('http://www.quandl.com/api/v1/datasets/GOOG/NASDAQ_AAPL.csv?&trim_start=1981-03-11&trim_end=2013-10-28&sort_order=desc', parse_dates=True) # function to load pandas DataFrame into R # written by Wes McKinney def pandas_to_r(df, name): from rpy2.robjects import r, globalenv r_df = rpy.convert_to_r_dataframe(df) globalenv[name] = r_df # Send it to R pandas_to_r(X, "df") # convert to date %%R library(xts) df$Date <- as.Date(as.character(df$Date)) df$Close <- as.numeric(df$Close) df.xts <- xts(df$Close, order.by=df$Date)
Now, in my case, the course mostly deals with ts
series object, we can do it with extra line:
df.ts <- as.ts(align.time(df.xts,1))