def test_copy_metadata(self): """ Verify ``Series`` methods that copy will keep SAS metadata. """ v = xport.Variable( name='A', label='Alpha', format='$CHAR4.', dtype='string', ) self.compare_metadata(v.copy(), v) self.compare_metadata(v.append(xport.Variable(['1'])), v)
def test_vtype(self): character = ['string', 'object'] numeric = ['float', 'int', 'bool'] invalid = ['datetime64[ns]'] for dtype in character: v = xport.Variable(dtype=dtype) assert v.vtype == xport.VariableType.CHARACTER for dtype in numeric: v = xport.Variable(dtype=dtype) assert v.vtype == xport.VariableType.NUMERIC for dtype in invalid: with pytest.raises(TypeError): xport.Variable(dtype=dtype)
def test_init(self): """ Verify initialization. """ v = xport.Variable(dtype='float') for name in v._metadata: getattr(v, name) # Does not raise an error.
def test_format(self): v = xport.Variable(dtype='object') v.format = value = '$CHAR10.' assert v.format == xport.Format.from_spec(value) with pytest.raises(ValueError): v.format = '' with pytest.raises(ValueError): v.format = '$abcdefghi1.'
def test_troublesome_text(self): """ Some text patterns have been trouble in the past. """ trouble = xport.Variable(["'<>"], dtype='string') dataset = xport.Dataset({'a': trouble}, name='trouble') library = xport.Library(dataset) with pytest.warns(UserWarning, match=r'Converting column dtypes'): assert self.dump_and_load(library) == library
def test_dumps_name_and_label_length_validation(self): """ Verify variable and dataset name and label length. """ # Names must be <= 8 characters. # Labels must be <= 40 characters. # SAS v8 Transport Files allow longer labels. invalid = [ xport.Library(xport.Dataset(), sas_version='a' * 9), xport.Library(xport.Dataset(name='a' * 9)), xport.Library(xport.Dataset(label='a' * 41)), xport.Library(xport.Dataset({'a' * 9: [1.0]})), xport.Library( xport.Dataset({'a': xport.Variable([1.0], label='a' * 41)})), ] for bad_metadata in invalid: with pytest.raises(ValueError): xport.v56.dumps(bad_metadata)
def test_contents(self): """ Verify variables metadata summary. """ ds = xport.Dataset( data={ 'a': [1], 'b': xport.Variable(['x'], label='Beta'), 'c': [None], }, name='EXAMPLE', label='Example', ) ds['a'].vtype = xport.VariableType.NUMERIC ds['b'].vtype = xport.VariableType.CHARACTER got = ds.contents assert list(got.index) == [1, 2, 3] assert list(got['Label']) == ['', 'Beta', ''] assert list(got['Type']) == ['Numeric', 'Character', '']
def test_copy_metadata(self): """ Verify ``DataFrame`` methods that copy will keep SAS metadata. """ ds = xport.Dataset( data={ 'a': [1], 'b': xport.Variable(['x'], label='Beta') }, name='EXAMPLE', label='Example', ) self.compare_metadata(ds.copy(), ds) self.compare_metadata( ds.append(pd.DataFrame({ 'a': [2], 'b': ['y'], })), ds, ) self.compare_metadata(pd.concat([ds, ds]), ds)
def from_header(cls, header): """ Create an empty ``Member`` with metadata from a ``MemberHeader``. """ variables = { namestr.name: xport.Variable( dtype='float' if namestr.vtype == xport.VariableType.NUMERIC else 'string', name=namestr.name, label=namestr.label, vtype=namestr.vtype, width=namestr.length, format=namestr.format, informat=namestr.informat, ) for namestr in header.values() } public = (name.lstrip('_') for name in cls._metadata) self = cls(variables, **{name: getattr(header, name) for name in public}) return self
def test_informat(self): v = xport.Variable(dtype='object') v.informat = value = '10.2' assert v.informat == xport.Informat.from_spec(value) with pytest.raises(ValueError): v.informat = '1.2.3'