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])
def test_ew_empty_series(method): vals = pd.Series([], dtype=np.float64) ewm = vals.ewm(3) result = getattr(ewm, method)() tm.assert_almost_equal(result, vals)
def test_get_indexer_three_or_more_levels(self): # https://github.com/pandas-dev/pandas/issues/29896 # tests get_indexer() on MultiIndexes with 3+ levels # visually, these are # mult_idx_1: # 0: 1 2 5 # 1: 7 # 2: 4 5 # 3: 7 # 4: 6 5 # 5: 7 # 6: 3 2 5 # 7: 7 # 8: 4 5 # 9: 7 # 10: 6 5 # 11: 7 # # mult_idx_2: # 0: 1 1 8 # 1: 1 5 9 # 2: 1 6 7 # 3: 2 1 6 # 4: 2 7 6 # 5: 2 7 8 # 6: 3 6 8 mult_idx_1 = pd.MultiIndex.from_product([[1, 3], [2, 4, 6], [5, 7]]) mult_idx_2 = pd.MultiIndex.from_tuples( [ (1, 1, 8), (1, 5, 9), (1, 6, 7), (2, 1, 6), (2, 7, 7), (2, 7, 8), (3, 6, 8), ] ) # sanity check assert mult_idx_1.is_monotonic assert mult_idx_1.is_unique assert mult_idx_2.is_monotonic assert mult_idx_2.is_unique # show the relationships between the two assert mult_idx_2[0] < mult_idx_1[0] assert mult_idx_1[3] < mult_idx_2[1] < mult_idx_1[4] assert mult_idx_1[5] == mult_idx_2[2] assert mult_idx_1[5] < mult_idx_2[3] < mult_idx_1[6] assert mult_idx_1[5] < mult_idx_2[4] < mult_idx_1[6] assert mult_idx_1[5] < mult_idx_2[5] < mult_idx_1[6] assert mult_idx_1[-1] < mult_idx_2[6] indexer_no_fill = mult_idx_1.get_indexer(mult_idx_2) expected = np.array([-1, -1, 5, -1, -1, -1, -1], dtype=indexer_no_fill.dtype) tm.assert_almost_equal(expected, indexer_no_fill) # test with backfilling indexer_backfilled = mult_idx_1.get_indexer(mult_idx_2, method="backfill") expected = np.array([0, 4, 5, 6, 6, 6, -1], dtype=indexer_backfilled.dtype) tm.assert_almost_equal(expected, indexer_backfilled) # now, the same thing, but forward-filled (aka "padded") indexer_padded = mult_idx_1.get_indexer(mult_idx_2, method="pad") expected = np.array([-1, 3, 5, 5, 5, 5, 11], dtype=indexer_padded.dtype) tm.assert_almost_equal(expected, indexer_padded) # now, do the indexing in the other direction assert mult_idx_2[0] < mult_idx_1[0] < mult_idx_2[1] assert mult_idx_2[0] < mult_idx_1[1] < mult_idx_2[1] assert mult_idx_2[0] < mult_idx_1[2] < mult_idx_2[1] assert mult_idx_2[0] < mult_idx_1[3] < mult_idx_2[1] assert mult_idx_2[1] < mult_idx_1[4] < mult_idx_2[2] assert mult_idx_2[2] == mult_idx_1[5] assert mult_idx_2[5] < mult_idx_1[6] < mult_idx_2[6] assert mult_idx_2[5] < mult_idx_1[7] < mult_idx_2[6] assert mult_idx_2[5] < mult_idx_1[8] < mult_idx_2[6] assert mult_idx_2[5] < mult_idx_1[9] < mult_idx_2[6] assert mult_idx_2[5] < mult_idx_1[10] < mult_idx_2[6] assert mult_idx_2[5] < mult_idx_1[11] < mult_idx_2[6] indexer = mult_idx_2.get_indexer(mult_idx_1) expected = np.array( [-1, -1, -1, -1, -1, 2, -1, -1, -1, -1, -1, -1], dtype=indexer.dtype ) tm.assert_almost_equal(expected, indexer) backfill_indexer = mult_idx_2.get_indexer(mult_idx_1, method="bfill") expected = np.array( [1, 1, 1, 1, 2, 2, 6, 6, 6, 6, 6, 6], dtype=backfill_indexer.dtype ) tm.assert_almost_equal(expected, backfill_indexer) pad_indexer = mult_idx_2.get_indexer(mult_idx_1, method="pad") expected = np.array( [0, 0, 0, 0, 1, 2, 5, 5, 5, 5, 5, 5], dtype=pad_indexer.dtype ) tm.assert_almost_equal(expected, pad_indexer)
def test_get_indexer(self): major_axis = Index(np.arange(4)) minor_axis = Index(np.arange(2)) major_codes = np.array([0, 0, 1, 2, 2, 3, 3], dtype=np.intp) minor_codes = np.array([0, 1, 0, 0, 1, 0, 1], dtype=np.intp) index = MultiIndex( levels=[major_axis, minor_axis], codes=[major_codes, minor_codes] ) idx1 = index[:5] idx2 = index[[1, 3, 5]] r1 = idx1.get_indexer(idx2) tm.assert_almost_equal(r1, np.array([1, 3, -1], dtype=np.intp)) r1 = idx2.get_indexer(idx1, method="pad") e1 = np.array([-1, 0, 0, 1, 1], dtype=np.intp) tm.assert_almost_equal(r1, e1) r2 = idx2.get_indexer(idx1[::-1], method="pad") tm.assert_almost_equal(r2, e1[::-1]) rffill1 = idx2.get_indexer(idx1, method="ffill") tm.assert_almost_equal(r1, rffill1) r1 = idx2.get_indexer(idx1, method="backfill") e1 = np.array([0, 0, 1, 1, 2], dtype=np.intp) tm.assert_almost_equal(r1, e1) r2 = idx2.get_indexer(idx1[::-1], method="backfill") tm.assert_almost_equal(r2, e1[::-1]) rbfill1 = idx2.get_indexer(idx1, method="bfill") tm.assert_almost_equal(r1, rbfill1) # pass non-MultiIndex r1 = idx1.get_indexer(idx2.values) rexp1 = idx1.get_indexer(idx2) tm.assert_almost_equal(r1, rexp1) r1 = idx1.get_indexer([1, 2, 3]) assert (r1 == [-1, -1, -1]).all() # create index with duplicates idx1 = Index(list(range(10)) + list(range(10))) idx2 = Index(list(range(20))) msg = "Reindexing only valid with uniquely valued Index objects" with pytest.raises(InvalidIndexError, match=msg): idx1.get_indexer(idx2)
def test_series(raw, series): result = series.rolling(50).apply(f, raw=raw) assert isinstance(result, Series) tm.assert_almost_equal(result.iloc[-1], np.mean(series[-50:]))
def test_decode_floating_point(self, sign, float_number): float_number *= sign tm.assert_almost_equal( float_number, ujson.loads(str(float_number)), check_less_precise=15 )
def test_setitem_list_not_dataframe(self, float_frame): data = np.random.randn(len(float_frame), 2) float_frame[["A", "B"]] = data tm.assert_almost_equal(float_frame[["A", "B"]].values, data)