5

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? =)

Andrey Shokhin
  • 11,250
  • 1
  • 16
  • 15

3 Answers3

1

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)
agstudy
  • 119,832
  • 17
  • 199
  • 261
1

The easiest might just be to use the R function ts() in a call corresponding to your pandas.date_range() call.

from rpy2.robjects.packages import importr
stats = importr('stats')
from rpy2.robjects.vectors import IntVector
# The time series created in the question is:
# pd.date_range(start='2000-01-01', periods=100)
stats.ts(IntVector(range(100)), start=IntVector((2000, 1, 1)))
lgautier
  • 11,363
  • 29
  • 42
0

Inspired by the answers given here already, I created a small function for conversion of an existing Pandas time series towards an R time series. It might be usefull to more of you. Feel free to further improve and edit my contribution.

def pd_ts2r_ts(pd_ts):
    '''Pandas timeseries (pd_ts) to R timeseries (r_ts) conversion
    '''
    from rpy2.robjects.vectors import IntVector,FloatVector
    rstats = rpackages.importr('stats')
    r_start = IntVector((pd_ts.index[0].year,pd_ts.index[0].month,pd_ts.index[0].day))
    r_end = IntVector((pd_ts.index[-1].year,pd_ts.index[-1].month,pd_ts.index[-1].day)) 
    freq_pandas2r_ts = {
        # A dictionary for converting pandas.Series frequencies into R ts frequencies
        'D' : 365, # is this correct, how about leap-years?
        'M' : 12,
        'Y' : 1,
    }
    r_freq = freq_pandas2r_ts[pd_ts.index.freqstr]
    result = rstats.ts(FloatVector(pd_ts.values),start=r_start,end=r_end,frequency=r_freq)
    return result
Basileios
  • 285
  • 2
  • 6