Thursday, 15 January 2015

Can't convert R date ordinal to Python accurately -



Can't convert R date ordinal to Python accurately -

following on question here:

i'm trying create series hand here using rpy2

import rpy2.robjects ro rpy2.robjects.packages import importr import pandas.rpy.common 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) in jan['name']]) sector = ro.strvector([str(i) in jan['sector']]) date = ro.strvector([str(i) in jan['date']])

and @ date number of 14610 in date field representing 2010-01-01 suspect 1970-01-01 origin. can't find in datetime module allow me alter origin date don't know how reset it.

my questions:

is origin r sourced date 1970-01-01? is there way set origin , covert datetime.datetime object in python? am missing more obvious here?

thanks

is origin r sourced date 1970-01-01?

from ?date:

dates represented number of days since 1970-01-01, negative values before dates.

i @ date number of 14610 in date field representing 2010-01-01 suspect 1970-01-01 origin.

well suspected.

as.date(14610, origin = "1970-01-01") ## [1] "2010-01-01"

is there way set origin , covert datetime.datetime object in python?

python datetime docs show several ways of constructing date.

you can utilize datetime.date(year, month, day) syntax, values can retrieved r dates using year(x), month(x) , mday(x), x represents date vector.

you can utilize date.fromtimestamp(timestamp) syntax, timestamps can retrieved r dates using format(x).

the date.fromordinal(ordinal) documentation returns:

the date corresponding gregorian ordinal, jan 1 of year 1 has ordinal 1

so presumably problem passing dates numbers r calculates days 1st jan 1970, , python assumes 1st jan 0001.

python r datetime rpy2

No comments:

Post a Comment