Following on from the question here:
I'm trying to create the series by hand here using Rpy2
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
import pandas.rpy.common as com
pa = importr("pa")
ro.r("data(jan)")
jan = com.load_data('jan')
jan_r = com.convert_to_r_dataframe(jan)
name = ro.StrVector([str(i) for i in jan['name']])
sector = ro.StrVector([str(i) for i in jan['sector']])
date = ro.StrVector([str(i) for i in jan['date']])
and I get at date number of 14610
in the date field representing 2010-01-01
which I suspect is a 1970-01-01
origin. I can't find anything in the datetime
module that will allow me to change the origin for the date however so I don't know how to reset it.
My questions:
- Is the origin for the R sourced date
1970-01-01
? - Is there a way to set an origin and covert to a
datetime.datetime
object in python? - Am I missing something more obvious here?
Thanks
From
?Date
:Well suspected.
Python datetime docs show several ways of constructing a date.
You can use
datetime.date(year, month, day)
syntax, where those values can be retrieved from the R dates usingyear(x)
,month(x)
andmday(x)
, wherex
represents your date vector.You can use
date.fromtimestamp(timestamp)
syntax, where the timestamps can be retrieved from the R dates usingformat(x)
.The
date.fromordinal(ordinal)
documentation returns:So presumably your problem is that you are passing dates as numbers which R calculates as days from 1st Jan 1970, and python assumes are from 1st Jan 0001.