Python numpy.NaN() Examples
The following are 30
code examples of numpy.NaN().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example #1
Source File: test_constructors.py From recruit with Apache License 2.0 | 6 votes |
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)
Example #2
Source File: numpy_records.py From arctic with GNU Lesser General Public License v2.1 | 6 votes |
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
Example #3
Source File: test_meta.py From pysat with BSD 3-Clause "New" or "Revised" License | 6 votes |
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)
Example #4
Source File: test_decimal.py From recruit with Apache License 2.0 | 6 votes |
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)
Example #5
Source File: test_astype.py From recruit with Apache License 2.0 | 6 votes |
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)
Example #6
Source File: test_analytics.py From recruit with Apache License 2.0 | 6 votes |
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)
Example #7
Source File: test_asof.py From recruit with Apache License 2.0 | 6 votes |
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))
Example #8
Source File: test_window.py From recruit with Apache License 2.0 | 6 votes |
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)
Example #9
Source File: test_series.py From recruit with Apache License 2.0 | 6 votes |
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
Example #10
Source File: test_reshape.py From recruit with Apache License 2.0 | 6 votes |
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)
Example #11
Source File: test_reshape.py From recruit with Apache License 2.0 | 6 votes |
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)
Example #12
Source File: test_astype.py From recruit with Apache License 2.0 | 6 votes |
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)
Example #13
Source File: test_frame.py From recruit with Apache License 2.0 | 6 votes |
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])
Example #14
Source File: test_missing.py From recruit with Apache License 2.0 | 6 votes |
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)
Example #15
Source File: swiftshader_renderer.py From DOTA_models with Apache License 2.0 | 6 votes |
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
Example #16
Source File: test_dtypes.py From recruit with Apache License 2.0 | 5 votes |
def test_astype_str_float(self, text_dtype): # see gh-11302 result = DataFrame([np.NaN]).astype(text_dtype) expected = DataFrame(["nan"]) assert_frame_equal(result, expected) result = DataFrame([1.12345678901234567890]).astype(text_dtype) # < 1.14 truncates # >= 1.14 preserves the full repr val = ("1.12345678901" if _np_version_under1p14 else "1.1234567890123457") expected = DataFrame([val]) assert_frame_equal(result, expected)
Example #17
Source File: test_astype.py From recruit with Apache License 2.0 | 5 votes |
def test_astype_raises(self, dtype): # GH 13149, GH 13209 idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN]) msg = 'Cannot cast DatetimeArray to dtype' with pytest.raises(TypeError, match=msg): idx.astype(dtype)
Example #18
Source File: test_block_internals.py From recruit with Apache License 2.0 | 5 votes |
def test_stale_cached_series_bug_473(self): # this is chained, but ok with option_context('chained_assignment', None): Y = DataFrame(np.random.random((4, 4)), index=('a', 'b', 'c', 'd'), columns=('e', 'f', 'g', 'h')) repr(Y) Y['e'] = Y['e'].astype('object') Y['g']['c'] = np.NaN repr(Y) result = Y.sum() # noqa exp = Y['g'].sum() # noqa assert pd.isna(Y['g']['c'])
Example #19
Source File: test_dtypes.py From recruit with Apache License 2.0 | 5 votes |
def test_astype_cast_nan_inf_int(self, val, dtype): # see gh-14265 # # Check NaN and inf --> raise error when converting to int. msg = "Cannot convert non-finite values \\(NA or inf\\) to integer" df = DataFrame([val]) with pytest.raises(ValueError, match=msg): df.astype(dtype)
Example #20
Source File: test_missing.py From recruit with Apache License 2.0 | 5 votes |
def test_bfill(self): ts = Series([0., 1., 2., 3., 4.], index=tm.makeDateIndex(5)) ts[2] = np.NaN assert_series_equal(ts.bfill(), ts.fillna(method='bfill'))
Example #21
Source File: test_missing.py From recruit with Apache License 2.0 | 5 votes |
def test_ffill(self): ts = Series([0., 1., 2., 3., 4.], index=tm.makeDateIndex(5)) ts[2] = np.NaN assert_series_equal(ts.ffill(), ts.fillna(method='ffill'))
Example #22
Source File: test_pivot.py From recruit with Apache License 2.0 | 5 votes |
def test_pivot_table_not_series(self): # GH 4386 # pivot_table always returns a DataFrame # when values is not list like and columns is None # and aggfunc is not instance of list df = DataFrame({'col1': [3, 4, 5], 'col2': ['C', 'D', 'E'], 'col3': [1, 3, 9]}) result = df.pivot_table('col1', index=['col3', 'col2'], aggfunc=np.sum) m = MultiIndex.from_arrays([[1, 3, 9], ['C', 'D', 'E']], names=['col3', 'col2']) expected = DataFrame([3, 4, 5], index=m, columns=['col1']) tm.assert_frame_equal(result, expected) result = df.pivot_table( 'col1', index='col3', columns='col2', aggfunc=np.sum ) expected = DataFrame([[3, np.NaN, np.NaN], [np.NaN, 4, np.NaN], [np.NaN, np.NaN, 5]], index=Index([1, 3, 9], name='col3'), columns=Index(['C', 'D', 'E'], name='col2')) tm.assert_frame_equal(result, expected) result = df.pivot_table('col1', index='col3', aggfunc=[np.sum]) m = MultiIndex.from_arrays([['sum'], ['col1']]) expected = DataFrame([3, 4, 5], index=Index([1, 3, 9], name='col3'), columns=m) tm.assert_frame_equal(result, expected)
Example #23
Source File: test_constructors.py From recruit with Apache License 2.0 | 5 votes |
def test_constructor(self, datetime_series, empty_series): assert datetime_series.index.is_all_dates # Pass in Series derived = Series(datetime_series) assert derived.index.is_all_dates assert tm.equalContents(derived.index, datetime_series.index) # Ensure new index is not created assert id(datetime_series.index) == id(derived.index) # Mixed type Series mixed = Series(['hello', np.NaN], index=[0, 1]) assert mixed.dtype == np.object_ assert mixed[1] is np.NaN assert not empty_series.index.is_all_dates assert not Series({}).index.is_all_dates # exception raised is of type Exception with pytest.raises(Exception, match="Data must be 1-dimensional"): Series(np.random.randn(3, 3), index=np.arange(3)) mixed.name = 'Series' rs = Series(mixed).name xp = 'Series' assert rs == xp # raise on MultiIndex GH4187 m = MultiIndex.from_arrays([[1, 2], [3, 4]]) msg = "initializing a Series from a MultiIndex is not supported" with pytest.raises(NotImplementedError, match=msg): Series(m)
Example #24
Source File: test_multilevel.py From recruit with Apache License 2.0 | 5 votes |
def setup_method(self, method): index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'], ['one', 'two', 'three']], codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], names=['first', 'second']) self.frame = DataFrame(np.random.randn(10, 3), index=index, columns=Index(['A', 'B', 'C'], name='exp')) self.single_level = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux']], codes=[[0, 1, 2, 3]], names=['first']) # create test series object arrays = [['bar', 'bar', 'baz', 'baz', 'qux', 'qux', 'foo', 'foo'], ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']] tuples = lzip(*arrays) index = MultiIndex.from_tuples(tuples) s = Series(randn(8), index=index) s[3] = np.NaN self.series = s self.tdf = tm.makeTimeDataFrame(100) self.ymd = self.tdf.groupby([lambda x: x.year, lambda x: x.month, lambda x: x.day]).sum() # use Int64Index, to make sure things work self.ymd.index.set_levels([lev.astype('i8') for lev in self.ymd.index.levels], inplace=True) self.ymd.index.set_names(['year', 'month', 'day'], inplace=True)
Example #25
Source File: test_period.py From recruit with Apache License 2.0 | 5 votes |
def test_pickle_round_trip(self, freq): idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq=freq) result = tm.round_trip_pickle(idx) tm.assert_index_equal(result, idx)
Example #26
Source File: test_astype.py From recruit with Apache License 2.0 | 5 votes |
def test_astype_raises(self, dtype): # GH#13149, GH#13209 idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq='D') msg = 'Cannot cast PeriodArray to dtype' with pytest.raises(TypeError, match=msg): idx.astype(dtype)
Example #27
Source File: test_astype.py From recruit with Apache License 2.0 | 5 votes |
def test_astype_raises(self, dtype): # GH 13149, GH 13209 idx = TimedeltaIndex([1e14, 'NaT', NaT, np.NaN]) msg = 'Cannot cast TimedeltaArray to dtype' with pytest.raises(TypeError, match=msg): idx.astype(dtype)
Example #28
Source File: test_astype.py From recruit with Apache License 2.0 | 5 votes |
def test_astype_str_compat(self): # GH 13149, GH 13209 # verify that we are returning NaT as a string (and not unicode) idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN]) result = idx.astype(str) expected = Index(['2016-05-16', 'NaT', 'NaT', 'NaT'], dtype=object) tm.assert_index_equal(result, expected)
Example #29
Source File: test_timezones.py From recruit with Apache License 2.0 | 5 votes |
def test_dti_tz_localize_ambiguous_nat(self, tz): times = ['11/06/2011 00:00', '11/06/2011 01:00', '11/06/2011 01:00', '11/06/2011 02:00', '11/06/2011 03:00'] di = DatetimeIndex(times) localized = di.tz_localize(tz, ambiguous='NaT') times = ['11/06/2011 00:00', np.NaN, np.NaN, '11/06/2011 02:00', '11/06/2011 03:00'] di_test = DatetimeIndex(times, tz='US/Eastern') # left dtype is datetime64[ns, US/Eastern] # right is datetime64[ns, tzfile('/usr/share/zoneinfo/US/Eastern')] tm.assert_numpy_array_equal(di_test.values, localized.values)
Example #30
Source File: test_datetime_index.py From recruit with Apache License 2.0 | 5 votes |
def test_resample_how_method(): # GH9915 s = Series([11, 22], index=[Timestamp('2015-03-31 21:48:52.672000'), Timestamp('2015-03-31 21:49:52.739000')]) expected = Series([11, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, 22], index=[Timestamp('2015-03-31 21:48:50'), Timestamp('2015-03-31 21:49:00'), Timestamp('2015-03-31 21:49:10'), Timestamp('2015-03-31 21:49:20'), Timestamp('2015-03-31 21:49:30'), Timestamp('2015-03-31 21:49:40'), Timestamp('2015-03-31 21:49:50')]) assert_series_equal(s.resample("10S").mean(), expected)