Note that there are some explanatory texts on larger screens.

plurals
  1. POissue converting python pandas DataFrame to R dataframe for use with rpy2
    text
    copied!<p>I am having trouble converting a pandas <code>DataFrame</code> in Python to an R object, for future use in R using rpy2.</p> <p>The new pandas release 0.8.0 (released a few weeks ago) has a function to convert pandas DataFrames to R DataFrames. The problem is in converting the first column of my pandas DataFrame, which consists of python datetime objects (successively, in a time series). The conversion into an R dataframe returns an StrVector of the dates and times, rather than a vector of R datetime-type objects which I believe are called "POSIXct" objects.</p> <p>I know the command to convert a string of the type returned to a POSIXct, using the command "as.POSIXct('yyyy-mm-dd hh:mm:ss')". Unfortunately I have not been able to figure out the way to convert all these strings in the StrVector to POSIXct using python and rpy2. The dates need to be in the POSIXct format to be used with the TTR library in R. Below is the relevant python code:</p> <pre><code>import pandas from pandas import * import pandas.rpy.common as com import rpy2.robjects as robjects r = robjects.r r.library('TTR') #library contains the function ADX, to be used later dataframe = read_csv('file_name', parse_dates = [0], names = ['Date','Col1','Col2','Col3'] #command makes 1st column into datetime.datetime object r_dataframe = com.convert_to_r_dataframe(dataframe) ADX = r['ADX'] #creating a name for an R function in python adx = ADX(r_dataframe) #will not work because the dates in r_dataframe are in a StrVector </code></pre> <p>Further I do not believe that the StrVector can be iterated through to convert each object to a POSIXct object individually, due to the definition of a StrVector. Maybe there is a way to cast a StrVector to a generic one?</p> <p>Any help/insight into this matter is greatly appreciated. I am a novice programmer and have been working on this for a couple hours now to no avail.</p> <p>Thank you!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload