本文整理汇总了Python中numpy.datetime_data方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.datetime_data方法的具体用法?Python numpy.datetime_data怎么用?Python numpy.datetime_data使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.datetime_data方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _validate_date_like_dtype
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import datetime_data [as 别名]
def _validate_date_like_dtype(dtype):
"""
Check whether the dtype is a date-like dtype. Raises an error if invalid.
Parameters
----------
dtype : dtype, type
The dtype to check.
Raises
------
TypeError : The dtype could not be casted to a date-like dtype.
ValueError : The dtype is an illegal date-like dtype (e.g. the
the frequency provided is too specific)
"""
try:
typ = np.datetime_data(dtype)[0]
except ValueError as e:
raise TypeError('{error}'.format(error=e))
if typ != 'generic' and typ != 'ns':
msg = '{name!r} is too specific of a frequency, try passing {type!r}'
raise ValueError(msg.format(name=dtype.name, type=dtype.type.__name__))
示例2: _datetime_metadata_str
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import datetime_data [as 别名]
def _datetime_metadata_str(dtype):
# TODO: this duplicates the C append_metastr_to_string
unit, count = np.datetime_data(dtype)
if unit == 'generic':
return ''
elif count == 1:
return '[{}]'.format(unit)
else:
return '[{}{}]'.format(count, unit)
示例3: test_basic
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import datetime_data [as 别名]
def test_basic(self):
a = np.array(['1980-03-23'], dtype=np.datetime64)
assert_equal(np.datetime_data(a.dtype), ('D', 1))
示例4: fourier_model_mat
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import datetime_data [as 别名]
def fourier_model_mat(datetimes: np.ndarray,
K: int,
period: Union[np.timedelta64, str],
output_fmt: str = 'float64') -> np.ndarray:
"""
:param datetimes: An array of datetimes.
:param K: The expansion integer.
:param period: Either a np.timedelta64, or one of {'weekly','yearly','daily'}
:param start_datetime: A np.datetime64 on which to consider the season-start; useful for aligning (e.g) weekly
seasons to start on Monday, or daily seasons to start on a particular hour. Default is first monday after epoch.
:param output_fmt: A numpy dtype, or 'dataframe' to output a dataframe.
:return: A numpy array (or dataframe) with the expanded fourier series.
"""
# parse period:
name = 'fourier'
if isinstance(period, str):
name = period
if period == 'weekly':
period = np.timedelta64(7, 'D')
elif period == 'yearly':
period = np.timedelta64(int(365.25 * 24), 'h')
elif period == 'daily':
period = np.timedelta64(24, 'h')
else:
raise ValueError("Unrecognized `period`.")
period_int = period.view('int64')
dt_helper = DateTimeHelper(dt_unit=np.datetime_data(period)[0])
time = dt_helper.validate_datetimes(datetimes).view('int64')
output_dataframe = (output_fmt.lower() == 'dataframe')
if output_dataframe:
output_fmt = 'float64'
# fourier matrix:
out_shape = tuple(datetimes.shape) + (K * 2,)
out = np.empty(out_shape, dtype=output_fmt)
columns = []
for idx in range(K):
k = idx + 1
for is_cos in range(2):
val = 2. * np.pi * k * time / period_int
out[..., idx * 2 + is_cos] = np.sin(val) if is_cos == 0 else np.cos(val)
columns.append(f"{name}_K{k}_{'cos' if is_cos else 'sin'}")
if output_dataframe:
if len(out_shape) > 2:
raise ValueError("Cannot output dataframe when input is 2+D array.")
from pandas import DataFrame
out = DataFrame(out, columns=columns)
return out