How to use the swat.dataframe.SASDataFrame function in swat

To help you get started, we’ve selected a few swat examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sassoftware / python-swat / swat / cas / datamsghandlers.py View on Github external
if width <= 4:
                            out = (4, 'NUMERIC', 'INT32')
                        else:
                            out = (8, 'NUMERIC', 'INT64')
                    elif dtype in ['M', 'm']:
                        out = (8, 'NUMERIC', 'DATETIME')
                    if out is not None:
                        return out
            raise TypeError('%s is an unrecognized data type' % typ.str)

        # Empty reader iterator
        self.reader = iter([])

        # Add support for SASDataFrame metadata
        colinfo = {}
        if isinstance(data, SASDataFrame):
            # label = data.label
            colinfo = data.colinfo

        elif isinstance(data, pd.DataFrame):
            pass

        # Add support for chunked dataframes
        else:
            self.reader = iter(data)
            data = next(self.reader)

        if data.index.name is None:
            data = data.reset_index(drop=True)
        else:
            data = data.reset_index()
github sassoftware / python-swat / swat / functions.py View on Github external
:class:`SASDataFrame`
        If first input is a SASDataFrame
    :class:`DataFrame`
        If first input is a pandas.DataFrame
    :class:`Series`
        If first input is a pandas.Series

    '''
    objs = [x for x in objs if x is not None]
    if not objs:
        raise ValueError('There are no non-None objects in the given sequence')

    if isinstance(objs[0], table.CASTable):
        return table.concat(objs, **kwargs)

    if isinstance(objs[0], dataframe.SASDataFrame):
        return dataframe.concat(objs, **kwargs)

    return pd.concat(objs, **kwargs)
github sassoftware / python-swat / swat / dataframe.py View on Github external
title = proto.title
    label = proto.label
    name = proto.name
    formatter = proto.formatter

    attrs = {}
    colinfo = {}
    columns = collections.OrderedDict()
    for item in objs:
        attrs.update(item.attrs)
        colinfo.update(item.colinfo)
        for col in item.columns:
            columns[col] = True

    return SASDataFrame(pd.concat(objs, **kwargs), title=title, label=label,
                        name=name, attrs=attrs, colinfo=colinfo,
                        formatter=formatter)[list(columns.keys())]
github sassoftware / python-swat / swat / dataframe.py View on Github external
def __getitem__(self, *args, **kwargs):
        '''
        Retrieve items from a SASDataFrame

        See Also
        --------
        :meth:`pandas.DataFrame.__getitem__`

        '''
        result = super(SASDataFrame, self).__getitem__(*args, **kwargs)

        if isinstance(result, SASDataFrame):

            # Copy metadata fields
            for name in self._metadata:
                selfattr = getattr(self, name, None)
                if isinstance(selfattr, dict):
                    selfattr = selfattr.copy()
                object.__setattr__(result, name, selfattr)

        return result
github sassoftware / python-swat / swat / dataframe.py View on Github external
----------
        k : string
            The key to remove.

        See Also
        --------
        :meth:`pandas.DataFrame.pop`

        Returns
        -------
        any
            The value stored in `k`.

        '''
        self.colinfo.pop(k, None)
        return super(SASDataFrame, self).pop(k, *args)
github sassoftware / python-swat / swat / dataframe.py View on Github external
def _constructor(self):
        '''
        Constructor used by DataFrame when returning a new DataFrame from an operation

        '''
        return SASDataFrame
github sassoftware / python-swat / swat / cas / results.py View on Github external
by_re = re.compile(r'^(ByGroupSet%s\.)?ByGroup\d+\.%s$' % (set, re.escape(name)))
        for key, value in six.iteritems(self):
            if by_re.match(key):
                out.append(value)

        if concat and out:
            if isinstance(out[0], SASDataFrame):
                attrs = out[0].attrs.copy()
                attrs.pop('ByGroup', None)
                attrs.pop('ByGroupIndex', None)
                i = 1
                while 'ByVar%d' % i in attrs:
                    attrs.pop('ByVar%dValue' % i, None)
                    attrs.pop('ByVar%dValueFormatted' % i, None)
                    i = i + 1
                return SASDataFrame(pd.concat(out, **kwargs), name=out[0].name,
                                    label=out[0].label, title=out[0].title,
                                    formatter=out[0].formatter,
                                    attrs=attrs, colinfo=out[0].colinfo.copy())
            return pd.concat(out, **kwargs)

        return out
github sassoftware / python-swat / swat / cas / transformers.py View on Github external
# Numpy doesn't like unicode column names in Python 2, so map them to utf-8
    dtypes = [(a2n(x[0], 'utf-8'), x[1]) for x in dtypes]

    # Create a np.array and fill it
    kwargs['data'] = np.array(_sw_table.toTuples(a2n(
                         get_option('encoding_errors'), 'utf-8'),
                         casdt.cas2python_datetime, casdt.cas2python_date,
                         casdt.cas2python_time),
                         dtype=dtypes)

    # Short circuit for numpy arrays
#   if tformat == 'numpy_array':
#       return kwargs['data']

    cdf = SASDataFrame(**kwargs)

    # Map column names back to unicode in pandas
    cdf.columns = [a2u(x[0], 'utf-8') for x in dtypes]

    # Apply int missing values
    if intmiss:
        cdf = cdf.replace(to_replace=intmiss)

    # Apply mimetype transformations
    if mimetypes:
        from io import BytesIO
        Image = True
        for key, value in mimetypes.items():
            if value.startswith('image/'):
                if Image is True:
                    Image = None
github sassoftware / python-swat / swat / cas / results.py View on Github external
bykey = []

        def set_bykey(attrs):
            ''' Locate By variable keys '''
            if bykey:
                return bykey
            i = 1
            while True:
                if 'ByVar%s' % i not in attrs:
                    break
                bykey.append(attrs['ByVar%s' % i])
                i = i + 1
            return bykey

        for key, value in six.iteritems(self):
            if not isinstance(value, SASDataFrame):
                continue
            if not re.match(r'^ByGroup\d+\.', key):
                continue
            attrs = value.attrs
            match = True
            i = 1
            for byname in set_bykey(attrs):
                if kwargs:
                    if attrs['ByVar%sValue' % i] != kwargs[byname] and \
                            attrs['ByVar%sValueFormatted' % i] != kwargs[byname]:
                        match = False
                        break
                elif name:
                    try:
                        if attrs['ByVar%sValue' % i] != name[i - 1] and \
                                attrs['ByVar%sValueFormatted' % i] != name[i - 1]: