Skip to content

Commit 3950684

Browse files
Donald Curtiswesm
Donald Curtis
authored andcommitted
ENH: Support conversions on datetime columns
Check to see if the values we're looking at are numpy datetime64 objects. If they are, we can assume we're looking at a Timestamp object, which subclasses Python's datetime class, and we return a struct_time which is understood by rpy2's POSIXct class.
1 parent ff21355 commit 3950684

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

pandas/rpy/common.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,40 @@ def convert_robj(obj, use_pandas=True):
197197

198198
raise Exception('Do not know what to do with %s object' % type(obj))
199199

200+
201+
def convert_to_r_posixct(obj):
202+
"""
203+
Convert DatetimeIndex or np.datetime array to R POSIXct using
204+
m8[s] format.
205+
206+
Parameters
207+
----------
208+
obj : source pandas object (one of [DatetimeIndex, np.datetime])
209+
210+
Returns
211+
-------
212+
An R POSIXct vector (rpy2.robjects.vectors.POSIXct)
213+
214+
"""
215+
import time
216+
from rpy2.rinterface import StrSexpVector
217+
218+
# convert m8[ns] to m8[s]
219+
vals = robj.vectors.FloatSexpVector(obj.values.view('i8') / 1E9)
220+
as_posixct = robj.baseenv.get('as.POSIXct')
221+
origin = StrSexpVector([time.strftime("%Y-%m-%d",
222+
time.gmtime(0)),])
223+
224+
# We will be sending ints as UTC
225+
tz = obj.tz.zone if hasattr(obj, 'tz') and hasattr(obj.tz, 'zone') else 'UTC'
226+
tz = StrSexpVector([tz])
227+
utc_tz = StrSexpVector(['UTC'])
228+
229+
posixct = as_posixct(vals, origin=origin, tz=utc_tz)
230+
posixct.do_slot_assign('tzone', tz)
231+
return posixct
232+
233+
200234
VECTOR_TYPES = {np.float64: robj.FloatVector,
201235
np.float32: robj.FloatVector,
202236
np.float: robj.FloatVector,
@@ -242,14 +276,18 @@ def convert_to_r_dataframe(df, strings_as_factors=False):
242276
for column in df:
243277
value = df[column]
244278
value_type = value.dtype.type
245-
value = [item if pd.notnull(item) else NA_TYPES[value_type]
246-
for item in value]
247279

248-
value = VECTOR_TYPES[value_type](value)
280+
if value_type == np.datetime64:
281+
value = convert_to_r_posixct(value)
282+
else:
283+
value = [item if pd.notnull(item) else NA_TYPES[value_type]
284+
for item in value]
285+
286+
value = VECTOR_TYPES[value_type](value)
249287

250-
if not strings_as_factors:
251-
I = robj.baseenv.get("I")
252-
value = I(value)
288+
if not strings_as_factors:
289+
I = robj.baseenv.get("I")
290+
value = I(value)
253291

254292
columns[column] = value
255293

0 commit comments

Comments
 (0)