Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert pandas.TimeSeries to R.ts

I have some pandas TimeSeries with date index:

import pandas as pd
import numpy as np
pandas_ts = pd.TimeSeries(np.random.randn(100),pd.date_range(start='2000-01-01', periods=100))

I need convert it to R TS (like sunspots dataset) to call some R function (slt) with my TS, which works only with timeseries. But i found that in pandas.rpy and rpy2 API's there is only DataFrame support. Is there another way to do this?

If there is no such I can convert TS to DataFrame in python, then convert it to R DF and convert it to TS in R but I have some troubles at last step because i'm new in R.

Any ideas or help in converting in R? =)

like image 635
Andrey Shokhin Avatar asked Nov 02 '22 09:11

Andrey Shokhin


1 Answers

I am not a pandas proficient , But you can save you pandas time series to csv file and read it from R.

Python:

## write data 
with open(PATH_CSV_FILE,"w") as file:
   pandas_ts.to_csv(file)
## read data
with open(PATH_CSV_FILE,"r") as file:
   pandas_ts.from_csv(file)

R:

library(xts)
## to read data 
ts.xts <- read.zoo(PATH_CSV_FILE,index=0)
## to save data 
write.zoo(ts.xts,PATH_CSV_FILE)
like image 178
agstudy Avatar answered Nov 09 '22 07:11

agstudy