Python pandas.util.testing.assert_index_equal() Examples
The following are 30
code examples of pandas.util.testing.assert_index_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_scalar_compat.py From recruit with Apache License 2.0 | 7 votes |
def test_tdi_round(self): td = pd.timedelta_range(start='16801 days', periods=5, freq='30Min') elt = td[1] expected_rng = TimedeltaIndex([Timedelta('16801 days 00:00:00'), Timedelta('16801 days 00:00:00'), Timedelta('16801 days 01:00:00'), Timedelta('16801 days 02:00:00'), Timedelta('16801 days 02:00:00')]) expected_elt = expected_rng[1] tm.assert_index_equal(td.round(freq='H'), expected_rng) assert elt.round(freq='H') == expected_elt msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG with pytest.raises(ValueError, match=msg): td.round(freq='foo') with pytest.raises(ValueError, match=msg): elt.round(freq='foo') msg = "<MonthEnd> is a non-fixed frequency" with pytest.raises(ValueError, match=msg): td.round(freq='M') with pytest.raises(ValueError, match=msg): elt.round(freq='M')
Example #2
Source File: test_internals.py From recruit with Apache License 2.0 | 6 votes |
def test_take(self): def assert_take_ok(mgr, axis, indexer): mat = mgr.as_array() taken = mgr.take(indexer, axis) tm.assert_numpy_array_equal(np.take(mat, indexer, axis), taken.as_array(), check_dtype=False) tm.assert_index_equal(mgr.axes[axis].take(indexer), taken.axes[axis]) for mgr in self.MANAGERS: for ax in range(mgr.ndim): # take/fancy indexer assert_take_ok(mgr, ax, []) assert_take_ok(mgr, ax, [0, 0, 0]) assert_take_ok(mgr, ax, lrange(mgr.shape[ax])) if mgr.shape[ax] >= 3: assert_take_ok(mgr, ax, [0, 1, 2]) assert_take_ok(mgr, ax, [-1, -2, -3])
Example #3
Source File: test_panel.py From recruit with Apache License 2.0 | 6 votes |
def test_constructor_dict_mixed(self): data = {k: v.values for k, v in self.panel.iteritems()} result = Panel(data) exp_major = Index(np.arange(len(self.panel.major_axis))) tm.assert_index_equal(result.major_axis, exp_major) result = Panel(data, items=self.panel.items, major_axis=self.panel.major_axis, minor_axis=self.panel.minor_axis) assert_panel_equal(result, self.panel) data['ItemC'] = self.panel['ItemC'] result = Panel(data) assert_panel_equal(result, self.panel) # corner, blow up data['ItemB'] = data['ItemB'][:-1] pytest.raises(Exception, Panel, data) data['ItemB'] = self.panel['ItemB'].values[:, :-1] pytest.raises(Exception, Panel, data)
Example #4
Source File: test_decimal.py From recruit with Apache License 2.0 | 6 votes |
def assert_frame_equal(self, left, right, *args, **kwargs): # TODO(EA): select_dtypes tm.assert_index_equal( left.columns, right.columns, exact=kwargs.get('check_column_type', 'equiv'), check_names=kwargs.get('check_names', True), check_exact=kwargs.get('check_exact', False), check_categorical=kwargs.get('check_categorical', True), obj='{obj}.columns'.format(obj=kwargs.get('obj', 'DataFrame'))) decimals = (left.dtypes == 'decimal').index for col in decimals: self.assert_series_equal(left[col], right[col], *args, **kwargs) left = left.drop(columns=decimals) right = right.drop(columns=decimals) tm.assert_frame_equal(left, right, *args, **kwargs)
Example #5
Source File: test_window.py From recruit with Apache License 2.0 | 6 votes |
def test_getitem(self): r = self.frame.rolling(window=5) tm.assert_index_equal(r._selected_obj.columns, self.frame.columns) r = self.frame.rolling(window=5)[1] assert r._selected_obj.name == self.frame.columns[1] # technically this is allowed r = self.frame.rolling(window=5)[1, 3] tm.assert_index_equal(r._selected_obj.columns, self.frame.columns[[1, 3]]) r = self.frame.rolling(window=5)[[1, 3]] tm.assert_index_equal(r._selected_obj.columns, self.frame.columns[[1, 3]])
Example #6
Source File: test_window.py From recruit with Apache License 2.0 | 6 votes |
def test_agg_consistency(self): df = DataFrame({'A': range(5), 'B': range(0, 10, 2)}) r = df.rolling(window=3) result = r.agg([np.sum, np.mean]).columns expected = pd.MultiIndex.from_product([list('AB'), ['sum', 'mean']]) tm.assert_index_equal(result, expected) result = r['A'].agg([np.sum, np.mean]).columns expected = Index(['sum', 'mean']) tm.assert_index_equal(result, expected) result = r.agg({'A': [np.sum, np.mean]}).columns expected = pd.MultiIndex.from_tuples([('A', 'sum'), ('A', 'mean')]) tm.assert_index_equal(result, expected)
Example #7
Source File: test_window.py From recruit with Apache License 2.0 | 6 votes |
def test_pairwise_with_self(self, f): # DataFrame with itself, pairwise=True # note that we may construct the 1st level of the MI # in a non-motononic way, so compare accordingly results = [] for i, df in enumerate(self.df1s): result = f(df) tm.assert_index_equal(result.index.levels[0], df.index, check_names=False) tm.assert_numpy_array_equal(safe_sort(result.index.levels[1]), safe_sort(df.columns.unique())) tm.assert_index_equal(result.columns, df.columns) results.append(df) for i, result in enumerate(results): if i > 0: self.compare(result, results[0])
Example #8
Source File: test_panel.py From recruit with Apache License 2.0 | 6 votes |
def test_axis_dummies(self): from pandas.core.reshape.reshape import make_axis_dummies minor_dummies = make_axis_dummies(self.panel, 'minor').astype(np.uint8) assert len(minor_dummies.columns) == len(self.panel.index.levels[1]) major_dummies = make_axis_dummies(self.panel, 'major').astype(np.uint8) assert len(major_dummies.columns) == len(self.panel.index.levels[0]) mapping = {'A': 'one', 'B': 'one', 'C': 'two', 'D': 'two'} transformed = make_axis_dummies(self.panel, 'minor', transform=mapping.get).astype(np.uint8) assert len(transformed.columns) == 2 tm.assert_index_equal(transformed.columns, Index(['one', 'two'])) # TODO: test correctness
Example #9
Source File: test_window.py From recruit with Apache License 2.0 | 6 votes |
def test_no_pairwise_with_other(self, f): # DataFrame with another DataFrame, pairwise=False results = [f(df, self.df2) if df.columns.is_unique else None for df in self.df1s] for (df, result) in zip(self.df1s, results): if result is not None: with catch_warnings(record=True): warnings.simplefilter("ignore", RuntimeWarning) # we can have int and str columns expected_index = df.index.union(self.df2.index) expected_columns = df.columns.union(self.df2.columns) tm.assert_index_equal(result.index, expected_index) tm.assert_index_equal(result.columns, expected_columns) else: with pytest.raises(ValueError, match="'arg1' columns are not unique"): f(df, self.df2) with pytest.raises(ValueError, match="'arg2' columns are not unique"): f(self.df2, df)
Example #10
Source File: test_json.py From recruit with Apache License 2.0 | 6 votes |
def assert_frame_equal(self, left, right, *args, **kwargs): tm.assert_index_equal( left.columns, right.columns, exact=kwargs.get('check_column_type', 'equiv'), check_names=kwargs.get('check_names', True), check_exact=kwargs.get('check_exact', False), check_categorical=kwargs.get('check_categorical', True), obj='{obj}.columns'.format(obj=kwargs.get('obj', 'DataFrame'))) jsons = (left.dtypes == 'json').index for col in jsons: self.assert_series_equal(left[col], right[col], *args, **kwargs) left = left.drop(columns=jsons) right = right.drop(columns=jsons) tm.assert_frame_equal(left, right, *args, **kwargs)
Example #11
Source File: test_series.py From recruit with Apache License 2.0 | 6 votes |
def test_dropna(self): sp = SparseSeries([0, 0, 0, nan, nan, 5, 6], fill_value=0) sp_valid = sp.dropna() expected = sp.to_dense().dropna() expected = expected[expected != 0] exp_arr = pd.SparseArray(expected.values, fill_value=0, kind='block') tm.assert_sp_array_equal(sp_valid.values, exp_arr) tm.assert_index_equal(sp_valid.index, expected.index) assert len(sp_valid.sp_values) == 2 result = self.bseries.dropna() expected = self.bseries.to_dense().dropna() assert not isinstance(result, SparseSeries) tm.assert_series_equal(result, expected)
Example #12
Source File: test_integer.py From recruit with Apache License 2.0 | 6 votes |
def test_astype_index(self, all_data, dropna): # as an int/uint index to Index all_data = all_data[:10] if dropna: other = all_data[~all_data.isna()] else: other = all_data dtype = all_data.dtype idx = pd.Index(np.array(other)) assert isinstance(idx, ABCIndexClass) result = idx.astype(dtype) expected = idx.astype(object).astype(dtype) tm.assert_index_equal(result, expected)
Example #13
Source File: test_datetimelike.py From recruit with Apache License 2.0 | 6 votes |
def test_take(self): data = np.arange(100, dtype='i8') * 24 * 3600 * 10**9 np.random.shuffle(data) idx = self.index_cls._simple_new(data, freq='D') arr = self.array_cls(idx) takers = [1, 4, 94] result = arr.take(takers) expected = idx.take(takers) tm.assert_index_equal(self.index_cls(result), expected) takers = np.array([1, 4, 94]) result = arr.take(takers) expected = idx.take(takers) tm.assert_index_equal(self.index_cls(result), expected)
Example #14
Source File: test_missing.py From recruit with Apache License 2.0 | 6 votes |
def test_nan_handling(self): # Nans are represented as -1 in codes c = Categorical(["a", "b", np.nan, "a"]) tm.assert_index_equal(c.categories, Index(["a", "b"])) tm.assert_numpy_array_equal(c._codes, np.array([0, 1, -1, 0], dtype=np.int8)) c[1] = np.nan tm.assert_index_equal(c.categories, Index(["a", "b"])) tm.assert_numpy_array_equal(c._codes, np.array([0, -1, -1, 0], dtype=np.int8)) # Adding nan to categories should make assigned nan point to the # category! c = Categorical(["a", "b", np.nan, "a"]) tm.assert_index_equal(c.categories, Index(["a", "b"])) tm.assert_numpy_array_equal(c._codes, np.array([0, 1, -1, 0], dtype=np.int8))
Example #15
Source File: test_indexing.py From recruit with Apache License 2.0 | 6 votes |
def test_categories_assigments(self): s = Categorical(["a", "b", "c", "a"]) exp = np.array([1, 2, 3, 1], dtype=np.int64) s.categories = [1, 2, 3] tm.assert_numpy_array_equal(s.__array__(), exp) tm.assert_index_equal(s.categories, Index([1, 2, 3])) # lengthen with pytest.raises(ValueError): s.categories = [1, 2, 3, 4] # shorten with pytest.raises(ValueError): s.categories = [1, 2] # Combinations of sorted/unique:
Example #16
Source File: test_api.py From recruit with Apache License 2.0 | 6 votes |
def test_ordered_api(self): # GH 9347 cat1 = Categorical(list('acb'), ordered=False) tm.assert_index_equal(cat1.categories, Index(['a', 'b', 'c'])) assert not cat1.ordered cat2 = Categorical(list('acb'), categories=list('bca'), ordered=False) tm.assert_index_equal(cat2.categories, Index(['b', 'c', 'a'])) assert not cat2.ordered cat3 = Categorical(list('acb'), ordered=True) tm.assert_index_equal(cat3.categories, Index(['a', 'b', 'c'])) assert cat3.ordered cat4 = Categorical(list('acb'), categories=list('bca'), ordered=True) tm.assert_index_equal(cat4.categories, Index(['b', 'c', 'a'])) assert cat4.ordered
Example #17
Source File: test_analytics.py From recruit with Apache License 2.0 | 6 votes |
def test_unique_index_series(self): c = Categorical([3, 1, 2, 2, 1], categories=[3, 2, 1]) # Categorical.unique sorts categories by appearance order # if ordered=False exp = Categorical([3, 1, 2], categories=[3, 1, 2]) tm.assert_categorical_equal(c.unique(), exp) tm.assert_index_equal(Index(c).unique(), Index(exp)) tm.assert_categorical_equal(Series(c).unique(), exp) c = Categorical([1, 1, 2, 2], categories=[3, 2, 1]) exp = Categorical([1, 2], categories=[1, 2]) tm.assert_categorical_equal(c.unique(), exp) tm.assert_index_equal(Index(c).unique(), Index(exp)) tm.assert_categorical_equal(Series(c).unique(), exp) c = Categorical([3, 1, 2, 2, 1], categories=[3, 2, 1], ordered=True) # Categorical.unique keeps categories order if ordered=True exp = Categorical([3, 1, 2], categories=[3, 2, 1], ordered=True) tm.assert_categorical_equal(c.unique(), exp) tm.assert_index_equal(Index(c).unique(), Index(exp)) tm.assert_categorical_equal(Series(c).unique(), exp)
Example #18
Source File: test_timedelta.py From recruit with Apache License 2.0 | 6 votes |
def test_difference_freq(self, sort): # GH14323: Difference of TimedeltaIndex should not preserve frequency index = timedelta_range("0 days", "5 days", freq="D") other = timedelta_range("1 days", "4 days", freq="D") expected = TimedeltaIndex(["0 days", "5 days"], freq=None) idx_diff = index.difference(other, sort) tm.assert_index_equal(idx_diff, expected) tm.assert_attr_equal('freq', idx_diff, expected) other = timedelta_range("2 days", "5 days", freq="D") idx_diff = index.difference(other, sort) expected = TimedeltaIndex(["0 days", "1 days"], freq=None) tm.assert_index_equal(idx_diff, expected) tm.assert_attr_equal('freq', idx_diff, expected)
Example #19
Source File: test_timedelta.py From recruit with Apache License 2.0 | 6 votes |
def test_difference_sort(self, sort): index = pd.TimedeltaIndex(["5 days", "3 days", "2 days", "4 days", "1 days", "0 days"]) other = timedelta_range("1 days", "4 days", freq="D") idx_diff = index.difference(other, sort) expected = TimedeltaIndex(["5 days", "0 days"], freq=None) if sort is None: expected = expected.sort_values() tm.assert_index_equal(idx_diff, expected) tm.assert_attr_equal('freq', idx_diff, expected) other = timedelta_range("2 days", "5 days", freq="D") idx_diff = index.difference(other, sort) expected = TimedeltaIndex(["1 days", "0 days"], freq=None) if sort is None: expected = expected.sort_values() tm.assert_index_equal(idx_diff, expected) tm.assert_attr_equal('freq', idx_diff, expected)
Example #20
Source File: test_indexing.py From recruit with Apache License 2.0 | 6 votes |
def test_take(self): # GH 10295 idx1 = timedelta_range('1 day', '31 day', freq='D', name='idx') for idx in [idx1]: result = idx.take([0]) assert result == Timedelta('1 day') result = idx.take([-1]) assert result == Timedelta('31 day') result = idx.take([0, 1, 2]) expected = timedelta_range('1 day', '3 day', freq='D', name='idx') tm.assert_index_equal(result, expected) assert result.freq == expected.freq result = idx.take([0, 2, 4]) expected = timedelta_range('1 day', '5 day', freq='2D', name='idx') tm.assert_index_equal(result, expected) assert result.freq == expected.freq result = idx.take([7, 4, 1]) expected = timedelta_range('8 day', '2 day', freq='-3D', name='idx') tm.assert_index_equal(result, expected) assert result.freq == expected.freq result = idx.take([3, 2, 5]) expected = TimedeltaIndex(['4 day', '3 day', '6 day'], name='idx') tm.assert_index_equal(result, expected) assert result.freq is None result = idx.take([-3, 2, 5]) expected = TimedeltaIndex(['29 day', '3 day', '6 day'], name='idx') tm.assert_index_equal(result, expected) assert result.freq is None
Example #21
Source File: test_indexing.py From recruit with Apache License 2.0 | 6 votes |
def test_take2(self): tds = ['1day 02:00:00', '1 day 04:00:00', '1 day 10:00:00'] idx = timedelta_range(start='1d', end='2d', freq='H', name='idx') expected = TimedeltaIndex(tds, freq=None, name='idx') taken1 = idx.take([2, 4, 10]) taken2 = idx[[2, 4, 10]] for taken in [taken1, taken2]: tm.assert_index_equal(taken, expected) assert isinstance(taken, TimedeltaIndex) assert taken.freq is None assert taken.name == expected.name
Example #22
Source File: test_construction.py From recruit with Apache License 2.0 | 6 votes |
def test_float64_ns_rounded(self): # GH#23539 without specifying a unit, floats are regarded as nanos, # and fractional portions are truncated tdi = TimedeltaIndex([2.3, 9.7]) expected = TimedeltaIndex([2, 9]) tm.assert_index_equal(tdi, expected) # integral floats are non-lossy tdi = TimedeltaIndex([2.0, 9.0]) expected = TimedeltaIndex([2, 9]) tm.assert_index_equal(tdi, expected) # NaNs get converted to NaT tdi = TimedeltaIndex([2.0, np.nan]) expected = TimedeltaIndex([pd.Timedelta(nanoseconds=2), pd.NaT]) tm.assert_index_equal(tdi, expected)
Example #23
Source File: test_constructors.py From recruit with Apache License 2.0 | 5 votes |
def test_constructor_from_index_series_timedelta(self): idx = timedelta_range('1 days', freq='D', periods=3) result = Categorical(idx) tm.assert_index_equal(result.categories, idx) result = Categorical(Series(idx)) tm.assert_index_equal(result.categories, idx)
Example #24
Source File: test_window.py From recruit with Apache License 2.0 | 5 votes |
def test_no_flex(self, f): # DataFrame methods (which do not call _flex_binary_moment()) results = [f(df) for df in self.df1s] for (df, result) in zip(self.df1s, results): tm.assert_index_equal(result.index, df.columns) tm.assert_index_equal(result.columns, df.columns) for i, result in enumerate(results): if i > 0: self.compare(result, results[0])
Example #25
Source File: test_timedelta.py From recruit with Apache License 2.0 | 5 votes |
def test_fillna_timedelta(self): # GH 11343 idx = pd.TimedeltaIndex(['1 day', pd.NaT, '3 day']) exp = pd.TimedeltaIndex(['1 day', '2 day', '3 day']) tm.assert_index_equal(idx.fillna(pd.Timedelta('2 day')), exp) exp = pd.TimedeltaIndex(['1 day', '3 hour', '3 day']) idx.fillna(pd.Timedelta('3 hour')) exp = pd.Index( [pd.Timedelta('1 day'), 'x', pd.Timedelta('3 day')], dtype=object) tm.assert_index_equal(idx.fillna('x'), exp)
Example #26
Source File: test_constructors.py From recruit with Apache License 2.0 | 5 votes |
def test_constructor_imaginary(self): values = [1, 2, 3 + 1j] c1 = Categorical(values) tm.assert_index_equal(c1.categories, Index(values)) tm.assert_numpy_array_equal(np.array(c1), np.array(values))
Example #27
Source File: test_timedelta.py From recruit with Apache License 2.0 | 5 votes |
def test_pickle(self): rng = timedelta_range('1 days', periods=10) rng_p = tm.round_trip_pickle(rng) tm.assert_index_equal(rng, rng_p)
Example #28
Source File: test_setops.py From recruit with Apache License 2.0 | 5 votes |
def test_union(self): i1 = timedelta_range('1day', periods=5) i2 = timedelta_range('3day', periods=5) result = i1.union(i2) expected = timedelta_range('1day', periods=7) tm.assert_index_equal(result, expected) i1 = Int64Index(np.arange(0, 20, 2)) i2 = timedelta_range(start='1 day', periods=10, freq='D') i1.union(i2) # Works i2.union(i1) # Fails with "AttributeError: can't set attribute"
Example #29
Source File: test_setops.py From recruit with Apache License 2.0 | 5 votes |
def test_union_coverage(self): idx = TimedeltaIndex(['3d', '1d', '2d']) ordered = TimedeltaIndex(idx.sort_values(), freq='infer') result = ordered.union(idx) tm.assert_index_equal(result, ordered) result = ordered[:0].union(ordered) tm.assert_index_equal(result, ordered) assert result.freq == ordered.freq
Example #30
Source File: test_timedelta.py From recruit with Apache License 2.0 | 5 votes |
def test_does_not_convert_mixed_integer(self): df = tm.makeCustomDataframe(10, 10, data_gen_f=lambda *args, **kwargs: randn(), r_idx_type='i', c_idx_type='td') str(df) cols = df.columns.join(df.index, how='outer') joined = cols.join(df.columns) assert cols.dtype == np.dtype('O') assert cols.dtype == joined.dtype tm.assert_index_equal(cols, joined)