def test_unsignednumpyint_to_rint(): values = (1, 2, 3) a8 = numpy.array(values, dtype='uint8') v = rpyn.unsignednumpyint_to_rint(a8) assert values == tuple(v) a64 = numpy.array(values, dtype='uint64') with pytest.raises(ValueError): rpyn.unsignednumpyint_to_rint(a64)
def test_unsignednumpyint_to_rint(dtype): values = (1, 2, 3) a = numpy.array(values, dtype=dtype) v = rpyn.unsignednumpyint_to_rint(a) assert values == tuple(v)
def test_unsignednumpyint_to_rint_error(dtype): values = (1, 2, 3) a = numpy.array(values, dtype=dtype) with pytest.raises(ValueError): rpyn.unsignednumpyint_to_rint(a)
def py2rpy_pandasseries(obj): if obj.dtype.name == 'O': warnings.warn('Element "%s" is of dtype "O" and converted ' 'to R vector of strings.' % obj.name) res = StrVector(obj) elif obj.dtype.name == 'category': res = py2rpy_categoryseries(obj) res = FactorVector(res) elif is_datetime64_any_dtype(obj.dtype): # time series tzname = obj.dt.tz.zone if obj.dt.tz else '' d = [ IntVector([x.year for x in obj]), IntVector([x.month for x in obj]), IntVector([x.day for x in obj]), IntVector([x.hour for x in obj]), IntVector([x.minute for x in obj]), FloatSexpVector([x.second + x.microsecond * 1e-6 for x in obj]) ] res = ISOdatetime(*d, tz=StrSexpVector([tzname])) # TODO: can the POSIXct be created from the POSIXct constructor ? # (is '<M8[ns]' mapping to Python datetime.datetime ?) res = POSIXct(res) elif (obj.dtype == dt_O_type): homogeneous_type = None for x in obj.values: if x is None: continue if homogeneous_type is None: homogeneous_type = type(x) continue if type(x) is not homogeneous_type: raise ValueError('Series can only be of one type, or None.') # TODO: Could this be merged with obj.type.name == 'O' case above ? res = { int: IntVector, bool: BoolVector, None: BoolVector, str: StrVector, bytes: numpy2ri.converter.py2rpy.registry[numpy.ndarray] }[homogeneous_type](obj) elif obj.dtype.name in integer_array_types: if not obj.dtype.numpy_dtype.isnative: raise (ValueError('Cannot pass numpy arrays with non-native byte' ' orders at the moment.')) if obj.dtype.kind == 'i': res = numpy2ri._numpyarray_to_r(obj, IntVector) elif obj.dtype.kind == 'u': res = numpy2ri.unsignednumpyint_to_rint(obj) else: raise (ValueError('Unknown pandas dtype "%s".' % str(obj.dtype))) if len(obj.shape) == 1: if obj.dtype != dt_O_type: # force into an R vector res = as_vector(res) else: # converted as a numpy array func = numpy2ri.converter.py2rpy.registry[numpy.ndarray] # current conversion as performed by numpy res = func(obj) if len(obj.shape) == 1: if (obj.dtype != dt_O_type): # force into an R vector res = as_vector(res) # "index" is equivalent to "names" in R if obj.ndim == 1: res.do_slot_assign('names', StrVector(tuple(str(x) for x in obj.index))) else: res.do_slot_assign('dimnames', SexpVector(conversion.py2rpy(obj.index))) return res