Skip to content

Commit 459c261

Browse files
author
Kevin D Smith
committed
Add integer tests and options for setting missing values in integer-based columns
1 parent c9080a1 commit 459c261

File tree

6 files changed

+45
-4
lines changed

6 files changed

+45
-4
lines changed

swat/cas/datamsghandlers.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import datetime
2929
import numpy as np
3030
import pandas as pd
31+
import warnings
3132
from .utils.datetime import (str2cas_timestamp, str2cas_datetime, str2cas_date,
3233
str2cas_time, str2sas_timestamp, str2sas_datetime,
3334
str2sas_date, str2sas_time, cas2python_timestamp,
@@ -40,6 +41,7 @@
4041
python2sas_date, python2sas_time, python2cas_timestamp,
4142
python2cas_datetime, python2cas_date, python2cas_time)
4243
from .. import clib
44+
from ..config import get_option
4345
from ..clib import errorcheck
4446
from ..exceptions import SWATError
4547
from ..utils.compat import a2n, text_types, binary_types, int32, int64, float64
@@ -333,6 +335,13 @@ def get(arr, idx, default=0):
333335
errorcheck(self._sw_databuffer.setString(row, offset, a2n('')),
334336
self._sw_databuffer)
335337
elif vrtype == 'NUMERIC' and vtype in ['INT32', 'DATE']:
338+
if pd.isnull(value):
339+
value = get_option('cas.%s_missing' % vtype.lower())
340+
warnings.warn(("Missing value found in 32-bit integer-based column '%s'.\n" %
341+
v['name']) +
342+
("Substituting cas.%s_missing option value (%s)." %
343+
(vtype.lower(), value)),
344+
RuntimeWarning)
336345
if length > 4:
337346
for i in range(int64(length / 4)):
338347
errorcheck(self._sw_databuffer.setInt32(row, offset + (i * 4),
@@ -343,6 +352,13 @@ def get(arr, idx, default=0):
343352
int32(transformer(value))),
344353
self._sw_databuffer)
345354
elif vrtype == 'NUMERIC' and vtype in ['INT64', 'DATETIME', 'TIME']:
355+
if pd.isnull(value):
356+
value = get_option('cas.%s_missing' % vtype.lower())
357+
warnings.warn(("Missing value found in 64-bit integer-based column '%s'.\n"
358+
% v['name']) +
359+
("Substituting cas.%s_missing option value (%s)." %
360+
(vtype.lower(), value)),
361+
RuntimeWarning)
346362
if length > 8:
347363
for i in range(int64(length / 8)):
348364
errorcheck(self._sw_databuffer.setInt64(row, offset + (i * 8),

swat/config.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,25 @@ def check_severity(sev):
136136
'1 would raise exceptions on warnings. 2 would raise exceptions\n' +
137137
'on errors.')
138138

139+
#
140+
# Integer missing value substitutions
141+
#
142+
143+
register_option('cas.int64_missing', 'int', check_int, -2**(64-1),
144+
'Sets substitution value for int64 missing values.')
145+
146+
register_option('cas.int32_missing', 'int', check_int, -2**(32-1),
147+
'Sets substitution value for int32 missing values.')
148+
149+
register_option('cas.date_missing', 'int', check_int, -2**(32-1),
150+
'Sets substitution value for date missing values.')
151+
152+
register_option('cas.time_missing', 'int', check_int, -2**(64-1),
153+
'Sets substitution value for time missing values.')
154+
155+
register_option('cas.datetime_missing', 'int', check_int, -2**(64-1),
156+
'Sets substitution value for datetime missing values.')
157+
139158
#
140159
# Tabular data options
141160
#

swat/tests/cas/test_basics.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def setUp(self):
5252
swat.reset_option()
5353
swat.options.cas.print_messages = False
5454
swat.options.interactive_mode = True
55+
swat.options.int64_missing = -999999
5556

5657
self.s = swat.CAS(HOST, PORT, USER, PASSWD, protocol=PROTOCOL)
5758

@@ -412,7 +413,7 @@ def test_addtable(self):
412413
self.assertEqual(len(data), 15)
413414
self.assertEqual(data.ix[:,'Column'].tolist(),
414415
'Make,Model,Type,Origin,DriveTrain,MSRP,Invoice,EngineSize,Cylinders,Horsepower,MPG_City,MPG_Highway,Weight,Wheelbase,Length'.split(','))
415-
self.assertEqual(data.ix[:,'Type'].tolist(), (['varchar'] * 5) + (['double'] * 10))
416+
self.assertEqual(data.ix[:,'Type'].tolist(), ['varchar', 'varchar', 'varchar', 'varchar', 'varchar', 'int64', 'int64', 'double', 'int64', 'int64', 'int64', 'int64', 'int64', 'int64', 'int64'])
416417

417418
self.assertTablesEqual(cars, self.s.CASTable('cars', caslib=srcLib), sortby=SORT_KEYS)
418419

swat/tests/cas/test_datamsg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def setUp(self):
5252
swat.reset_option()
5353
swat.options.cas.print_messages = False
5454
swat.options.interactive_mode = False
55+
swat.options.int64_missing = -999999
5556

5657
self.s = swat.CAS(HOST, PORT, USER, PASSWD, protocol=PROTOCOL)
5758

@@ -104,7 +105,7 @@ def test_csv(self):
104105

105106
self.assertEqual(len(data), 15)
106107
self.assertEqual(data.ix[:,'Column'].tolist(), 'Make,Model,Type,Origin,DriveTrain,MSRP,Invoice,EngineSize,Cylinders,Horsepower,MPG_City,MPG_Highway,Weight,Wheelbase,Length'.split(','))
107-
self.assertEqual(data.ix[:,'Type'].tolist(), (['varchar'] * 5) + (['double'] * 10))
108+
self.assertEqual(data.ix[:,'Type'].tolist(), ['varchar', 'varchar', 'varchar', 'varchar', 'varchar', 'int64', 'int64', 'double', 'int64', 'int64', 'int64', 'int64', 'int64', 'int64', 'int64'])
108109

109110
self.assertTablesEqual(cars, self.s.CASTable('cars', caslib=srcLib), sortby=SORT_KEYS)
110111

swat/tests/cas/test_table.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def setUp(self):
5959
swat.options.cas.trace_actions = False
6060
swat.options.cas.trace_ui_actions = False
6161
swat.options.interactive_mode = False
62+
swat.options.int64_missing = -999999
6263

6364
self.s = swat.CAS(HOST, PORT, USER, PASSWD, protocol=PROTOCOL)
6465

swat/tests/test_config.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,11 @@ def test_doc(self):
245245

246246
def test_suboptions(self):
247247
self.assertEqual(list(sorted(get_suboptions('cas').keys())),
248-
['dataset', 'exception_on_severity', 'hostname',
249-
'port', 'print_messages', 'protocol', 'trace_actions',
248+
['dataset', 'date_missing', 'datetime_missing',
249+
'exception_on_severity', 'hostname',
250+
'int32_missing', 'int64_missing',
251+
'port', 'print_messages', 'protocol',
252+
'time_missing', 'trace_actions',
250253
'trace_ui_actions'])
251254

252255
with self.assertRaises(SWATOptionError):

0 commit comments

Comments
 (0)