Here are the examples of the python api numpy.datetime64 taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
126 Examples
3
Example 1
Project: AWS-Lambda-ML-Microservice-Skeleton Source File: test_common.py
def test_isnull_numpy_nat():
arr = np.array([NaT, np.datetime64('NaT'), np.timedelta64('NaT'),
np.datetime64('NaT', 's')])
result = isnull(arr)
expected = np.array([True] * 4)
tm.assert_numpy_array_equal(result, expected)
3
Example 2
@tag('important')
def test_datetime_values(self):
"""
Test map_arrayscalar_type() with np.datetime64 values.
"""
f = numpy_support.map_arrayscalar_type
self.check_datetime_values(f)
# datetime64s with a non-one factor shouldn't be supported
t = np.datetime64('2014', '10Y')
with self.assertRaises(NotImplementedError):
f(t)
3
Example 3
Project: hyperspy Source File: protochips.py
def _read_all_metadata_header(self):
i = 1
param, value = self._parse_metadata_header(self.raw_header[i])
while 'User' not in param: # user should be the last of the header
if 'Calibration file name' in param:
self.calibration_file = value
elif 'Date (yyyy.mm.dd)' in param:
date = value
elif 'Time (hh:mm:ss.ms)' in param:
time = value
else:
attr_name = param.replace(' ', '_').lower()
self.__dict__[attr_name] = value
i += 1
param, value = self._parse_metadata_header(self.raw_header[i])
self.user = self._parse_metadata_header(self.raw_header[i])[1]
self.start_datetime = np.datetime64(dt.strptime(date + time,
"%Y.%m.%d%H:%M:%S.%f"))
3
Example 4
def _parse_entry(field_value, field_type):
if field_value is None or field_value == 'null':
return None
if field_type == 'INTEGER' or field_type == 'FLOAT':
return float(field_value)
elif field_type == 'TIMESTAMP':
timestamp = datetime.utcfromtimestamp(float(field_value))
return np.datetime64(timestamp)
elif field_type == 'BOOLEAN':
return field_value == 'true'
return field_value
3
Example 5
def _update_ranges(self, element, ranges):
l, b, r, t = self.get_extents(element, ranges)
plot = self.handles['plot']
if self.invert_axes:
l, b, r, t = b, l, t, r
if l == r:
offset = abs(l*0.1 if l else 0.5)
l -= offset
r += offset
if b == t:
offset = abs(b*0.1 if b else 0.5)
b -= offset
t += offset
# Ensure that it never sets a NaN value
if isinstance(l, np.datetime64) or np.isfinite(l): plot.x_range.start = l
if isinstance(l, np.datetime64) or np.isfinite(r): plot.x_range.end = r
if isinstance(l, np.datetime64) or np.isfinite(b): plot.y_range.start = b
if isinstance(l, np.datetime64) or np.isfinite(t): plot.y_range.end = t
3
Example 6
Project: dask Source File: indexing.py
def _coerce_loc_index(divisions, o):
""" Transform values to be comparable against divisions
This is particularly valuable to use with pandas datetimes
"""
if divisions and isinstance(divisions[0], datetime):
return pd.Timestamp(o)
if divisions and isinstance(divisions[0], np.datetime64):
return np.datetime64(o).astype(divisions[0].dtype)
return o
3
Example 7
Project: nansat Source File: mapper_opendap_globcurrent_thredds.py
Function: convert_dstime_datetimes
Function: convert_dstime_datetimes
def convert_dstime_datetimes(self, dsTime):
''' Convert time variable to np.datetime64 '''
dsDatetimes = np.array([np.datetime64(self.timeCalendarStart) + int(day)
for day in dsTime]).astype('M8[s]')
return dsDatetimes
3
Example 8
def _get_time(time, start_date, end_date, months, indices=False):
"""Determine indices/values of a time array within the specified interval.
Assumes time is an xarray DataArray and that it can be represented
by numpy datetime64 objects (i.e. the year is between 1678 and 2262).
"""
inds = TimeManager._construct_month_conditional(time, months)
inds &= (time[TIME_STR] <= np.datetime64(end_date))
inds &= (time[TIME_STR] >= np.datetime64(start_date))
if indices == 'only':
return inds
elif indices:
return (inds, time.sel(time=inds))
else:
return time.sel(time=inds)
3
Example 9
def function(self, simulation, period):
birth = simulation.get_array('birth', period)
if birth is None:
age_en_mois = simulation.get_array('age_en_mois', period)
if age_en_mois is not None:
return period, age_en_mois // 12
birth = simulation.calculate('birth', period)
return period, (np.datetime64(period.date) - birth).astype('timedelta64[Y]')
3
Example 10
Project: xlwings Source File: test_conversion.py
def test_dataframe_timezone(self):
np_dt = np.datetime64(1434149887000, 'ms')
ix = pd.DatetimeIndex(data=[np_dt], tz='GMT')
df = pd.DataFrame(data=[1], index=ix, columns=['A'])
self.wb1.sheets[0].range('A1').value = df
self.assertEqual(self.wb1.sheets[0].range('A2').value, dt.datetime(2015, 6, 12, 22, 58, 7))
3
Example 11
@property
def is_datetime(self):
try:
t = self.type.type
except AttributeError:
t = self.type
return issubclass(t, (datetime, np.datetime64))
3
Example 12
def _start_end(date_range, dts):
"""
Return tuple: [start, end] of np.datetime64 dates that are inclusive of the passed
in datetimes.
"""
# FIXME: timezones
assert len(dts)
_assert_no_timezone(date_range)
date_range = to_pandas_closed_closed(date_range, add_tz=False)
start = np.datetime64(date_range.start) if date_range.start else dts[0]
end = np.datetime64(date_range.end) if date_range.end else dts[-1]
return start, end
3
Example 13
Project: bokeh Source File: test_comp_glyphs.py
def test_step_glyph():
xx = [-2, 0, 1, 3, 4, 5, 6, 9]
dates = np.array(['2016-05-%02i' % i for i in range(1, 9)], dtype=np.datetime64)
values = [1, 2, 3, 2, 1, 5, 4, 5]
# Test with integer x
g = StepGlyph(x=xx, y=values)
data = g.renderers[0].data_source.data
for i in range(0, len(xx)-1):
assert data['x_values'][i*2+0] == xx[i+0]
assert data['x_values'][i*2+1] == xx[i+1]
# Test with dates (#3616)
g = StepGlyph(x=dates, y=values)
data = g.renderers[0].data_source.data
for i in range(0, len(xx)-1):
assert data['x_values'][i*2+0] == dates[i+0]
assert data['x_values'][i*2+1] == dates[i+1]
3
Example 14
def test_datetimes(self):
# fails under 2.6/win32 (np.datetime64 seems broken)
if LooseVersion(sys.version) < '2.7':
raise nose.SkipTest('2.6 with np.datetime64 is broken')
for i in [datetime.datetime(2013, 1, 1),
datetime.datetime(2013, 1, 1, 5, 1),
datetime.date(2013, 1, 1),
np.datetime64(datetime.datetime(2013, 1, 5, 2, 15))]:
i_rec = self.encode_decode(i)
self.assertEqual(i, i_rec)
3
Example 15
Project: scipy Source File: arffread.py
def safe_date(value, date_format, datetime_unit):
date_str = value.strip().strip("'").strip('"')
if date_str == '?':
return np.datetime64('NaT', datetime_unit)
else:
dt = datetime.datetime.strptime(date_str, date_format)
return np.datetime64(dt).astype("datetime64[%s]" % datetime_unit)
3
Example 16
Project: AWS-Lambda-ML-Microservice-Skeleton Source File: test_internals.py
def test_try_coerce_arg(self):
block = create_block('datetime', [0])
# coerce None
none_coerced = block._try_coerce_args(block.values, None)[2]
self.assertTrue(pd.Timestamp(none_coerced) is pd.NaT)
# coerce different types of date bojects
vals = (np.datetime64('2010-10-10'), datetime(2010, 10, 10),
date(2010, 10, 10))
for val in vals:
coerced = block._try_coerce_args(block.values, val)[2]
self.assertEqual(np.int64, type(coerced))
self.assertEqual(pd.Timestamp('2010-10-10'), pd.Timestamp(coerced))
3
Example 17
def to_unix_time(dt):
"""Converts a datetime.datetime to seconds since epoch"""
# TODO test this
# from
# http://stackoverflow.com/questions/6999726/how-can-i-convert-a-datetime-object-to-milliseconds-since-epoch-unix-time-in-p
# and
# http://stackoverflow.com/questions/29753060/how-to-convert-numpy-datetime64-into-datetime
if isinstance(dt, np.datetime64):
# TODO CRITICAL correct for datetime resolution!
dt = dt.astype('M8[s]').astype('O')
if isinstance(dt, datetime):
return (dt - EPOCH).total_seconds()
return dt
3
Example 18
def convert_dstime_datetimes(self, dsTime):
''' Convert time variable to np.datetime64 '''
dsDatetimes = np.array([np.datetime64(self.t0 + dt.timedelta(seconds=day))
for day in dsTime]).astype('M8[s]')
return dsDatetimes
3
Example 19
Project: xarray Source File: utils.py
def ensure_us_time_resolution(val):
"""Convert val out of numpy time, for use in to_dict.
Needed because of numpy bug GH#7619"""
if np.issubdtype(val.dtype, np.datetime64):
val = val.astype('datetime64[us]')
elif np.issubdtype(val.dtype, np.timedelta64):
val = val.astype('timedelta64[us]')
return val
3
Example 20
def test_datetime(self):
a = np.datetime64(1, 'Y')
b = np.datetime64(2, 'Y')
c = np.datetime64(2, 's')
d = np.timedelta64(2, 's')
self.assertEqual(compute_fingerprint(a),
compute_fingerprint(b))
distinct = set(compute_fingerprint(x) for x in (a, c, d))
self.assertEqual(len(distinct), 3, distinct)
3
Example 21
@classmethod
def range(cls, dataset, dimension):
column = dataset.dimension_values(dimension)
if dataset.get_dimension_type(dimension) is np.datetime64:
return column.min(), column.max()
else:
try:
return (np.nanmin(column), np.nanmax(column))
except TypeError:
column.sort()
return column[0], column[-1]
3
Example 22
Project: holoviews Source File: util.py
def dt64_to_dt(dt64):
"""
Safely converts NumPy datetime64 to a datetime object.
"""
ts = (dt64 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')
return dt.datetime.utcfromtimestamp(ts)
3
Example 23
Project: xarray Source File: test_variable.py
def test_index_0d_datetime(self):
d = datetime(2000, 1, 1)
x = self.cls(['x'], [d])
self._assertIndexedLikeNDArray(x, np.datetime64(d))
x = self.cls(['x'], [np.datetime64(d)])
self._assertIndexedLikeNDArray(x, np.datetime64(d), 'datetime64[ns]')
x = self.cls(['x'], pd.DatetimeIndex([d]))
self._assertIndexedLikeNDArray(x, np.datetime64(d), 'datetime64[ns]')
3
Example 24
def get_data(self, element, ranges, style):
xs = element.dimension_values(0)
ys = element.dimension_values(1)
dims = element.dimensions()
if xs.dtype.kind == 'M':
dt_format = Dimension.type_formatters[np.datetime64]
dims[0] = dims[0](value_format=DateFormatter(dt_format))
return (xs, ys), style, {'dimensions': dims}
3
Example 25
@dispatch(Coerce, (Series, DaskSeries))
def compute_up(expr, data, **kwargs):
measure = expr.to.measure
if measure in {datashape.string, datashape.Option(datashape.string)}:
return data.astype(str)
elif measure in {datashape.datetime_,
datashape.Option(datashape.datetime_)}:
return data.astype(np.datetime64)
return data.astype(to_numpy_dtype(expr.schema))
3
Example 26
Project: AWS-Lambda-ML-Microservice-Skeleton Source File: numpy_compat.py
def np_datetime64_compat(s, *args, **kwargs):
"""
provide compat for construction of strings to numpy datetime64's with
tz-changes in 1.11 that make '2015-01-01 09:00:00Z' show a deprecation
warning, when need to pass '2015-01-01 09:00:00'
"""
if not _np_version_under1p11:
s = tz_replacer(s)
return np.datetime64(s, *args, **kwargs)
3
Example 27
Project: memex-explorer Source File: stream.py
@staticmethod
def jtime_to_datetime(t):
"""
Convert a Java-format Epoch time stamp into np.datetime64 object
:param t: Java-format Epoch time stamp (milliseconds)
:return: A np.datetime64 scalar
"""
return np.datetime64(datetime.fromtimestamp(t/1000.0))
3
Example 28
Project: blaze Source File: test_pandas_compute.py
@pytest.mark.parametrize('d, tp, ptp', [(list(range(10)), 'string', str),
(list(range(10)) + [None], '?string', str),
(['2010-01-01'], 'datetime', np.datetime64),
(['2010-01-01', None, ''], '?datetime', np.datetime64)])
def test_coerce_series_string_datetime(d, tp, ptp):
s = pd.Series(d, name='a')
e = symbol('t', discover(s)).coerce(to=tp)
assert e.schema == dshape(tp)
result = compute(e, s)
expected = s.astype(ptp)
assert_series_equal(result, expected)
3
Example 29
@property
def is_datetime(self):
try:
t = self.return_type.type
except AttributeError:
t = self.return_type
return issubclass(t, (datetime, np.datetime64))
3
Example 30
Project: xarray Source File: test_variable.py
def test_transpose_0d(self):
for value in [
3.5,
('a', 1),
np.datetime64('2000-01-01'),
np.timedelta64(1, 'h'),
None,
object(),
]:
variable = Variable([], value)
actual = variable.transpose()
assert actual.identical(variable)
3
Example 31
Project: AWS-Lambda-ML-Microservice-Skeleton Source File: test_gbq.py
def test_should_properly_handle_arbitrary_timestamp(self):
query = 'SELECT TIMESTAMP("2004-09-15 05:00:00") as VALID_TIMESTAMP'
df = gbq.read_gbq(query, project_id=PROJECT_ID)
tm.assert_frame_equal(df, DataFrame({
'VALID_TIMESTAMP': [np.datetime64('2004-09-15T05:00:00.000000Z')]
}))
3
Example 32
Project: trtools Source File: test_pytables.py
def test_convert_frame(self):
desc, recs, types = tb.convert_frame(df)
dtype_msg = "{0} does not match dtype. \nExpected: {1}\nGot: {2}"
# test floats
for col in ['open', 'high', 'low', 'close']:
assert df[col].dtype == desc[col].dtype, dtype_msg.format(col, df[col].dtype, desc[col].dtype)
# test datetime / pytable represents as ints
col = 'other_dates'
assert df[col].dtype.type == np.datetime64
assert desc[col].dtype == int
3
Example 33
Project: AWS-Lambda-ML-Microservice-Skeleton Source File: test_stata.py
def test_read_write_dta10(self):
original = DataFrame(data=[["string", "object", 1, 1.1,
np.datetime64('2003-12-25')]],
columns=['string', 'object', 'integer',
'floating', 'datetime'])
original["object"] = Series(original["object"], dtype=object)
original.index.name = 'index'
original.index = original.index.astype(np.int32)
original['integer'] = original['integer'].astype(np.int32)
with tm.ensure_clean() as path:
original.to_stata(path, {'datetime': 'tc'})
written_and_read_again = self.read_dta(path)
# original.index is np.int32, readed index is np.int64
tm.assert_frame_equal(written_and_read_again.set_index('index'),
original, check_index_type=False)
3
Example 34
def format_item(x, timedelta_format=None, quote_strings=True):
"""Returns a succinct summary of an object as a string"""
if isinstance(x, (np.datetime64, datetime)):
return format_timestamp(x)
if isinstance(x, (np.timedelta64, timedelta)):
return format_timedelta(x, timedelta_format=timedelta_format)
elif isinstance(x, (unicode_type, bytes_type)):
return repr(x) if quote_strings else x
elif isinstance(x, (float, np.float)):
return u'{0:.4}'.format(x)
else:
return unicode_type(x)
3
Example 35
Project: AWS-Lambda-ML-Microservice-Skeleton Source File: test_common.py
def test_dict_compat():
data_datetime64 = {np.datetime64('1990-03-15'): 1,
np.datetime64('2015-03-15'): 2}
data_unchanged = {1: 2, 3: 4, 5: 6}
expected = {Timestamp('1990-3-15'): 1, Timestamp('2015-03-15'): 2}
assert (com._dict_compat(data_datetime64) == expected)
assert (com._dict_compat(expected) == expected)
assert (com._dict_compat(data_unchanged) == data_unchanged)
3
Example 36
Project: dask Source File: test_indexing.py
def test_loc_on_numpy_datetimes():
df = pd.DataFrame({'x': [1, 2, 3]},
index=list(map(np.datetime64, ['2014', '2015', '2016'])))
a = dd.from_pandas(df, 2)
a.divisions = list(map(np.datetime64, a.divisions))
assert_eq(a.loc['2014': '2015'], a.loc['2014': '2015'])
3
Example 37
def register():
units.registry[lib.Timestamp] = DatetimeConverter()
units.registry[Period] = PeriodConverter()
units.registry[pydt.datetime] = DatetimeConverter()
units.registry[pydt.date] = DatetimeConverter()
units.registry[pydt.time] = TimeConverter()
units.registry[np.datetime64] = DatetimeConverter()
3
Example 38
Project: aospy Source File: timedate.py
@staticmethod
def ymd_to_numpy(year, month, day):
"""Create a numpy.datetime64 with the given year, month, and day."""
return np.datetime64(
'{:04d}-{:02d}-{:02d}'.format(year, month, day)
)
3
Example 39
def convert_dstime_datetimes(self, dsTime):
''' Convert time variable to np.datetime64 '''
dsDatetimes = np.array([np.datetime64(self.timeCalendarStart) + day
for day in dsTime]).astype('M8[s]')
return dsDatetimes
3
Example 40
Project: qPython Source File: publisher.py
def get_ask_data(self):
c = random.randint(1, 10)
today = numpy.datetime64(datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0))
time = [numpy.timedelta64((numpy.datetime64(datetime.datetime.now()) - today), 'ms') for x in range(c)]
instr = ['instr_%d' % random.randint(1, 100) for x in range(c)]
src = ['qPython' for x in range(c)]
ask = [random.random() * random.randint(1, 100) for x in range(c)]
data = [qlist(time, qtype=QTIME_LIST), qlist(instr, qtype=QSYMBOL_LIST), qlist(src, qtype=QSYMBOL_LIST), qlist(ask, qtype=QFLOAT_LIST)]
print(data)
return data
3
Example 41
def convert_dstime_datetimes(self, dsTime):
''' Convert time variable to np.datetime64 '''
dsDatetimes = np.array([np.datetime64(self.t0 + dt.timedelta(seconds=int(day)))
for day in dsTime]).astype('M8[s]')
return dsDatetimes
3
Example 42
Project: xarray Source File: conventions.py
def _encode_datetime_with_netcdf4(dates, units, calendar):
"""Fallback method for encoding dates using netCDF4-python.
This method is more flexible than xarray's parsing using datetime64[ns]
arrays but also slower because it loops over each element.
"""
import netCDF4 as nc4
if np.issubdtype(dates.dtype, np.datetime64):
# numpy's broken datetime conversion only works for us precision
dates = dates.astype('M8[us]').astype(datetime)
def encode_datetime(d):
return np.nan if d is None else nc4.date2num(d, units, calendar)
return np.vectorize(encode_datetime)(dates)
3
Example 43
Project: numba Source File: test_support.py
def test_npdatetime(self):
a = np.datetime64('1900', 'Y')
b = np.datetime64('1900', 'Y')
c = np.datetime64('1900-01-01', 'D')
d = np.datetime64('1901', 'Y')
self.eq(a, b)
# Different unit
self.ne(a, c)
# Different value
self.ne(a, d)
3
Example 44
def _test_date_time(self, dt_str='now'):
dt0 = np.datetime64(dt_str)
data_str, time_str = np.datetime_as_string(dt0).split('T')
self.tree.add_node("General")
self.tree.General.date = data_str
self.tree.General.time = time_str
dt1 = np.datetime64('%sT%s' % (self.tree.General.date,
self.tree.General.time))
np.testing.assert_equal(dt0, dt1)
return dt1
3
Example 45
Project: nsepy Source File: archives.py
def str_to_date(d):
k = d.split('-')
import calendar
lookup = dict((v,k) for k,v in enumerate(calendar.month_abbr))
return np.datetime64(k[2] + '-' + str(lookup[k[1]]).zfill(2) + '-' + k[0])
3
Example 46
Project: plotly.py Source File: test_utils.py
def test_numpy_dates():
a = np.arange(np.datetime64('2011-07-11'), np.datetime64('2011-07-18'))
j1 = json.dumps(a, cls=utils.PlotlyJSONEncoder)
assert(j1 == '["2011-07-11", "2011-07-12", "2011-07-13", '
'"2011-07-14", "2011-07-15", "2011-07-16", '
'"2011-07-17"]')
3
Example 47
Project: xarray Source File: conventions.py
def maybe_encode_datetime(var):
if np.issubdtype(var.dtype, np.datetime64):
dims, data, attrs, encoding = _var_as_tuple(var)
(data, units, calendar) = encode_cf_datetime(
data, encoding.pop('units', None), encoding.pop('calendar', None))
safe_setitem(attrs, 'units', units)
safe_setitem(attrs, 'calendar', calendar)
var = Variable(dims, data, attrs, encoding)
return var
3
Example 48
Project: xarray Source File: test_variable.py
def test_datetime64_conversion_scalar(self):
expected = np.datetime64('2000-01-01T00:00:00Z', 'ns')
for values in [
np.datetime64('2000-01-01T00Z'),
pd.Timestamp('2000-01-01T00'),
datetime(2000, 1, 1),
]:
v = Variable([], values)
self.assertEqual(v.dtype, np.dtype('datetime64[ns]'))
self.assertEqual(v.values, expected)
self.assertEqual(v.values.dtype, np.dtype('datetime64[ns]'))
3
Example 49
Project: blaze Source File: test_numpy_compute.py
def test_numpy_and_python_datetime_truncate_agree_on_start_of_week():
s = symbol('s', 'datetime')
n = np.datetime64('2014-11-11')
p = datetime(2014, 11, 11)
expr = s.truncate(1, 'week')
assert compute(expr, n) == compute(expr, p)
3
Example 50
Project: AWS-Lambda-ML-Microservice-Skeleton Source File: _iotools.py
@classmethod
def _dtypeortype(cls, dtype):
"""Returns dtype for datetime64 and type of dtype otherwise."""
if dtype.type == np.datetime64:
return dtype
return dtype.type