Пример #1
0
    def __init__(self, obj, stringsasfactor=False, checknames=False):
        """ Create a new data frame.

        :param obj: object inheriting from rpy2.rinterface.SexpVector,
            or inheriting from TaggedList or a mapping name -> value
        :param stringsasfactors: Boolean indicating whether vectors
            of strings should be turned to vectors. Note that factors
            will not be turned to string vectors.
        :param checknames: Boolean indicating whether column names
            should be transformed to R syntactically valid names.
        """
        if isinstance(obj, rinterface.ListSexpVector):
            if obj.typeof != rinterface.RTYPES.VECSXP:
                raise ValueError(
                    "obj should of typeof RTYPES.VECSXP "
                    " (and we get %s)" % rinterface.RTYPES(obj.typeof)
                )
            if (
                    self._is_list(obj)[0] or
                    globalenv_ri.find('inherits')(
                        obj, self._dataframe_name
                    )[0]
            ):
                # TODO: is it really a good idea to pass R lists
                # to the constructor ?
                super().__init__(obj)
                return
            else:
                raise ValueError(
                    "When passing R objects to build a DataFrame, "
                    "the R object must be a list or inherit from "
                    "the R class 'data.frame'."
                )
        elif isinstance(obj, rlc.TaggedList):
            kv = [(k, conversion.py2rpy(v)) for k, v in obj.items()]
        else:
            try:
                kv = [(str(k), conversion.py2rpy(v)) for k, v in obj.items()]
            except AttributeError:
                raise ValueError(
                    'obj can only be'
                    'an instance of rpy2.rinterface.ListSexpVector, '
                    'an instance of TaggedList, '
                    'or an objects with a methods items() that returns '
                    '(key, value) pairs '
                    '(such a Python dict, rpy2.rlike.container OrdDict).')

        # Check if there is a conflicting column name
        if 'stringsAsFactors' in (k for k, v in kv):
            warnings.warn('The column name "stringsAsFactors" is '
                          'conflicting with named parameter '
                          'in underlying R function "data.frame()".')
        else:
            kv.extend((('stringsAsFactors', stringsasfactor),
                       ('check.names', checknames)))

        # Call R's data frame constructor
        kv = tuple(kv)
        df = baseenv_ri.find("data.frame").rcall(kv, globalenv_ri)
        super().__init__(df)
Пример #2
0
    def __init__(self, obj, stringsasfactor=False):
        """ Create a new data frame.

        :param obj: object inheriting from rpy2.rinterface.SexpVector,
                    or inheriting from TaggedList
                    or a mapping name -> value
        :param stringsasfactors: Boolean indicating whether vectors
                    of strings should be turned to vectors. Note
                    that factors will not be turned to string vectors.
        """
        if isinstance(obj, rinterface.ListSexpVector):
            if obj.typeof != rinterface.RTYPES.VECSXP:
                raise ValueError("obj should of typeof RTYPES.VECSXP"+\
                                     " (and we get %s)" % rinterface.RTYPES(obj.typeof))
            if self._is_list(obj)[0] or \
                    globalenv_ri.find('inherits')(obj, self._dataframe_name)[0]:
                #FIXME: is it really a good idea to pass R lists
                # to the constructor ?
                super().__init__(obj)
                return
            else:
                raise ValueError(
                "When passing R objects to build a DataFrame," +\
                " the R object must be a list or inherit from" +\
                " the R class 'data.frame'")
        elif isinstance(obj, rlc.TaggedList):
            kv = [(k, conversion.py2rpy(v)) for k, v in obj.items()]
        else:
            try:
                kv = [(str(k), conversion.py2rpy(obj[k])) for k in obj]
            except TypeError:
                raise ValueError(
                    "obj can be either " +
                    "an instance of an iter-able class" +
                    "(such a Python dict, rpy2.rlike.container OrdDict" +
                    " or an instance of rpy2.rinterface.SexpVector" +
                    " of type VECSXP")

        # Check if there is a conflicting column name
        if 'stringsAsFactors' in (k for k, v in kv):
            warnings.warn('The column name "stringsAsFactors" is '
                          'conflicting with named parameter '
                          'in underlying R function "data.frame()".')
        else:
            kv.append(('stringsAsFactors', stringsasfactor))

        # Call R's data frame constructor
        kv = tuple(kv)
        df = baseenv_ri.find("data.frame").rcall(kv, globalenv_ri)
        super().__init__(df)