本文整理汇总了Python中datetime.date.fromordinal方法的典型用法代码示例。如果您正苦于以下问题:Python date.fromordinal方法的具体用法?Python date.fromordinal怎么用?Python date.fromordinal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类datetime.date
的用法示例。
在下文中一共展示了date.fromordinal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _unconvert_index
# 需要导入模块: from datetime import date [as 别名]
# 或者: from datetime.date import fromordinal [as 别名]
def _unconvert_index(data, kind, encoding=None):
kind = _ensure_decoded(kind)
if kind == u('datetime64'):
index = DatetimeIndex(data)
elif kind == u('datetime'):
index = np.array([datetime.fromtimestamp(v) for v in data],
dtype=object)
elif kind == u('date'):
try:
index = np.array(
[date.fromordinal(v) for v in data], dtype=object)
except (ValueError):
index = np.array(
[date.fromtimestamp(v) for v in data], dtype=object)
elif kind in (u('integer'), u('float')):
index = np.array(data)
elif kind in (u('string')):
index = _unconvert_string_array(data, nan_rep=None, encoding=encoding)
elif kind == u('object'):
index = np.array(data[0])
else: # pragma: no cover
raise ValueError('unrecognized index type %s' % kind)
return index
示例2: _unconvert_index
# 需要导入模块: from datetime import date [as 别名]
# 或者: from datetime.date import fromordinal [as 别名]
def _unconvert_index(data, kind, encoding=None):
kind = _ensure_decoded(kind)
if kind == u('datetime64'):
index = DatetimeIndex(data)
elif kind == u('timedelta64'):
index = TimedeltaIndex(data)
elif kind == u('datetime'):
index = np.asarray([datetime.fromtimestamp(v) for v in data],
dtype=object)
elif kind == u('date'):
try:
index = np.asarray(
[date.fromordinal(v) for v in data], dtype=object)
except (ValueError):
index = np.asarray(
[date.fromtimestamp(v) for v in data], dtype=object)
elif kind in (u('integer'), u('float')):
index = np.asarray(data)
elif kind in (u('string')):
index = _unconvert_string_array(data, nan_rep=None, encoding=encoding)
elif kind == u('object'):
index = np.asarray(data[0])
else: # pragma: no cover
raise ValueError('unrecognized index type %s' % kind)
return index
示例3: _unconvert_index
# 需要导入模块: from datetime import date [as 别名]
# 或者: from datetime.date import fromordinal [as 别名]
def _unconvert_index(data, kind, encoding=None, errors='strict'):
kind = _ensure_decoded(kind)
if kind == u'datetime64':
index = DatetimeIndex(data)
elif kind == u'timedelta64':
index = TimedeltaIndex(data)
elif kind == u'datetime':
index = np.asarray([datetime.fromtimestamp(v) for v in data],
dtype=object)
elif kind == u'date':
try:
index = np.asarray(
[date.fromordinal(v) for v in data], dtype=object)
except (ValueError):
index = np.asarray(
[date.fromtimestamp(v) for v in data], dtype=object)
elif kind in (u'integer', u'float'):
index = np.asarray(data)
elif kind in (u'string'):
index = _unconvert_string_array(data, nan_rep=None, encoding=encoding,
errors=errors)
elif kind == u'object':
index = np.asarray(data[0])
else: # pragma: no cover
raise ValueError('unrecognized index type {kind}'.format(kind=kind))
return index
示例4: _unconvert_index
# 需要导入模块: from datetime import date [as 别名]
# 或者: from datetime.date import fromordinal [as 别名]
def _unconvert_index(data, kind, encoding=None, errors='strict'):
kind = _ensure_decoded(kind)
if kind == u('datetime64'):
index = DatetimeIndex(data)
elif kind == u('timedelta64'):
index = TimedeltaIndex(data)
elif kind == u('datetime'):
index = np.asarray([datetime.fromtimestamp(v) for v in data],
dtype=object)
elif kind == u('date'):
try:
index = np.asarray(
[date.fromordinal(v) for v in data], dtype=object)
except (ValueError):
index = np.asarray(
[date.fromtimestamp(v) for v in data], dtype=object)
elif kind in (u('integer'), u('float')):
index = np.asarray(data)
elif kind in (u('string')):
index = _unconvert_string_array(data, nan_rep=None, encoding=encoding,
errors=errors)
elif kind == u('object'):
index = np.asarray(data[0])
else: # pragma: no cover
raise ValueError('unrecognized index type %s' % kind)
return index
示例5: fromordinal
# 需要导入模块: from datetime import date [as 别名]
# 或者: from datetime.date import fromordinal [as 别名]
def fromordinal(ordinal):
"""Return the date corresponding to the proleptic Gregorian ordinal.
January 1 of year 1 has ordinal 1. ValueError is raised unless
1 <= ordinal <= date.max.toordinal().
For any date d, date.fromordinal(d.toordinal()) == d.
"""
示例6: toordinal
# 需要导入模块: from datetime import date [as 别名]
# 或者: from datetime.date import fromordinal [as 别名]
def toordinal():
"""Return the proleptic Gregorian ordinal of the date
January 1 of year 1 has ordinal 1. For any date object d,
date.fromordinal(d.toordinal()) == d.
"""
示例7: get_values
# 需要导入模块: from datetime import date [as 别名]
# 或者: from datetime.date import fromordinal [as 别名]
def get_values(self, data, filter_values):
field = self._params['field']
min_value = get_value(data, 'min_start'.format(field))
max_value = get_value(data, 'max_end'.format(field))
if not (min_value and max_value):
return None
return {
'min': date.fromordinal(int(min_value)),
'max': date.fromordinal(int(max_value)),
'days': max_value - min_value,
}
示例8: fromordinal
# 需要导入模块: from datetime import date [as 别名]
# 或者: from datetime.date import fromordinal [as 别名]
def fromordinal(ordinal):
"""Return the date corresponding to the proleptic Gregorian ordinal.
January 1 of year 1 has ordinal 1. `ValueError` is raised unless
1 <= ordinal <= date.max.toordinal().
For any date *d*, ``date.fromordinal(d.toordinal()) == d``.
"""
示例9: toordinal
# 需要导入模块: from datetime import date [as 别名]
# 或者: from datetime.date import fromordinal [as 别名]
def toordinal():
"""Return the proleptic Gregorian ordinal of the date
January 1 of year 1 has ordinal 1. For any date object *d*,
``date.fromordinal(d.toordinal()) == d``.
"""
示例10: test_fromordinal
# 需要导入模块: from datetime import date [as 别名]
# 或者: from datetime.date import fromordinal [as 别名]
def test_fromordinal():
assert pendulum.Date.fromordinal(730120) == date.fromordinal(730120)
示例11: fromordinal
# 需要导入模块: from datetime import date [as 别名]
# 或者: from datetime.date import fromordinal [as 别名]
def fromordinal(n):
return DateWithFuture(date=date_class.fromordinal(n))
示例12: _get_json_for_random_dates
# 需要导入模块: from datetime import date [as 别名]
# 或者: from datetime.date import fromordinal [as 别名]
def _get_json_for_random_dates(count, use_concept_tags, thumbs):
"""
This returns the JSON data for a set of randomly chosen dates. The number of dates is specified by the count
parameter
:param count:
:param use_concept_tags:
:return:
"""
if count > 100 or count <= 0:
raise ValueError('Count must be positive and cannot exceed 100')
begin_ordinal = datetime(1995, 6, 16).toordinal()
today_ordinal = datetime.today().toordinal()
date_range = range(begin_ordinal, today_ordinal + 1)
random_date_ordinals = sample(date_range, count)
all_data = []
for date_ordinal in random_date_ordinals:
dt = date.fromordinal(date_ordinal)
data = _apod_handler(dt, use_concept_tags, date_ordinal == today_ordinal, thumbs)
data['service_version'] = SERVICE_VERSION
all_data.append(data)
return jsonify(all_data)
示例13: random_date
# 需要导入模块: from datetime import date [as 别名]
# 或者: from datetime.date import fromordinal [as 别名]
def random_date(starting_date=None):
starting_date = starting_date or earliest
# ordinal dates are easier to pick random between
start_date = starting_date.toordinal()
end_date = now.toordinal()
return date.fromordinal(randint(start_date, end_date))
示例14: datespan
# 需要导入模块: from datetime import date [as 别名]
# 或者: from datetime.date import fromordinal [as 别名]
def datespan(fromdate, todate, fromdateincl=True, todateincl=True,
key='dateid',
strings={'date': '%Y-%m-%d', 'monthname': '%B', 'weekday': '%A'},
ints={'year': '%Y', 'month': '%m', 'day': '%d'},
expander=None):
"""Return a generator yielding dicts for all dates in an interval.
Arguments:
- fromdate: The lower bound for the date interval. Should be a
datetime.date or a YYYY-MM-DD formatted string.
- todate: The upper bound for the date interval. Should be a
datetime.date or a YYYY-MM-DD formatted string.
- fromdateincl: Decides if fromdate is included. Default: True
- todateincl: Decides if todate is included. Default: True
- key: The name of the attribute where an int (YYYYMMDD) that uniquely
identifies the date is stored. Default: 'dateid'.
- strings: A dict mapping attribute names to formatting directives (as
those used by strftime). The returned dicts will have the specified
attributes as strings.
Default: {'date':'%Y-%m-%d', 'monthname':'%B', 'weekday':'%A'}
- ints: A dict mapping attribute names to formatting directives (as
those used by strftime). The returned dicts will have the specified
attributes as ints.
Default: {'year':'%Y', 'month':'%m', 'day':'%d'}
- expander: A callable f(date, dict) that is invoked on each created
dict. Not invoked if None. Default: None
"""
for arg in (fromdate, todate):
if not ((type(arg) in _stringtypes and arg.count('-') == 2) or
isinstance(arg, date)):
raise ValueError(
"fromdate and today must be datetime.dates or " +
"YYYY-MM-DD formatted strings")
(year, month, day) = fromdate.split('-')
fromdate = date(int(year), int(month), int(day))
(year, month, day) = todate.split('-')
todate = date(int(year), int(month), int(day))
start = fromdate.toordinal()
if not fromdateincl:
start += 1
end = todate.toordinal()
if todateincl:
end += 1
for i in range(start, end):
d = date.fromordinal(i)
res = {}
res[key] = int(d.strftime('%Y%m%d'))
for (att, attformat) in strings.iteritems():
res[att] = d.strftime(attformat)
for (att, attformat) in ints.iteritems():
res[att] = int(d.strftime(attformat))
if expander is not None:
expander(d, res)
yield res
示例15: convert
# 需要导入模块: from datetime import date [as 别名]
# 或者: from datetime.date import fromordinal [as 别名]
def convert(self, values, nan_rep, encoding):
"""set the data from this selection (and convert to the correct dtype
if we can)
"""
try:
values = values[self.cname]
except:
pass
self.set_data(values)
# convert to the correct dtype
if self.dtype is not None:
dtype = _ensure_decoded(self.dtype)
# reverse converts
if dtype == u('datetime64'):
# recreate the timezone
if self.tz is not None:
# data should be 2-dim here
# we stored as utc, so just set the tz
index = DatetimeIndex(
self.data.ravel(), tz='UTC').tz_convert(self.tz)
self.data = np.array(
index.tolist(), dtype=object).reshape(self.data.shape)
else:
self.data = np.asarray(self.data, dtype='M8[ns]')
elif dtype == u('timedelta64'):
self.data = np.asarray(self.data, dtype='m8[ns]')
elif dtype == u('date'):
try:
self.data = np.array(
[date.fromordinal(v) for v in self.data], dtype=object)
except ValueError:
self.data = np.array(
[date.fromtimestamp(v) for v in self.data],
dtype=object)
elif dtype == u('datetime'):
self.data = np.array(
[datetime.fromtimestamp(v) for v in self.data],
dtype=object)
else:
try:
self.data = self.data.astype(dtype)
except:
self.data = self.data.astype('O')
# convert nans / decode
if _ensure_decoded(self.kind) == u('string'):
self.data = _unconvert_string_array(
self.data, nan_rep=nan_rep, encoding=encoding)
return self