quick pass on pandas <-> xts bridge.

124 views
Skip to first unread message

Dale Jung

unread,
Aug 7, 2012, 1:20:00 PM8/7/12
to pyd...@googlegroups.com
https://gist.github.com/3287286

Put this together last night. It will convert pandas DataFrame to an xts object if the index is Datetime. So something like:

rdaily = r['dailyReturn']
rdaily(df) # calling R function with DataFrame within python.

Will work without converting the df explicitly. Also means that 

r.assign('xts_object', df) # properly converts df to xts
assert isinstance(r('dailyReturn(xts_object)'), DataFrame) # properly converts xts output to DataFrame on the python side. 

I don't have a lot of test cases since I'm new to R. So caveat emptor. Just wanted to share and get suggestions if I'm approaching it wrong.

Dale Jung

unread,
Aug 11, 2012, 10:51:43 AM8/11/12
to pyd...@googlegroups.com
Update on more poking around:

Added a DatetimeIndex <-> POSIXct bridge. 
Changed the index conversion to send m8[s] ints instead of converting to strings first. 
Should respect timezone now. Will always default to UTC when sending over to R because there is no naive datetime in R.

Starting to think that the automatic conversion shouldn't happen. Maybe grabbing a xts from R should return an XTS object instead of a dataframe. Then have a to_df method on XTS.

Otherwise there might be too much random DataFrame and DatetimeIndex creations. 

Same thing for going from DataFrame -> XTS. It's nice to pass in a pandas DataFrame into R functions, but if pass it in twice it will recreate the XTS object twice. 
Reply all
Reply to author
Forward
0 new messages