本文整理匯總了Python中numpy.datetime64方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.datetime64方法的具體用法?Python numpy.datetime64怎麽用?Python numpy.datetime64使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.datetime64方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ensure_datetime
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import datetime64 [as 別名]
def ensure_datetime(obj):
"""Return the object if it is a datetime-like object
Parameters
----------
obj : Object to be tested.
Returns
-------
The original object if it is a datetime-like object
Raises
------
TypeError if `obj` is not datetime-like
"""
_VALID_TYPES = (str, datetime.datetime, cftime.datetime,
np.datetime64)
if isinstance(obj, _VALID_TYPES):
return obj
raise TypeError("datetime-like object required. "
"Type given: {}".format(type(obj)))
示例2: extract_months
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import datetime64 [as 別名]
def extract_months(time, months):
"""Extract times within specified months of the year.
Parameters
----------
time : xarray.DataArray
Array of times that can be represented by numpy.datetime64 objects
(i.e. the year is between 1678 and 2262).
months : Desired months of the year to include
Returns
-------
xarray.DataArray of the desired times
"""
inds = _month_conditional(time, months)
return time.sel(time=inds)
示例3: __getitem__
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import datetime64 [as 別名]
def __getitem__(self, key):
if isinstance(key, numpy.datetime64):
idx = numpy.searchsorted(self.timestamps, key)
if self.timestamps[idx] == key:
return self[idx]
raise KeyError(key)
if isinstance(key, slice):
if isinstance(key.start, numpy.datetime64):
start = numpy.searchsorted(self.timestamps, key.start)
else:
start = key.start
if isinstance(key.stop, numpy.datetime64):
stop = numpy.searchsorted(self.timestamps, key.stop)
else:
stop = key.stop
key = slice(start, stop, key.step)
return self.ts[key]
示例4: truncate
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import datetime64 [as 別名]
def truncate(self, oldest_point=None):
"""Truncate the time series up to oldest_point excluded.
:param oldest_point: Oldest point to keep from, this excluded.
Default is the aggregation timespan.
:type oldest_point: numpy.datetime64 or numpy.timedelta64
:return: The oldest point that could have been kept.
"""
last = self.last
if last is None:
return
if oldest_point is None:
oldest_point = self.aggregation.timespan
if oldest_point is None:
return
if isinstance(oldest_point, numpy.timedelta64):
oldest_point = last - oldest_point
index = numpy.searchsorted(self.ts['timestamps'], oldest_point,
side='right')
self.ts = self.ts[index:]
return oldest_point
示例5: test_corrupted_split
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import datetime64 [as 別名]
def test_corrupted_split(self):
self.incoming.add_measures(self.metric.id, [
incoming.Measure(datetime64(2014, 1, 1, 12, 0, 1), 69),
])
self.trigger_processing()
aggregation = self.metric.archive_policy.get_aggregation(
"mean", numpy.timedelta64(5, 'm'))
with mock.patch('gnocchi.carbonara.AggregatedTimeSerie.unserialize',
side_effect=carbonara.InvalidData()):
results = self.storage._get_splits_and_unserialize({
self.metric: {
aggregation: [
carbonara.SplitKey(
numpy.datetime64(1387800000, 's'),
numpy.timedelta64(5, 'm'))
],
},
})[self.metric][aggregation]
self.assertEqual(1, len(results))
self.assertIsInstance(results[0], carbonara.AggregatedTimeSerie)
# Assert it's an empty one since corrupted
self.assertEqual(0, len(results[0]))
self.assertEqual(results[0].aggregation, aggregation)
示例6: test_get_splits_and_unserialize
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import datetime64 [as 別名]
def test_get_splits_and_unserialize(self):
self.incoming.add_measures(self.metric.id, [
incoming.Measure(datetime64(2014, 1, 1, 12, 0, 1), 69),
])
self.trigger_processing()
aggregation = self.metric.archive_policy.get_aggregation(
"mean", numpy.timedelta64(5, 'm'))
results = self.storage._get_splits_and_unserialize({
self.metric: {
aggregation: [
carbonara.SplitKey(
numpy.datetime64(1387800000, 's'),
numpy.timedelta64(5, 'm')),
],
},
})[self.metric][aggregation]
self.assertEqual(1, len(results))
self.assertIsInstance(results[0], carbonara.AggregatedTimeSerie)
# Assert it's not empty one since corrupted
self.assertGreater(len(results[0]), 0)
self.assertEqual(results[0].aggregation, aggregation)
示例7: test_delete_nonempty_metric
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import datetime64 [as 別名]
def test_delete_nonempty_metric(self):
self.incoming.add_measures(self.metric.id, [
incoming.Measure(datetime64(2014, 1, 1, 12, 0, 1), 69),
])
self.trigger_processing()
self.storage._delete_metric(self.metric)
self.trigger_processing()
aggregations = (
self.metric.archive_policy.get_aggregations_for_method("mean")
)
self.assertRaises(storage.MetricDoesNotExist,
self.storage.get_aggregated_measures,
{self.metric: aggregations})
self.assertEqual(
{self.metric: None},
self.storage._get_or_create_unaggregated_timeseries(
[self.metric]))
示例8: test_get_aggregated_measures
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import datetime64 [as 別名]
def test_get_aggregated_measures(self):
self.incoming.add_measures(self.metric.id, [
incoming.Measure(datetime64(2014, 1, 1, 12, i, j), 100)
for i in six.moves.range(0, 60) for j in six.moves.range(0, 60)])
self.trigger_processing([self.metric])
aggregations = self.metric.archive_policy.aggregations
measures = self.storage.get_aggregated_measures(
{self.metric: aggregations})
self.assertEqual(1, len(measures))
self.assertIn(self.metric, measures)
measures = measures[self.metric]
self.assertEqual(len(aggregations), len(measures))
self.assertGreater(len(measures[aggregations[0]]), 0)
for agg in aggregations:
self.assertEqual(agg, measures[agg].aggregation)
示例9: test_add_measures_update_subset
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import datetime64 [as 別名]
def test_add_measures_update_subset(self):
m, m_sql = self._create_metric('medium')
measures = [
incoming.Measure(datetime64(2014, 1, 6, i, j, 0), 100)
for i in six.moves.range(2) for j in six.moves.range(0, 60, 2)]
self.incoming.add_measures(m.id, measures)
self.trigger_processing([m])
# add measure to end, in same aggregate time as last point.
new_point = datetime64(2014, 1, 6, 1, 58, 1)
self.incoming.add_measures(m.id, [incoming.Measure(new_point, 100)])
with mock.patch.object(self.incoming, 'add_measures') as c:
self.trigger_processing([m])
for __, args, __ in c.mock_calls:
self.assertEqual(
list(args[3])[0][0], carbonara.round_timestamp(
new_point, args[1].granularity * 10e8))
示例10: test_get_measure_unknown_aggregation
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import datetime64 [as 別名]
def test_get_measure_unknown_aggregation(self):
self.incoming.add_measures(self.metric.id, [
incoming.Measure(datetime64(2014, 1, 1, 12, 0, 1), 69),
incoming.Measure(datetime64(2014, 1, 1, 12, 7, 31), 42),
incoming.Measure(datetime64(2014, 1, 1, 12, 9, 31), 4),
incoming.Measure(datetime64(2014, 1, 1, 12, 12, 45), 44),
])
aggregations = (
self.metric.archive_policy.get_aggregations_for_method("last")
)
self.assertRaises(
storage.MetricDoesNotExist,
self.storage.get_aggregated_measures,
{self.metric: aggregations})
示例11: test_duplicate_timestamps
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import datetime64 [as 別名]
def test_duplicate_timestamps(self):
ts = carbonara.BoundTimeSerie.from_data(
[datetime64(2014, 1, 1, 12, 0, 0),
datetime64(2014, 1, 1, 12, 0, 9)],
[10, 23])
self.assertEqual(2, len(ts))
self.assertEqual(10.0, ts[0][1])
self.assertEqual(23.0, ts[1][1])
ts.set_values(numpy.array([(datetime64(2014, 1, 1, 13, 0, 10), 3),
(datetime64(2014, 1, 1, 13, 0, 11), 9),
(datetime64(2014, 1, 1, 13, 0, 11), 8),
(datetime64(2014, 1, 1, 13, 0, 11), 7),
(datetime64(2014, 1, 1, 13, 0, 11), 4)],
dtype=carbonara.TIMESERIES_ARRAY_DTYPE))
self.assertEqual(4, len(ts))
self.assertEqual(10.0, ts[0][1])
self.assertEqual(23.0, ts[1][1])
self.assertEqual(3.0, ts[2][1])
self.assertEqual(9.0, ts[3][1])
示例12: test_derived_hole
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import datetime64 [as 別名]
def test_derived_hole(self):
ts = carbonara.TimeSerie.from_data(
[datetime.datetime(2014, 1, 1, 12, 0, 0),
datetime.datetime(2014, 1, 1, 12, 0, 4),
datetime.datetime(2014, 1, 1, 12, 1, 2),
datetime.datetime(2014, 1, 1, 12, 1, 14),
datetime.datetime(2014, 1, 1, 12, 1, 24),
datetime.datetime(2014, 1, 1, 12, 3, 2),
datetime.datetime(2014, 1, 1, 12, 3, 22),
datetime.datetime(2014, 1, 1, 12, 3, 42),
datetime.datetime(2014, 1, 1, 12, 4, 9)],
[50, 55, 65, 66, 70, 105, 108, 200, 202])
ts = self._resample(ts, numpy.timedelta64(60, 's'), 'last',
derived=True)
self.assertEqual(4, len(ts))
self.assertEqual(
[(datetime64(2014, 1, 1, 12, 0, 0), 5),
(datetime64(2014, 1, 1, 12, 1, 0), 4),
(datetime64(2014, 1, 1, 12, 3, 0), 92),
(datetime64(2014, 1, 1, 12, 4, 0), 2)],
list(ts.fetch(
from_timestamp=datetime64(2014, 1, 1, 12))))
示例13: test_aggregation_std_with_unique
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import datetime64 [as 別名]
def test_aggregation_std_with_unique(self):
ts = carbonara.TimeSerie.from_data(
[datetime64(2014, 1, 1, 12, 0, 0)], [3])
ts = self._resample(ts, numpy.timedelta64(60, 's'), 'std')
self.assertEqual(0, len(ts), ts.values)
ts = carbonara.TimeSerie.from_data(
[datetime64(2014, 1, 1, 12, 0, 0),
datetime64(2014, 1, 1, 12, 0, 4),
datetime64(2014, 1, 1, 12, 0, 9),
datetime64(2014, 1, 1, 12, 1, 6)],
[3, 6, 5, 9])
ts = self._resample(ts, numpy.timedelta64(60, 's'), "std")
self.assertEqual(1, len(ts))
self.assertEqual(1.5275252316519465,
ts[datetime64(2014, 1, 1, 12, 0, 0)][1])
示例14: test_no_truncation
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import datetime64 [as 別名]
def test_no_truncation(self):
ts = {'sampling': numpy.timedelta64(60, 's'), 'agg': 'mean'}
tsb = carbonara.BoundTimeSerie()
for i in six.moves.range(1, 11):
tsb.set_values(numpy.array([
(datetime64(2014, 1, 1, 12, i, i), float(i))],
dtype=carbonara.TIMESERIES_ARRAY_DTYPE),
before_truncate_callback=functools.partial(
self._resample_and_merge, agg_dict=ts))
tsb.set_values(numpy.array([
(datetime64(2014, 1, 1, 12, i, i + 1), float(i + 1))],
dtype=carbonara.TIMESERIES_ARRAY_DTYPE),
before_truncate_callback=functools.partial(
self._resample_and_merge, agg_dict=ts))
self.assertEqual(i, len(list(ts['return'].fetch())))
示例15: test_split_key
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import datetime64 [as 別名]
def test_split_key(self):
self.assertEqual(
numpy.datetime64("2014-10-07"),
carbonara.SplitKey.from_timestamp_and_sampling(
numpy.datetime64("2015-01-01T15:03"),
numpy.timedelta64(3600, 's')))
self.assertEqual(
numpy.datetime64("2014-12-31 18:00"),
carbonara.SplitKey.from_timestamp_and_sampling(
numpy.datetime64("2015-01-01 15:03:58"),
numpy.timedelta64(58, 's')))
key = carbonara.SplitKey.from_timestamp_and_sampling(
numpy.datetime64("2015-01-01 15:03"),
numpy.timedelta64(3600, 's'))
self.assertGreater(key, numpy.datetime64("1970"))
self.assertGreaterEqual(key, numpy.datetime64("1970"))