本文整理汇总了Python中Orange.data.TimeVariable.parse方法的典型用法代码示例。如果您正苦于以下问题:Python TimeVariable.parse方法的具体用法?Python TimeVariable.parse怎么用?Python TimeVariable.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Orange.data.TimeVariable
的用法示例。
在下文中一共展示了TimeVariable.parse方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_have_date
# 需要导入模块: from Orange.data import TimeVariable [as 别名]
# 或者: from Orange.data.TimeVariable import parse [as 别名]
def test_have_date(self):
var = TimeVariable('time')
ts = var.parse('1937-08-02') # parse date
self.assertEqual(var.repr_val(ts), '1937-08-02')
ts = var.parse('16:20') # parse time
# observe have datetime
self.assertEqual(var.repr_val(ts), '1970-01-01 16:20:00')
示例2: test_parse_utc
# 需要导入模块: from Orange.data import TimeVariable [as 别名]
# 或者: from Orange.data.TimeVariable import parse [as 别名]
def test_parse_utc(self):
var = TimeVariable('time')
datestr, offset = '2015-10-18 22:48:20', '+0200'
ts1 = var.parse(datestr + offset)
self.assertEqual(var.repr_val(ts1), datestr + offset)
# Once a value is without a TZ, all the values lose it
ts2 = var.parse(datestr)
self.assertEqual(var.repr_val(ts2), datestr)
self.assertEqual(var.repr_val(ts1), '2015-10-18 20:48:20')
示例3: guess_data_type
# 需要导入模块: from Orange.data import TimeVariable [as 别名]
# 或者: from Orange.data.TimeVariable import parse [as 别名]
def guess_data_type(orig_values, namask=None):
"""
Use heuristics to guess data type.
"""
valuemap, values = None, orig_values
is_discrete = is_discrete_values(orig_values)
if is_discrete:
valuemap = sorted(is_discrete)
coltype = DiscreteVariable
else:
# try to parse as float
orig_values = np.asarray(orig_values)
if namask is None:
namask = isnastr(orig_values)
values = np.empty_like(orig_values, dtype=float)
values[namask] = np.nan
try:
np.copyto(values, orig_values, where=~namask, casting="unsafe")
except ValueError:
tvar = TimeVariable('_')
try:
values[~namask] = [tvar.parse(i) for i in orig_values[~namask]]
except ValueError:
coltype = StringVariable
# return original_values
values = orig_values
else:
coltype = TimeVariable
else:
coltype = ContinuousVariable
return valuemap, values, coltype
示例4: test_parse_repr
# 需要导入模块: from Orange.data import TimeVariable [as 别名]
# 或者: from Orange.data.TimeVariable import parse [as 别名]
def test_parse_repr(self):
for datestr, timestamp, outstr in self.TESTS:
var = TimeVariable('time')
ts = var.parse(datestr)
if not np.isnan(ts):
self.assertEqual(ts, timestamp, msg=datestr)
self.assertEqual(var.repr_val(ts), outstr, msg=datestr)
示例5: _date_to_iso
# 需要导入模块: from Orange.data import TimeVariable [as 别名]
# 或者: from Orange.data.TimeVariable import parse [as 别名]
def _date_to_iso(date):
possible_date_formats = [
'%Y %b %d',
'%Y %b',
'%Y',
]
season_mapping = {
'fall': 'Sep',
'autumn': 'Sep',
'winter': 'Dec',
'spring': 'Mar',
'summer': 'Jun',
}
date = date.lower()
# Seasons to their respective months.
for season, month in season_mapping.items():
date = date.replace(season, month)
date = date.split('-')[0] # 2015 Sep-Dec --> 2015 Sep
time_var = TimeVariable()
for date_format in possible_date_formats:
try:
date_string = datetime.strptime(
date, date_format
).date().isoformat()
return time_var.parse(date_string)
except ValueError:
continue # Try the next format.
warnings.warn(
'Could not parse "{}" into a date.'.format(date),
RuntimeWarning
)
return time_var.parse(np.nan)
示例6: guess_data_type
# 需要导入模块: from Orange.data import TimeVariable [as 别名]
# 或者: from Orange.data.TimeVariable import parse [as 别名]
def guess_data_type(orig_values):
"""
Use heuristics to guess data type.
"""
valuemap, values = [], orig_values
is_discrete = is_discrete_values(orig_values)
if is_discrete:
valuemap = sorted(is_discrete)
coltype = DiscreteVariable
else:
try:
values = [float(i) for i in orig_values]
except ValueError:
tvar = TimeVariable('_')
try:
values = [tvar.parse(i) for i in orig_values]
except ValueError:
coltype = StringVariable
else:
coltype = TimeVariable
else:
coltype = ContinuousVariable
return valuemap, values, coltype
示例7: test_parse_invalid
# 需要导入模块: from Orange.data import TimeVariable [as 别名]
# 或者: from Orange.data.TimeVariable import parse [as 别名]
def test_parse_invalid(self):
var = TimeVariable('var')
with self.assertRaises(ValueError):
var.parse('123')
示例8: test_parse_timestamp
# 需要导入模块: from Orange.data import TimeVariable [as 别名]
# 或者: from Orange.data.TimeVariable import parse [as 别名]
def test_parse_timestamp(self):
var = TimeVariable("time")
datestr = str(datetime(2016, 6, 14, 23, 8, tzinfo=timezone.utc).timestamp())
ts1 = var.parse(datestr)
self.assertEqual(var.repr_val(ts1), '2016-06-14 23:08:00')
示例9: data_table
# 需要导入模块: from Orange.data import TimeVariable [as 别名]
# 或者: from Orange.data.TimeVariable import parse [as 别名]
def data_table(self, data, headers=None):
"""
Return Orange.data.Table given rows of `headers` (iterable of iterable)
and rows of `data` (iterable of iterable; if ``numpy.ndarray``, might
as well **have it sorted column-major**, e.g. ``order='F'``).
Basically, the idea of subclasses is to produce those two iterables,
however they might.
If `headers` is not provided, the header rows are extracted from `data`,
assuming they precede it.
"""
if not headers:
headers, data = self.parse_headers(data)
# Consider various header types (single-row, two-row, three-row, none)
if 3 == len(headers):
names, types, flags = map(list, headers)
else:
if 1 == len(headers):
HEADER1_FLAG_SEP = '#'
# First row format either:
# 1) delimited column names
# 2) -||- with type and flags prepended, separated by #,
# e.g. d#sex,c#age,cC#IQ
_flags, names = zip(*[i.split(HEADER1_FLAG_SEP, 1) if HEADER1_FLAG_SEP in i else ('', i)
for i in headers[0]])
names = list(names)
elif 2 == len(headers):
names, _flags = map(list, headers)
else:
# Use heuristics for everything
names, _flags = [], []
types = [''.join(filter(str.isupper, flag)).lower() for flag in _flags]
flags = [Flags.join(filter(str.islower, flag)) for flag in _flags]
# Determine maximum row length
rowlen = max(map(len, (names, types, flags)))
def _equal_length(lst):
lst.extend(['']*(rowlen - len(lst)))
return lst
# Ensure all data is of equal width in a column-contiguous array
data = np.array([_equal_length(list(row)) for row in data if any(row)],
copy=False, dtype=object, order='F')
# Data may actually be longer than headers were
try: rowlen = data.shape[1]
except IndexError: pass
else:
for lst in (names, types, flags):
_equal_length(lst)
NAMEGEN = namegen('Feature ', 1)
Xcols, attrs = [], []
Mcols, metas = [], []
Ycols, clses = [], []
Wcols = []
# Iterate through the columns
for col in range(rowlen):
flag = Flags(Flags.split(flags[col]))
if flag.i: continue
type_flag = types and types[col].strip()
try:
orig_values = [np.nan if i in MISSING_VALUES else i
for i in (i.strip() for i in data[:, col])]
except IndexError:
# No data instances leads here
orig_values = []
# In this case, coltype could be anything. It's set as-is
# only to satisfy test_table.TableTestCase.test_append
coltype = DiscreteVariable
coltype_kwargs = {}
valuemap = []
values = orig_values
if type_flag in StringVariable.TYPE_HEADERS:
coltype = StringVariable
elif type_flag in ContinuousVariable.TYPE_HEADERS:
coltype = ContinuousVariable
try:
values = [float(i) for i in orig_values]
except ValueError:
for row, num in enumerate(orig_values):
try: float(num)
except ValueError: break
raise ValueError('Non-continuous value in (1-based) '
'line {}, column {}'.format(row + len(headers) + 1,
col + 1))
elif type_flag in TimeVariable.TYPE_HEADERS:
coltype = TimeVariable
elif (type_flag in DiscreteVariable.TYPE_HEADERS or
_RE_DISCRETE_LIST.match(type_flag)):
if _RE_DISCRETE_LIST.match(type_flag):
#.........这里部分代码省略.........