Skip to content

Commit 2929026

Browse files
authored
Merge pull request #1387 from kernc/fixups
[FIX] TimeVariable: fix repr rounding and repr for nan
2 parents ae82d28 + 0c05cc1 commit 2929026

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

Orange/data/variable.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,9 @@ def _tzre_sub(s, _subtz=re.compile(r'([+-])(\d\d):(\d\d)$').sub):
902902
return s[:-6] if s.endswith(('+00:00', '-00:00')) else _subtz(r'\1\2\3', s)
903903

904904
def repr_val(self, val):
905-
seconds = int(round(val))
905+
if isnan(val):
906+
return '?'
907+
seconds = int(val)
906908
microseconds = int(round((val - seconds) * 1e6))
907909
if val < 0:
908910
date = datetime.fromtimestamp(0, tz=self.timezone) + timedelta(seconds=seconds)

Orange/tests/test_variable.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ class TestTimeVariable(VariableTest):
289289
TESTS = [
290290
# in str, UTC timestamp, out str (in UTC)
291291
('2015-10-12 14:13:11.01+0200', 1444651991.01, '2015-10-12 14:13:11.010000+0200'),
292-
('2015-10-12T14:13:11.01+0200', 1444651991.01, '2015-10-12 14:13:11.010000+0200'),
292+
('2015-10-12T14:13:11.81+0200', 1444651991.81, '2015-10-12 14:13:11.810000+0200'),
293293
('2015-10-12 14:13:11+0200', 1444651991, '2015-10-12 14:13:11+0200'),
294294
('2015-10-12T14:13:11+0200', 1444651991, '2015-10-12 14:13:11+0200'),
295295
('20151012T141311+0200', 1444651991, '2015-10-12 14:13:11+0200'),
@@ -311,13 +311,15 @@ class TestTimeVariable(VariableTest):
311311
('1970-01-01 00:00:00', 0, '1970-01-01 00:00:00'),
312312
('1969-12-31 23:59:59', -1, '1969-12-31 23:59:59'),
313313
('1900-01-01', -2208988800, '1900-01-01'),
314+
('nan', np.nan, '?'),
314315
]
315316

316317
def test_parse_repr(self):
317318
for datestr, timestamp, outstr in self.TESTS:
318319
var = TimeVariable('time')
319320
ts = var.parse(datestr)
320-
self.assertEqual(ts, timestamp, msg=datestr)
321+
if not np.isnan(ts):
322+
self.assertEqual(ts, timestamp, msg=datestr)
321323
self.assertEqual(var.repr_val(ts), outstr, msg=datestr)
322324

323325
def test_parse_utc(self):

0 commit comments

Comments
 (0)