本文整理匯總了Python中numpy.isnat方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.isnat方法的具體用法?Python numpy.isnat怎麽用?Python numpy.isnat使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.isnat方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_isnat
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import isnat [as 別名]
def test_isnat(self):
assert_(np.isnat(np.datetime64('NaT', 'ms')))
assert_(np.isnat(np.datetime64('NaT', 'ns')))
assert_(not np.isnat(np.datetime64('2038-01-19T03:14:07')))
assert_(np.isnat(np.timedelta64('NaT', "ms")))
assert_(not np.isnat(np.timedelta64(34, "ms")))
res = np.array([False, False, True])
for unit in ['Y', 'M', 'W', 'D',
'h', 'm', 's', 'ms', 'us',
'ns', 'ps', 'fs', 'as']:
arr = np.array([123, -321, "NaT"], dtype='<datetime64[%s]' % unit)
assert_equal(np.isnat(arr), res)
arr = np.array([123, -321, "NaT"], dtype='>datetime64[%s]' % unit)
assert_equal(np.isnat(arr), res)
arr = np.array([123, -321, "NaT"], dtype='<timedelta64[%s]' % unit)
assert_equal(np.isnat(arr), res)
arr = np.array([123, -321, "NaT"], dtype='>timedelta64[%s]' % unit)
assert_equal(np.isnat(arr), res)
示例2: get_datetime_str
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import isnat [as 別名]
def get_datetime_str(arr: ndarray):
dt = {0: 'ns', 1: 'us', 2: 'ms', 3: 's', 4: 'D'}
arr = arr[~np.isnat(arr)].view('int64')
counts = np.zeros(len(arr), dtype='int64')
for i, val in enumerate(arr):
if val == 0:
counts[i] = 4
continue
dec = decimal.Decimal(int(val)).as_tuple()
ct = 0
for digit in dec.digits[::-1]:
if digit == 0:
ct += 1
else:
break
if ct >= 11:
counts[i] = 4
else:
counts[i] = ct // 3
return dt[counts.min()]
示例3: isna_overload
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import isnat [as 別名]
def isna_overload(arr, i):
if arr == string_array_type:
return lambda arr, i: sdc.str_arr_ext.str_arr_is_na(arr, i)
# TODO: support NaN in list(list(str))
if arr == list_string_array_type:
return lambda arr, i: False
# TODO: extend to other types
assert isinstance(arr, types.Array) or isinstance(arr, types.List)
dtype = arr.dtype
if isinstance(dtype, types.Float):
return lambda arr, i: np.isnan(arr[i])
# NaT for dt64
if isinstance(dtype, (types.NPDatetime, types.NPTimedelta)):
nat = dtype('NaT')
# TODO: replace with np.isnat
return lambda arr, i: arr[i] == nat
# XXX integers don't have nans, extend to boolean
return lambda arr, i: False
示例4: isnat
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import isnat [as 別名]
def isnat(val):
"""
Checks if the value is a NaT. Should only be called on datetimelike objects.
"""
if (isinstance(val, (np.datetime64, np.timedelta64)) or
(isinstance(val, np.ndarray) and val.dtype.kind == 'M')):
if numpy_version >= '1.13':
return np.isnat(val)
else:
return val.view('i8') == nat_as_integer
elif pd and val is pd.NaT:
return True
elif pd and isinstance(val, pandas_datetime_types+pandas_timedelta_types):
return pd.isna(val)
else:
return False
示例5: test_get_timestamps
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import isnat [as 別名]
def test_get_timestamps(self):
"""Test getting the timestamps."""
tline = self.reader._get_timestamps()
# First and last scanline have invalid timestamps (space)
self.assertTrue(np.isnat(tline[0]))
self.assertTrue(np.isnat(tline[-1]))
# Test remaining lines
year = tline.astype('datetime64[Y]').astype(int) + 1970
month = tline.astype('datetime64[M]').astype(int) % 12 + 1
day = (tline.astype('datetime64[D]') - tline.astype('datetime64[M]') + 1).astype(int)
msec = (tline - tline.astype('datetime64[D]')).astype(int)
self.assertTrue(np.all(year[1:-1] == 2016))
self.assertTrue(np.all(month[1:-1] == 3))
self.assertTrue(np.all(day[1:-1] == 3))
self.assertTrue(np.all(msec[1:-1] == np.arange(len(tline) - 2)))
示例6: set_jds
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import isnat [as 別名]
def set_jds(self, val1, val2):
# If there are any masked values in the ``val1`` datetime64 array
# ('NaT') then stub them with a valid date so downstream parse_string
# will work. The value under the mask is arbitrary but a "modern" date
# is good.
mask = np.isnat(val1)
masked = np.any(mask)
if masked:
val1 = val1.copy()
val1[mask] = '2000'
# Make sure M(onth) and Y(ear) dates will parse and convert to bytestring
if val1.dtype.name in ['datetime64[M]', 'datetime64[Y]']:
val1 = val1.astype('datetime64[D]')
val1 = val1.astype('S')
# Standard ISO string parsing now
super().set_jds(val1, val2)
# Finally apply mask if necessary
if masked:
self.jd2[mask] = np.nan
示例7: test_isnat_error
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import isnat [as 別名]
def test_isnat_error(self):
# Test that only datetime dtype arrays are accepted
for t in np.typecodes["All"]:
if t in np.typecodes["Datetime"]:
continue
assert_raises(TypeError, np.isnat, np.zeros(10, t))
示例8: _day
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import isnat [as 別名]
def _day(self, data):
if data.size < 5000:
days = (data.astype('datetime64[D]') - data.astype('datetime64[M]') + 1).astype('float64')
days[np.isnat(data)] = nan
else:
return _date.day(data.astype('int64'))
return days
示例9: _day_of_year
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import isnat [as 別名]
def _day_of_year(self, data):
if data.size < 2500:
doy = (data.astype('datetime64[D]') - data.astype('datetime64[Y]') + 1).astype('float64')
doy[np.isnat(data)] = nan
return doy
else:
return _date.day_of_year(data.astype('int64'))
示例10: _days_in_month
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import isnat [as 別名]
def _days_in_month(self, data):
if data.size < 5000:
month = data.astype('datetime64[M]')
next_month = (month + 1).astype('datetime64[D]')
dim = (next_month - month).astype('float64')
dim[np.isnat(data)] = nan
return dim
else:
return _date.days_in_month(data.astype('int64'))
示例11: _is_leap_year
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import isnat [as 別名]
def _is_leap_year(self, data):
if data.size < 500:
years = data.astype('datetime64[Y]').astype('float64') + 1970
years[np.isnat(data)] = nan
return np.where(years % 4 == 0, np.where(years % 100 == 0,
np.where(years % 400 == 0, True, False), True), False)
else:
return _date.is_leap_year(data.astype('int64'))
示例12: _quarter
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import isnat [as 別名]
def _quarter(self, data):
if data.size < 5000:
t = data.astype('datetime64[M]').astype('float64') % 12 // 3 + 1
t[np.isnat(data)] = nan
return t
else:
return _date.quarter(data.astype('int64'))
示例13: _seconds
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import isnat [as 別名]
def _seconds(self, data, total):
if total:
t = data.astype('float64') / 10 ** 9
t[np.isnat(data)] = nan
return t
else:
return _td.seconds(data.astype('int64'))
示例14: any_date
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import isnat [as 別名]
def any_date(arr, axis, **kwargs):
return (~np.isnat(arr)).sum(axis=axis) > 0
示例15: all_date
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import isnat [as 別名]
def all_date(arr, axis, **kwargs):
return (~np.isnat(arr)).sum(axis=axis) == arr.shape[0]