Python pandas.util.testing.assert_almost_equal() Examples
The following are 30
code examples of pandas.util.testing.assert_almost_equal().
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
pandas.util.testing
, or try the search function
.
Example #1
Source File: test_eval.py From recruit with Apache License 2.0 | 9 votes |
def check_pow(self, lhs, arith1, rhs): ex = 'lhs {0} rhs'.format(arith1) expected = self.get_expected_pow_result(lhs, rhs) result = pd.eval(ex, engine=self.engine, parser=self.parser) if (is_scalar(lhs) and is_scalar(rhs) and _is_py3_complex_incompat(result, expected)): pytest.raises(AssertionError, tm.assert_numpy_array_equal, result, expected) else: tm.assert_almost_equal(result, expected) ex = '(lhs {0} rhs) {0} rhs'.format(arith1) result = pd.eval(ex, engine=self.engine, parser=self.parser) expected = self.get_expected_pow_result( self.get_expected_pow_result(lhs, rhs), rhs) tm.assert_almost_equal(result, expected)
Example #2
Source File: test_eval.py From recruit with Apache License 2.0 | 9 votes |
def check_compound_invert_op(self, lhs, cmp1, rhs): skip_these = 'in', 'not in' ex = '~(lhs {0} rhs)'.format(cmp1) if is_scalar(rhs) and cmp1 in skip_these: pytest.raises(TypeError, pd.eval, ex, engine=self.engine, parser=self.parser, local_dict={'lhs': lhs, 'rhs': rhs}) else: # compound if is_scalar(lhs) and is_scalar(rhs): lhs, rhs = map(lambda x: np.array([x]), (lhs, rhs)) expected = _eval_single_bin(lhs, cmp1, rhs, self.engine) if is_scalar(expected): expected = not expected else: expected = ~expected result = pd.eval(ex, engine=self.engine, parser=self.parser) tm.assert_almost_equal(expected, result) # make sure the other engines work the same as this one for engine in self.current_engines: ev = pd.eval(ex, engine=self.engine, parser=self.parser) tm.assert_almost_equal(ev, result)
Example #3
Source File: test_eval.py From recruit with Apache License 2.0 | 8 votes |
def check_chained_cmp_op(self, lhs, cmp1, mid, cmp2, rhs): def check_operands(left, right, cmp_op): return _eval_single_bin(left, cmp_op, right, self.engine) lhs_new = check_operands(lhs, mid, cmp1) rhs_new = check_operands(mid, rhs, cmp2) if lhs_new is not None and rhs_new is not None: ex1 = 'lhs {0} mid {1} rhs'.format(cmp1, cmp2) ex2 = 'lhs {0} mid and mid {1} rhs'.format(cmp1, cmp2) ex3 = '(lhs {0} mid) & (mid {1} rhs)'.format(cmp1, cmp2) expected = _eval_single_bin(lhs_new, '&', rhs_new, self.engine) for ex in (ex1, ex2, ex3): result = pd.eval(ex, engine=self.engine, parser=self.parser) tm.assert_almost_equal(result, expected)
Example #4
Source File: test_frame.py From recruit with Apache License 2.0 | 6 votes |
def test_errorbar_asymmetrical(self): np.random.seed(0) err = np.random.rand(3, 2, 5) # each column is [0, 1, 2, 3, 4], [3, 4, 5, 6, 7]... df = DataFrame(np.arange(15).reshape(3, 5)).T ax = df.plot(yerr=err, xerr=err / 2) yerr_0_0 = ax.collections[1].get_paths()[0].vertices[:, 1] expected_0_0 = err[0, :, 0] * np.array([-1, 1]) tm.assert_almost_equal(yerr_0_0, expected_0_0) with pytest.raises(ValueError): df.plot(yerr=err.T) tm.close() # This XPASSES when tested with mpl == 3.0.1
Example #5
Source File: test_chaining_and_caching.py From recruit with Apache License 2.0 | 6 votes |
def test_slice_consolidate_invalidate_item_cache(self): # this is chained assignment, but will 'work' with option_context('chained_assignment', None): # #3970 df = DataFrame({"aa": compat.lrange(5), "bb": [2.2] * 5}) # Creates a second float block df["cc"] = 0.0 # caches a reference to the 'bb' series df["bb"] # repr machinery triggers consolidation repr(df) # Assignment to wrong series df['bb'].iloc[0] = 0.17 df._clear_item_cache() tm.assert_almost_equal(df['bb'][0], 0.17)
Example #6
Source File: test_setitem.py From recruit with Apache License 2.0 | 6 votes |
def test_frame_getitem_setitem_boolean( self, multiindex_dataframe_random_data): frame = multiindex_dataframe_random_data df = frame.T.copy() values = df.values result = df[df > 0] expected = df.where(df > 0) tm.assert_frame_equal(result, expected) df[df > 0] = 5 values[values > 0] = 5 tm.assert_almost_equal(df.values, values) df[df == 5] = 0 values[values == 5] = 0 tm.assert_almost_equal(df.values, values) # a df that needs alignment first df[df[:-1] < 0] = 2 np.putmask(values[:-1], values[:-1] < 0, 2) tm.assert_almost_equal(df.values, values) with pytest.raises(TypeError, match='boolean values only'): df[df * 0] = 2
Example #7
Source File: common.py From recruit with Apache License 2.0 | 6 votes |
def check_values(self, f, func, values=False): if f is None: return axes = f.axes indicies = itertools.product(*axes) for i in indicies: result = getattr(f, func)[i] # check against values if values: expected = f.values[i] else: expected = f for a in reversed(i): expected = expected.__getitem__(a) tm.assert_almost_equal(result, expected)
Example #8
Source File: test_nanops.py From recruit with Apache License 2.0 | 6 votes |
def test_nanvar_ddof(self): n = 5 samples = self.prng.uniform(size=(10000, n + 1)) samples[:, -1] = np.nan # Force use of our own algorithm. variance_0 = nanops.nanvar(samples, axis=1, skipna=True, ddof=0).mean() variance_1 = nanops.nanvar(samples, axis=1, skipna=True, ddof=1).mean() variance_2 = nanops.nanvar(samples, axis=1, skipna=True, ddof=2).mean() # The unbiased estimate. var = 1.0 / 12 tm.assert_almost_equal(variance_1, var, check_less_precise=2) # The underestimated variance. tm.assert_almost_equal(variance_0, (n - 1.0) / n * var, check_less_precise=2) # The overestimated variance. tm.assert_almost_equal(variance_2, (n - 1.0) / (n - 2.0) * var, check_less_precise=2)
Example #9
Source File: test_panel.py From recruit with Apache License 2.0 | 6 votes |
def test_set_value(self): for item in self.panel.items: for mjr in self.panel.major_axis[::2]: for mnr in self.panel.minor_axis: with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): self.panel.set_value(item, mjr, mnr, 1.) tm.assert_almost_equal(self.panel[item][mnr][mjr], 1.) # resize with catch_warnings(): simplefilter("ignore", FutureWarning) res = self.panel.set_value('ItemE', 'foo', 'bar', 1.5) assert isinstance(res, Panel) assert res is not self.panel assert res.get_value('ItemE', 'foo', 'bar') == 1.5 res3 = self.panel.set_value('ItemE', 'foobar', 'baz', 5) assert is_float_dtype(res3['ItemE'].values) msg = ("There must be an argument for each " "axis plus the value provided") with pytest.raises(TypeError, match=msg): self.panel.set_value('a')
Example #10
Source File: test_panel.py From recruit with Apache License 2.0 | 6 votes |
def test_constructor_cast(self): zero_filled = self.panel.fillna(0) casted = Panel(zero_filled._data, dtype=int) casted2 = Panel(zero_filled.values, dtype=int) exp_values = zero_filled.values.astype(int) assert_almost_equal(casted.values, exp_values) assert_almost_equal(casted2.values, exp_values) casted = Panel(zero_filled._data, dtype=np.int32) casted2 = Panel(zero_filled.values, dtype=np.int32) exp_values = zero_filled.values.astype(np.int32) assert_almost_equal(casted.values, exp_values) assert_almost_equal(casted2.values, exp_values) # can't cast data = [[['foo', 'bar', 'baz']]] pytest.raises(ValueError, Panel, data, dtype=float)
Example #11
Source File: test_apply.py From recruit with Apache License 2.0 | 6 votes |
def test_apply(frame): applied = frame.apply(np.sqrt) assert isinstance(applied, SparseDataFrame) tm.assert_almost_equal(applied.values, np.sqrt(frame.values)) # agg / broadcast with tm.assert_produces_warning(FutureWarning): broadcasted = frame.apply(np.sum, broadcast=True) assert isinstance(broadcasted, SparseDataFrame) with tm.assert_produces_warning(FutureWarning): exp = frame.to_dense().apply(np.sum, broadcast=True) tm.assert_frame_equal(broadcasted.to_dense(), exp) applied = frame.apply(np.sum) tm.assert_series_equal(applied, frame.to_dense().apply(nanops.nansum).to_sparse())
Example #12
Source File: test_scalar_compat.py From recruit with Apache License 2.0 | 6 votes |
def test_tdi_total_seconds(self): # GH#10939 # test index rng = timedelta_range('1 days, 10:11:12.100123456', periods=2, freq='s') expt = [1 * 86400 + 10 * 3600 + 11 * 60 + 12 + 100123456. / 1e9, 1 * 86400 + 10 * 3600 + 11 * 60 + 13 + 100123456. / 1e9] tm.assert_almost_equal(rng.total_seconds(), Index(expt)) # test Series ser = Series(rng) s_expt = Series(expt, index=[0, 1]) tm.assert_series_equal(ser.dt.total_seconds(), s_expt) # with nat ser[1] = np.nan s_expt = Series([1 * 86400 + 10 * 3600 + 11 * 60 + 12 + 100123456. / 1e9, np.nan], index=[0, 1]) tm.assert_series_equal(ser.dt.total_seconds(), s_expt) # with both nat ser = Series([np.nan, np.nan], dtype='timedelta64[ns]') tm.assert_series_equal(ser.dt.total_seconds(), Series([np.nan, np.nan], index=[0, 1]))
Example #13
Source File: common.py From recruit with Apache License 2.0 | 6 votes |
def _check_data(self, xp, rs): """ Check each axes has identical lines Parameters ---------- xp : matplotlib Axes object rs : matplotlib Axes object """ xp_lines = xp.get_lines() rs_lines = rs.get_lines() def check_line(xpl, rsl): xpdata = xpl.get_xydata() rsdata = rsl.get_xydata() tm.assert_almost_equal(xpdata, rsdata) assert len(xp_lines) == len(rs_lines) [check_line(xpl, rsl) for xpl, rsl in zip(xp_lines, rs_lines)] tm.close()
Example #14
Source File: test_nanops.py From recruit with Apache License 2.0 | 5 votes |
def test_nans_skipna(self): samples = np.hstack([self.samples, np.nan]) skew = nanops.nanskew(samples, skipna=True) tm.assert_almost_equal(skew, self.actual_skew)
Example #15
Source File: test_nanops.py From recruit with Apache License 2.0 | 5 votes |
def test_axis(self): samples = np.vstack([self.samples, np.nan * np.ones(len(self.samples))]) skew = nanops.nanskew(samples, axis=1) tm.assert_almost_equal(skew, np.array([self.actual_skew, np.nan]))
Example #16
Source File: test_eval.py From recruit with Apache License 2.0 | 5 votes |
def test_binary_functions(self): df = DataFrame({'a': np.random.randn(10), 'b': np.random.randn(10)}) a = df.a b = df.b for fn in self.binary_fns: expr = "{0}(a, b)".format(fn) got = self.eval(expr) with np.errstate(all='ignore'): expect = getattr(np, fn)(a, b) tm.assert_almost_equal(got, expect, check_names=False)
Example #17
Source File: test_eval.py From recruit with Apache License 2.0 | 5 votes |
def check_alignment(self, result, nlhs, ghs, op): try: nlhs, ghs = nlhs.align(ghs) except (ValueError, TypeError, AttributeError): # ValueError: series frame or frame series align # TypeError, AttributeError: series or frame with scalar align pass else: expected = eval('nlhs {0} ghs'.format(op)) tm.assert_almost_equal(result, expected)
Example #18
Source File: test_eval.py From recruit with Apache License 2.0 | 5 votes |
def check_modulus(self, lhs, arith1, rhs): ex = 'lhs {0} rhs'.format(arith1) result = pd.eval(ex, engine=self.engine, parser=self.parser) expected = lhs % rhs tm.assert_almost_equal(result, expected) expected = _eval_single_bin(expected, arith1, rhs, self.engine) tm.assert_almost_equal(result, expected)
Example #19
Source File: test_nanops.py From recruit with Apache License 2.0 | 5 votes |
def test_axis(self): samples = np.vstack([self.samples, np.nan * np.ones(len(self.samples))]) kurt = nanops.nankurt(samples, axis=1) tm.assert_almost_equal(kurt, np.array([self.actual_kurt, np.nan]))
Example #20
Source File: test_api.py From recruit with Apache License 2.0 | 5 votes |
def test_tab_completion_with_categorical(self): # test the tab completion display ok_for_cat = ['name', 'index', 'categorical', 'categories', 'codes', 'ordered', 'set_categories', 'add_categories', 'remove_categories', 'rename_categories', 'reorder_categories', 'remove_unused_categories', 'as_ordered', 'as_unordered'] def get_dir(s): results = [r for r in s.cat.__dir__() if not r.startswith('_')] return list(sorted(set(results))) s = Series(list('aabbcde')).astype('category') results = get_dir(s) tm.assert_almost_equal(results, list(sorted(set(ok_for_cat))))
Example #21
Source File: test_timezones.py From recruit with Apache License 2.0 | 5 votes |
def test_string_index_alias_tz_aware(self, tz): rng = date_range('1/1/2000', periods=10, tz=tz) ser = Series(np.random.randn(len(rng)), index=rng) result = ser['1/3/2000'] tm.assert_almost_equal(result, ser[2]) # TODO: De-duplicate with test below
Example #22
Source File: test_eval.py From recruit with Apache License 2.0 | 5 votes |
def check_modulus(self, lhs, arith1, rhs): ex = 'lhs {0} rhs'.format(arith1) result = pd.eval(ex, engine=self.engine, parser=self.parser) expected = lhs % rhs tm.assert_almost_equal(result, expected) expected = self.ne.evaluate('expected {0} rhs'.format(arith1)) if isinstance(result, (DataFrame, Series)): tm.assert_almost_equal(result.values, expected) else: tm.assert_almost_equal(result, expected.item())
Example #23
Source File: test_eval.py From recruit with Apache License 2.0 | 5 votes |
def check_binary_arith_op(self, lhs, arith1, rhs): ex = 'lhs {0} rhs'.format(arith1) result = pd.eval(ex, engine=self.engine, parser=self.parser) expected = _eval_single_bin(lhs, arith1, rhs, self.engine) tm.assert_almost_equal(result, expected) ex = 'lhs {0} rhs {0} rhs'.format(arith1) result = pd.eval(ex, engine=self.engine, parser=self.parser) nlhs = _eval_single_bin(lhs, arith1, rhs, self.engine) self.check_alignment(result, nlhs, rhs, arith1)
Example #24
Source File: test_indexing.py From recruit with Apache License 2.0 | 5 votes |
def test_getitem_setitem_integers(): # caused bug without test s = Series([1, 2, 3], ['a', 'b', 'c']) assert s.iloc[0] == s['a'] s.iloc[0] = 5 tm.assert_almost_equal(s['a'], 5)
Example #25
Source File: test_api.py From recruit with Apache License 2.0 | 5 votes |
def check(self, namespace, expected, ignored=None): # see which names are in the namespace, minus optional # ignored ones # compare vs the expected result = sorted(f for f in dir(namespace) if not f.startswith('_')) if ignored is not None: result = sorted(list(set(result) - set(ignored))) expected = sorted(expected) tm.assert_almost_equal(result, expected)
Example #26
Source File: test_converter.py From recruit with Apache License 2.0 | 5 votes |
def test_conversion_float(self): decimals = 9 rs = self.dtc.convert( Timestamp('2012-1-1 01:02:03', tz='UTC'), None, None) xp = converter.dates.date2num(Timestamp('2012-1-1 01:02:03', tz='UTC')) tm.assert_almost_equal(rs, xp, decimals) rs = self.dtc.convert( Timestamp('2012-1-1 09:02:03', tz='Asia/Hong_Kong'), None, None) tm.assert_almost_equal(rs, xp, decimals) rs = self.dtc.convert(datetime(2012, 1, 1, 1, 2, 3), None, None) tm.assert_almost_equal(rs, xp, decimals)
Example #27
Source File: test_analytics.py From recruit with Apache License 2.0 | 5 votes |
def test_corr(self, datetime_series): import scipy.stats as stats # full overlap tm.assert_almost_equal(datetime_series.corr(datetime_series), 1) # partial overlap tm.assert_almost_equal(datetime_series[:15].corr(datetime_series[5:]), 1) assert isna(datetime_series[:15].corr(datetime_series[5:], min_periods=12)) ts1 = datetime_series[:15].reindex(datetime_series.index) ts2 = datetime_series[5:].reindex(datetime_series.index) assert isna(ts1.corr(ts2, min_periods=12)) # No overlap assert np.isnan(datetime_series[::2].corr(datetime_series[1::2])) # all NA cp = datetime_series[:10].copy() cp[:] = np.nan assert isna(cp.corr(cp)) A = tm.makeTimeSeries() B = tm.makeTimeSeries() result = A.corr(B) expected, _ = stats.pearsonr(A, B) tm.assert_almost_equal(result, expected)
Example #28
Source File: test_series.py From recruit with Apache License 2.0 | 5 votes |
def test_bar_log(self): expected = np.array([1e-1, 1e0, 1e1, 1e2, 1e3, 1e4]) _, ax = self.plt.subplots() ax = Series([200, 500]).plot.bar(log=True, ax=ax) tm.assert_numpy_array_equal(ax.yaxis.get_ticklocs(), expected) tm.close() _, ax = self.plt.subplots() ax = Series([200, 500]).plot.barh(log=True, ax=ax) tm.assert_numpy_array_equal(ax.xaxis.get_ticklocs(), expected) tm.close() # GH 9905 expected = np.array([1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1e0, 1e1]) _, ax = self.plt.subplots() ax = Series([0.1, 0.01, 0.001]).plot(log=True, kind='bar', ax=ax) ymin = 0.0007943282347242822 ymax = 0.12589254117941673 res = ax.get_ylim() tm.assert_almost_equal(res[0], ymin) tm.assert_almost_equal(res[1], ymax) tm.assert_numpy_array_equal(ax.yaxis.get_ticklocs(), expected) tm.close() _, ax = self.plt.subplots() ax = Series([0.1, 0.01, 0.001]).plot(log=True, kind='barh', ax=ax) res = ax.get_xlim() tm.assert_almost_equal(res[0], ymin) tm.assert_almost_equal(res[1], ymax) tm.assert_numpy_array_equal(ax.xaxis.get_ticklocs(), expected)
Example #29
Source File: test_scalar.py From recruit with Apache License 2.0 | 5 votes |
def test_at_and_iat_set(self): def _check(f, func, values=False): if f is not None: indicies = self.generate_indices(f, values) for i in indicies: getattr(f, func)[i] = 1 expected = self.get_value(f, i, values) tm.assert_almost_equal(expected, 1) for t in self._objs: d = getattr(self, t) # iat for f in [d['ints'], d['uints']]: _check(f, 'iat', values=True) for f in [d['labels'], d['ts'], d['floats']]: if f is not None: pytest.raises(ValueError, _check, f, 'iat') # at for f in [d['ints'], d['uints'], d['labels'], d['ts'], d['floats']]: _check(f, 'at')
Example #30
Source File: reduce.py From recruit with Apache License 2.0 | 5 votes |
def check_reduce(self, s, op_name, skipna): result = getattr(s, op_name)(skipna=skipna) expected = getattr(s.astype('float64'), op_name)(skipna=skipna) tm.assert_almost_equal(result, expected)