How to use the swat.cas.utils.datetime 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 / transformers.py View on Github external
j = set_list_value(_sw_sublist, j, None, v)
            i = i + 1
        elif isinstance(item, (dict_types, ParamManager)):
            if isinstance(item, ParamManager):
                item = item.to_params()
            _sw_sublist = errorcheck(_sw_values.createListAt(
                                     i, key, len(item)), _sw_values)
            j = 0
            for k, v in six.iteritems(item):
                if isinstance(k, (text_types, binary_types)):
                    j = set_list_value(_sw_sublist, j, k, v)
                else:
                    j = set_list_value(_sw_sublist, j, None, v)
            i = i + 1
        elif isinstance(item, datetime.datetime):
            errorcheck(_sw_values.setDateTime(i, key, casdt.python2cas_datetime(item)),
                       _sw_values)
            i = i + 1
        elif isinstance(item, datetime.date):
            errorcheck(_sw_values.setDate(i, key, casdt.python2cas_date(item)),
                       _sw_values)
            i = i + 1
        elif isinstance(item, datetime.time):
            errorcheck(_sw_values.setTime(i, key, casdt.python2cas_time(item)),
                       _sw_values)
            i = i + 1

        return i
github sassoftware / python-swat / swat / cas / transformers.py View on Github external
Image = None
                    try:
                        from PIL import Image
                    except ImportError:
                        warnings.warn('The PIL or Pillow package is required '
                                      'to convert bytes to Image objects',
                                      RuntimeWarning)
                if Image is None:
                    continue
                cdf[key] = cdf[key].map(lambda x: Image.open(BytesIO(x)))

    # Apply date / datetime transformations
    for item in dates:
        cdf[item] = cdf[item].apply(casdt.sas2python_date)
    for item in datetimes:
        cdf[item] = cdf[item].apply(casdt.sas2python_datetime)

    # Check for By group information
    optbycol = get_option('cas.dataset.bygroup_columns')
    optbyidx = get_option('cas.dataset.bygroup_as_index')
    optbysfx = get_option('cas.dataset.bygroup_formatted_suffix')
    optbycolsfx = get_option('cas.dataset.bygroup_collision_suffix')
    cdf = cdf.reshape_bygroups(bygroup_columns=optbycol,
                               bygroup_as_index=optbyidx,
                               bygroup_formatted_suffix=optbysfx,
                               bygroup_collision_suffix=optbycolsfx)

    # Add an index as needed
    index = get_option('cas.dataset.index_name')
    if index:
        if not isinstance(index, (list, tuple, set)):
            index = [index]
github sassoftware / python-swat / swat / cas / transformers.py View on Github external
SASDataFrame representation of SWIG CASTable
    DataFrame object
       Pandas DataFrame representation of SWIG CASTable
    dict or list
       Any variant of the Pandas DataFrame.to_dict() results
    tuple
       A tuple of tuples of the data values only

    '''
    tformat = get_option('cas.dataset.format')
    needattrs = (tformat == 'dataframe:sas')

    # We can short circuit right away if they just want tuples
    if tformat.startswith('tuple'):
        return _sw_table.toTuples(a2n(get_option('encoding_errors'), 'utf-8'),
                                  casdt.cas2python_datetime,
                                  casdt.cas2python_date,
                                  casdt.cas2python_time)

    kwargs = {}

    check = errorcheck
    if connection is not None:
        kwargs['formatter'] = connection.SASFormatter()
    else:
        kwargs['formatter'] = SASFormatter(soptions=soptions)
    kwargs['name'] = check(a2u(_sw_table.getName(), 'utf-8'), _sw_table)
    kwargs['label'] = check(a2u(_sw_table.getLabel(), 'utf-8'), _sw_table)
    kwargs['title'] = check(a2u(_sw_table.getTitle(), 'utf-8'), _sw_table)

    # get table attributes
    attrs = {}
github sassoftware / python-swat / swat / cas / transformers.py View on Github external
if value.startswith('image/'):
                if Image is True:
                    Image = None
                    try:
                        from PIL import Image
                    except ImportError:
                        warnings.warn('The PIL or Pillow package is required '
                                      'to convert bytes to Image objects',
                                      RuntimeWarning)
                if Image is None:
                    continue
                cdf[key] = cdf[key].map(lambda x: Image.open(BytesIO(x)))

    # Apply date / datetime transformations
    for item in dates:
        cdf[item] = cdf[item].apply(casdt.sas2python_date)
    for item in datetimes:
        cdf[item] = cdf[item].apply(casdt.sas2python_datetime)

    # Check for By group information
    optbycol = get_option('cas.dataset.bygroup_columns')
    optbyidx = get_option('cas.dataset.bygroup_as_index')
    optbysfx = get_option('cas.dataset.bygroup_formatted_suffix')
    optbycolsfx = get_option('cas.dataset.bygroup_collision_suffix')
    cdf = cdf.reshape_bygroups(bygroup_columns=optbycol,
                               bygroup_as_index=optbyidx,
                               bygroup_formatted_suffix=optbysfx,
                               bygroup_collision_suffix=optbycolsfx)

    # Add an index as needed
    index = get_option('cas.dataset.index_name')
    if index:
github sassoftware / python-swat / swat / cas / transformers.py View on Github external
----------
    _sw_value : SWIG CASValue object
       Object to convert to Python
    soptions : string
       soptions of connection object

    Returns
    -------
    any
       Python representation of CASValue

    '''
    return _sw_value.toPython(_sw_value, soptions,
                              a2n(get_option('encoding_errors'), 'utf-8'),
                              connection, ctb2tabular,
                              base64.b64decode, casdt.cas2python_datetime,
                              casdt.cas2python_date, casdt.cas2python_time)
#   return CAS2PY[errorcheck(_sw_value.getType(),
github sassoftware / python-swat / swat / cas / utils / datetime.py View on Github external
Parameters
    ----------
    cts : int
        CAS timestamp.

    Examples
    --------
    >>> cas2python_timestamp(315662400000000)
    datetime.datetime(1970, 1, 1, 12, 0)

    Returns
    -------
    :class:`datetime.datetime`

    '''
    return CAS_EPOCH + datetime.timedelta(microseconds=cts)
github sassoftware / python-swat / swat / formatter.py View on Github external
int32(width)), a2n('utf-8')),
                                 self._sw_formatter)
            except SWATError:
                out = value
        # TODO: Should binary types ever get here?
        elif isinstance(value, binary_types):
            out = errorcheck(a2u(self._sw_formatter.formatString(
                                 a2n(value), a2n(sasfmt), int32(width)), a2n('utf-8')),
                             self._sw_formatter)
        elif isinstance(value, bool_types):
            out = errorcheck(a2u(self._sw_formatter.formatInt32(
                                 int32(value), a2n(sasfmt), int32(width)), a2n('utf-8')),
                             self._sw_formatter)
        elif isinstance(value, (datetime.datetime, Timestamp)):
            out = errorcheck(a2u(self._sw_formatter.formatDouble(
                                 utils.datetime.python2sas_datetime(value),
                                 a2n(sasfmt), int32(width)),
                                 a2n('utf-8')),
                             self._sw_formatter)
        elif isinstance(value, datetime.date):
            out = errorcheck(a2u(self._sw_formatter.formatDouble(
                                 utils.datetime.python2sas_date(value),
                                 a2n(sasfmt), int32(width)),
                                 a2n('utf-8')),
                             self._sw_formatter)
        elif isinstance(value, datetime.time):
            out = errorcheck(a2u(self._sw_formatter.formatDouble(
                                 utils.datetime.python2sas_time(value),
                                 a2n(sasfmt), int32(width)),
                                 a2n('utf-8')),
                             self._sw_formatter)
        elif value is None:
github sassoftware / python-swat / swat / cas / utils / datetime.py View on Github external
'''
Datetime utilities for interfacing with CAS

'''

from __future__ import print_function, division, absolute_import, unicode_literals

import datetime
import time
import numpy as np
import pandas as pd
from ...utils.compat import int64, int32


CAS_EPOCH = datetime.datetime(month=1, day=1, year=1960)


# str to CAS/SAS


def str2cas_timestamp(dts):
    '''
    Convert a string to a CAS timestamp

    Parameters
    ----------
    dts : string
        The string representation of a timestamp.

    Examples
    --------