def test_map_with_categorical_series(self): # GH 12756 a = Index([1, 2, 3, 4]) b = Series(["even", "odd", "even", "odd"], dtype="category") c = Series(["even", "odd", "even", "odd"]) exp = CategoricalIndex(["odd", "even", "odd", np.nan]) tm.assert_index_equal(a.map(b), exp) exp = Index(["odd", "even", "odd", np.nan]) tm.assert_index_equal(a.map(c), exp)
def _get_skip_mask(cls, rows_index: pandas.Index, skiprows: Callable): """ Get mask of skipped by callable `skiprows` rows. Parameters ---------- rows_index : pandas.Index Rows index to get mask for. skiprows : Callable Callable to check whether row index should be skipped. Returns ------- pandas.Index """ try: # direct `skiprows` call is more efficient than using of # map method, but in some cases it can work incorrectly, e.g. # when `skiprows` contains `in` operator mask = skiprows(rows_index) assert is_list_like(mask) except (ValueError, TypeError, AssertionError): # ValueError can be raised if `skiprows` callable contains membership operator # TypeError is raised if `skiprows` callable contains bitwise operator # AssertionError is raised if unexpected behavior was detected mask = rows_index.map(skiprows) return mask
def test_equals2(self): # GH#13107 idx = TimedeltaIndex(["1 days", "2 days", "NaT"]) assert idx.equals(idx) assert idx.equals(idx.copy()) assert idx.equals(idx.astype(object)) assert idx.astype(object).equals(idx) assert idx.astype(object).equals(idx.astype(object)) assert not idx.equals(list(idx)) assert not idx.equals(pd.Series(idx)) idx2 = TimedeltaIndex(["2 days", "1 days", "NaT"]) assert not idx.equals(idx2) assert not idx.equals(idx2.copy()) assert not idx.equals(idx2.astype(object)) assert not idx.astype(object).equals(idx2) assert not idx.astype(object).equals(idx2.astype(object)) assert not idx.equals(list(idx2)) assert not idx.equals(pd.Series(idx2)) # Check that we dont raise OverflowError on comparisons outside the # implementation range GH#28532 oob = Index([timedelta(days=10**6)] * 3, dtype=object) assert not idx.equals(oob) assert not idx2.equals(oob) oob2 = Index([np.timedelta64(x) for x in oob], dtype=object) assert (oob == oob2).all() assert not idx.equals(oob2) assert not idx2.equals(oob2) oob3 = oob.map(np.timedelta64) assert (oob3 == oob).all() assert not idx.equals(oob3) assert not idx2.equals(oob3)
def nanmap(columns: pd.Index, dict_map: dict) -> np.ndarray: """ columns.map(dict) では dict に定義されていない変数は nan になる. 定義されてないcolumnはそのまま残すようにする """ columns = columns.copy() columnswk = columns.map(dict_map).fillna("__work").values columns = columns.values columns[(columnswk != "__work")] = columnswk[(columnswk != "__work")] return columns
def test_equals(self): # GH 13107 idx = DatetimeIndex(["2011-01-01", "2011-01-02", "NaT"]) assert idx.equals(idx) assert idx.equals(idx.copy()) assert idx.equals(idx.astype(object)) assert idx.astype(object).equals(idx) assert idx.astype(object).equals(idx.astype(object)) assert not idx.equals(list(idx)) assert not idx.equals(Series(idx)) idx2 = DatetimeIndex(["2011-01-01", "2011-01-02", "NaT"], tz="US/Pacific") assert not idx.equals(idx2) assert not idx.equals(idx2.copy()) assert not idx.equals(idx2.astype(object)) assert not idx.astype(object).equals(idx2) assert not idx.equals(list(idx2)) assert not idx.equals(Series(idx2)) # same internal, different tz idx3 = DatetimeIndex(idx.asi8, tz="US/Pacific") tm.assert_numpy_array_equal(idx.asi8, idx3.asi8) assert not idx.equals(idx3) assert not idx.equals(idx3.copy()) assert not idx.equals(idx3.astype(object)) assert not idx.astype(object).equals(idx3) assert not idx.equals(list(idx3)) assert not idx.equals(Series(idx3)) # check that we do not raise when comparing with OutOfBounds objects oob = Index([datetime(2500, 1, 1)] * 3, dtype=object) assert not idx.equals(oob) assert not idx2.equals(oob) assert not idx3.equals(oob) # check that we do not raise when comparing with OutOfBounds dt64 oob2 = oob.map(np.datetime64) assert not idx.equals(oob2) assert not idx2.equals(oob2) assert not idx3.equals(oob2)