本文整理汇总了Python中numpy.NaN方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.NaN方法的具体用法?Python numpy.NaN怎么用?Python numpy.NaN使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.NaN方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: render
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import NaN [as 别名]
def render(self, take_screenshot=False, output_type=0):
# self.render_timer.tic()
self._actual_render()
# self.render_timer.toc(log_at=1000, log_str='render timer', type='time')
np_rgb_img = None
np_d_img = None
c = 1000.
if take_screenshot:
if self.modality == 'rgb':
screenshot_rgba = np.zeros((self.height, self.width, 4), dtype=np.uint8)
glReadPixels(0, 0, self.width, self.height, GL_RGBA, GL_UNSIGNED_BYTE, screenshot_rgba)
np_rgb_img = screenshot_rgba[::-1,:,:3];
if self.modality == 'depth':
screenshot_d = np.zeros((self.height, self.width, 4), dtype=np.uint8)
glReadPixels(0, 0, self.width, self.height, GL_RGBA, GL_UNSIGNED_BYTE, screenshot_d)
np_d_img = screenshot_d[::-1,:,:3];
np_d_img = np_d_img[:,:,2]*(255.*255./c) + np_d_img[:,:,1]*(255./c) + np_d_img[:,:,0]*(1./c)
np_d_img = np_d_img.astype(np.float32)
np_d_img[np_d_img == 0] = np.NaN
np_d_img = np_d_img[:,:,np.newaxis]
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
return np_rgb_img, np_d_img
示例2: test_basic_equality
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import NaN [as 别名]
def test_basic_equality(self):
self.meta['new1'] = {'units': 'hey1', 'long_name': 'crew'}
self.meta['new2'] = {'units': 'hey', 'long_name': 'boo',
'description': 'boohoo', 'fill': np.NaN}
# ensure things are the same
meta2 = self.meta.copy()
assert (meta2 == self.meta)
# different way to create meta object
meta3 = pysat.Meta()
meta3['new1'] = self.meta['new1']
meta3['new2'] = self.meta['new2']
assert (meta3 == self.meta)
# make sure differences matter
self.meta['new2'] = {'fill': 1}
assert not (meta2 == self.meta)
示例3: _to_primitive
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import NaN [as 别名]
def _to_primitive(arr, string_max_len=None, forced_dtype=None):
if arr.dtype.hasobject:
if len(arr) > 0 and isinstance(arr[0], Timestamp):
return np.array([t.value for t in arr], dtype=DTN64_DTYPE)
if forced_dtype is not None:
casted_arr = arr.astype(dtype=forced_dtype, copy=False)
elif string_max_len is not None:
casted_arr = np.array(arr.astype('U{:d}'.format(string_max_len)))
else:
casted_arr = np.array(list(arr))
# Pick any unwanted data conversions (e.g. np.NaN to 'nan')
if np.array_equal(arr, casted_arr):
return casted_arr
return arr
示例4: assert_series_equal
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import NaN [as 别名]
def assert_series_equal(self, left, right, *args, **kwargs):
def convert(x):
# need to convert array([Decimal(NaN)], dtype='object') to np.NaN
# because Series[object].isnan doesn't recognize decimal(NaN) as
# NA.
try:
return math.isnan(x)
except TypeError:
return False
if left.dtype == 'object':
left_na = left.apply(convert)
else:
left_na = left.isna()
if right.dtype == 'object':
right_na = right.apply(convert)
else:
right_na = right.isna()
tm.assert_series_equal(left_na, right_na)
return tm.assert_series_equal(left[~left_na],
right[~right_na],
*args, **kwargs)
示例5: test_rolling_skew_edge_cases
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import NaN [as 别名]
def test_rolling_skew_edge_cases(self):
all_nan = Series([np.NaN] * 5)
# yields all NaN (0 variance)
d = Series([1] * 5)
x = d.rolling(window=5).skew()
tm.assert_series_equal(all_nan, x)
# yields all NaN (window too small)
d = Series(np.random.randn(5))
x = d.rolling(window=2).skew()
tm.assert_series_equal(all_nan, x)
# yields [NaN, NaN, NaN, 0.177994, 1.548824]
d = Series([-1.50837035, -0.1297039, 0.19501095, 1.73508164, 0.41941401
])
expected = Series([np.NaN, np.NaN, np.NaN, 0.177994, 1.548824])
x = d.rolling(window=4).skew()
tm.assert_series_equal(expected, x)
示例6: test_dense_to_sparse
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import NaN [as 别名]
def test_dense_to_sparse(self):
series = self.bseries.to_dense()
bseries = series.to_sparse(kind='block')
iseries = series.to_sparse(kind='integer')
tm.assert_sp_series_equal(bseries, self.bseries)
tm.assert_sp_series_equal(iseries, self.iseries, check_names=False)
assert iseries.name == self.bseries.name
assert len(series) == len(bseries)
assert len(series) == len(iseries)
assert series.shape == bseries.shape
assert series.shape == iseries.shape
# non-NaN fill value
series = self.zbseries.to_dense()
zbseries = series.to_sparse(kind='block', fill_value=0)
ziseries = series.to_sparse(kind='integer', fill_value=0)
tm.assert_sp_series_equal(zbseries, self.zbseries)
tm.assert_sp_series_equal(ziseries, self.ziseries, check_names=False)
assert ziseries.name == self.zbseries.name
assert len(series) == len(zbseries)
assert len(series) == len(ziseries)
assert series.shape == zbseries.shape
assert series.shape == ziseries.shape
示例7: test_constructor_from_series
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import NaN [as 别名]
def test_constructor_from_series(self):
# GH 2873
x = Series(np.random.randn(10000), name='a')
x = x.to_sparse(fill_value=0)
assert isinstance(x, SparseSeries)
df = SparseDataFrame(x)
assert isinstance(df, SparseDataFrame)
x = Series(np.random.randn(10000), name='a')
y = Series(np.random.randn(10000), name='b')
x2 = x.astype(float)
x2.loc[:9998] = np.NaN
# TODO: x_sparse is unused...fix
x_sparse = x2.to_sparse(fill_value=np.NaN) # noqa
# Currently fails too with weird ufunc error
# df1 = SparseDataFrame([x_sparse, y])
y.loc[:9998] = 0
# TODO: y_sparse is unsused...fix
y_sparse = y.to_sparse(fill_value=0) # noqa
# without sparse value raises error
# df2 = SparseDataFrame([x2_sparse, y])
示例8: test_astype
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import NaN [as 别名]
def test_astype(self):
# GH 13149, GH 13209
idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN])
result = idx.astype(object)
expected = Index([Timestamp('2016-05-16')] + [NaT] * 3, dtype=object)
tm.assert_index_equal(result, expected)
result = idx.astype(int)
expected = Int64Index([1463356800000000000] +
[-9223372036854775808] * 3, dtype=np.int64)
tm.assert_index_equal(result, expected)
rng = date_range('1/1/2000', periods=10)
result = rng.astype('i8')
tm.assert_index_equal(result, Index(rng.asi8))
tm.assert_numpy_array_equal(result.values, rng.asi8)
示例9: test_astype_conversion
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import NaN [as 别名]
def test_astype_conversion(self):
# GH#13149, GH#13209
idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq='D')
result = idx.astype(object)
expected = Index([Period('2016-05-16', freq='D')] +
[Period(NaT, freq='D')] * 3, dtype='object')
tm.assert_index_equal(result, expected)
result = idx.astype(np.int64)
expected = Int64Index([16937] + [-9223372036854775808] * 3,
dtype=np.int64)
tm.assert_index_equal(result, expected)
result = idx.astype(str)
expected = Index(str(x) for x in idx)
tm.assert_index_equal(result, expected)
idx = period_range('1990', '2009', freq='A')
result = idx.astype('i8')
tm.assert_index_equal(result, Index(idx.asi8))
tm.assert_numpy_array_equal(result.values, idx.asi8)
示例10: test_count
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import NaN [as 别名]
def test_count(self, datetime_series):
assert datetime_series.count() == len(datetime_series)
datetime_series[::2] = np.NaN
assert datetime_series.count() == np.isfinite(datetime_series).sum()
mi = MultiIndex.from_arrays([list('aabbcc'), [1, 2, 2, nan, 1, 2]])
ts = Series(np.arange(len(mi)), index=mi)
left = ts.count(level=1)
right = Series([2, 3, 1], index=[1, 2, nan])
assert_series_equal(left, right)
ts.iloc[[0, 3, 5]] = nan
assert_series_equal(ts.count(level=1), right - 1)
示例11: test_scalar
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import NaN [as 别名]
def test_scalar(self):
N = 30
rng = date_range('1/1/1990', periods=N, freq='53s')
ts = Series(np.arange(N), index=rng)
ts[5:10] = np.NaN
ts[15:20] = np.NaN
val1 = ts.asof(ts.index[7])
val2 = ts.asof(ts.index[19])
assert val1 == ts[4]
assert val2 == ts[14]
# accepts strings
val1 = ts.asof(str(ts.index[7]))
assert val1 == ts[4]
# in there
result = ts.asof(ts.index[3])
assert result == ts[3]
# no as of value
d = ts.index[0] - offsets.BDay()
assert np.isnan(ts.asof(d))
示例12: test_fromValue
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import NaN [as 别名]
def test_fromValue(self, datetime_series):
nans = Series(np.NaN, index=datetime_series.index)
assert nans.dtype == np.float_
assert len(nans) == len(datetime_series)
strings = Series('foo', index=datetime_series.index)
assert strings.dtype == np.object_
assert len(strings) == len(datetime_series)
d = datetime.now()
dates = Series(d, index=datetime_series.index)
assert dates.dtype == 'M8[ns]'
assert len(dates) == len(datetime_series)
# GH12336
# Test construction of categorical series from value
categorical = Series(0, index=datetime_series.index, dtype="category")
expected = Series(0, index=datetime_series.index).astype("category")
assert categorical.dtype == 'category'
assert len(categorical) == len(datetime_series)
tm.assert_series_equal(categorical, expected)
示例13: test_unstack_to_series
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import NaN [as 别名]
def test_unstack_to_series(self):
# check reversibility
data = self.frame.unstack()
assert isinstance(data, Series)
undo = data.unstack().T
assert_frame_equal(undo, self.frame)
# check NA handling
data = DataFrame({'x': [1, 2, np.NaN], 'y': [3.0, 4, np.NaN]})
data.index = Index(['a', 'b', 'c'])
result = data.unstack()
midx = MultiIndex(levels=[['x', 'y'], ['a', 'b', 'c']],
codes=[[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]])
expected = Series([1, 2, np.NaN, 3, 4, np.NaN], index=midx)
assert_series_equal(result, expected)
# check composability of unstack
old_data = data.copy()
for _ in range(4):
data = data.unstack()
assert_frame_equal(old_data, data)
示例14: test_unstack_fill_frame_object
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import NaN [as 别名]
def test_unstack_fill_frame_object():
# GH12815 Test unstacking with object.
data = pd.Series(['a', 'b', 'c', 'a'], dtype='object')
data.index = pd.MultiIndex.from_tuples(
[('x', 'a'), ('x', 'b'), ('y', 'b'), ('z', 'a')])
# By default missing values will be NaN
result = data.unstack()
expected = pd.DataFrame(
{'a': ['a', np.nan, 'a'], 'b': ['b', 'c', np.nan]},
index=list('xyz')
)
assert_frame_equal(result, expected)
# Fill with any value replaces missing values as expected
result = data.unstack(fill_value='d')
expected = pd.DataFrame(
{'a': ['a', 'd', 'a'], 'b': ['b', 'c', 'd']},
index=list('xyz')
)
assert_frame_equal(result, expected)
示例15: test_isna_lists
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import NaN [as 别名]
def test_isna_lists(self):
result = isna([[False]])
exp = np.array([[False]])
tm.assert_numpy_array_equal(result, exp)
result = isna([[1], [2]])
exp = np.array([[False], [False]])
tm.assert_numpy_array_equal(result, exp)
# list of strings / unicode
result = isna(['foo', 'bar'])
exp = np.array([False, False])
tm.assert_numpy_array_equal(result, exp)
result = isna([u('foo'), u('bar')])
exp = np.array([False, False])
tm.assert_numpy_array_equal(result, exp)
# GH20675
result = isna([np.NaN, 'world'])
exp = np.array([True, False])
tm.assert_numpy_array_equal(result, exp)