예제 #1
0
파일: internals.py 프로젝트: JWCornV/pandas
    def replace(self, to_replace, value, inplace=False):
        new_values = self.values if inplace else self.values.copy()
        if self._can_hold_element(value):
            value = self._try_cast(value)

        if not isinstance(to_replace, (list, np.ndarray)):
            if self._can_hold_element(to_replace):
                to_replace = self._try_cast(to_replace)
                np.putmask(new_values, com.mask_missing(new_values, to_replace),
                           value)
        else:
            try:
                to_replace = np.array(to_replace, dtype=self.dtype)
                np.putmask(new_values, com.mask_missing(new_values, to_replace),
                           value)
            except:
                to_replace = np.array(to_replace, dtype=object)
                for r in to_replace:
                    if self._can_hold_element(r):
                        r = self._try_cast(r)
                np.putmask(new_values, com.mask_missing(new_values, to_replace),
                           value)

        if inplace:
            return self
        else:
            return make_block(new_values, self.items, self.ref_items)
예제 #2
0
파일: period.py 프로젝트: Arthurkorn/pandas
    def wrapper(self, other):
        if isinstance(other, Period):
            func = getattr(self.values, opname)
            if other.freq != self.freq:
                raise AssertionError("Frequencies must be equal")

            result = func(other.ordinal)
        elif isinstance(other, PeriodIndex):
            if other.freq != self.freq:
                raise AssertionError("Frequencies must be equal")

            result = getattr(self.values, opname)(other.values)

            mask = (com.mask_missing(self.values, tslib.iNaT) |
                    com.mask_missing(other.values, tslib.iNaT))
            if mask.any():
                result[mask] = nat_result

            return result
        else:
            other = Period(other, freq=self.freq)
            func = getattr(self.values, opname)
            result = func(other.ordinal)

        if other.ordinal == tslib.iNaT:
            result.fill(nat_result)
        mask = self.values == tslib.iNaT
        if mask.any():
            result[mask] = nat_result

        return result
예제 #3
0
파일: period.py 프로젝트: DT021/wau
    def wrapper(self, other):
        if isinstance(other, Period):
            func = getattr(self.values, opname)
            if other.freq != self.freq:
                raise AssertionError("Frequencies must be equal")

            result = func(other.ordinal)
        elif isinstance(other, PeriodIndex):
            if other.freq != self.freq:
                raise AssertionError("Frequencies must be equal")

            result = getattr(self.values, opname)(other.values)

            mask = (com.mask_missing(self.values, tslib.iNaT)
                    | com.mask_missing(other.values, tslib.iNaT))
            if mask.any():
                result[mask] = nat_result

            return result
        else:
            other = Period(other, freq=self.freq)
            func = getattr(self.values, opname)
            result = func(other.ordinal)

        if other.ordinal == tslib.iNaT:
            result.fill(nat_result)
        mask = self.values == tslib.iNaT
        if mask.any():
            result[mask] = nat_result

        return result
예제 #4
0
파일: period.py 프로젝트: Allen1203/pandas
    def wrapper(self, other):
        if isinstance(other, Period):
            func = getattr(self.values, opname)
            other_base, _ = _gfc(other.freq)
            if other.freq != self.freq:
                msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
                raise IncompatibleFrequency(msg)

            result = func(other.ordinal)
        elif isinstance(other, PeriodIndex):
            if other.freq != self.freq:
                msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
                raise IncompatibleFrequency(msg)

            result = getattr(self.values, opname)(other.values)

            mask = (com.mask_missing(self.values, tslib.iNaT) |
                    com.mask_missing(other.values, tslib.iNaT))
            if mask.any():
                result[mask] = nat_result

            return result
        else:
            other = Period(other, freq=self.freq)
            func = getattr(self.values, opname)
            result = func(other.ordinal)

        if other.ordinal == tslib.iNaT:
            result.fill(nat_result)
        mask = self.values == tslib.iNaT
        if mask.any():
            result[mask] = nat_result

        return result
예제 #5
0
    def wrapper(self, other):
        if isinstance(other, Period):
            func = getattr(self.values, opname)
            other_base, _ = _gfc(other.freq)
            if other.freq != self.freq:
                msg = _DIFFERENT_FREQ_ERROR.format(self.freqstr, other.freqstr)
                raise ValueError(msg)

            result = func(other.ordinal)
        elif isinstance(other, PeriodIndex):
            if other.freq != self.freq:
                msg = _DIFFERENT_FREQ_ERROR.format(self.freqstr, other.freqstr)
                raise ValueError(msg)

            result = getattr(self.values, opname)(other.values)

            mask = (com.mask_missing(self.values, tslib.iNaT)
                    | com.mask_missing(other.values, tslib.iNaT))
            if mask.any():
                result[mask] = nat_result

            return result
        else:
            other = Period(other, freq=self.freq)
            func = getattr(self.values, opname)
            result = func(other.ordinal)

        if other.ordinal == tslib.iNaT:
            result.fill(nat_result)
        mask = self.values == tslib.iNaT
        if mask.any():
            result[mask] = nat_result

        return result
예제 #6
0
    def replace(self, to_replace, value, inplace=False):
        new_values = self.values if inplace else self.values.copy()
        if self._can_hold_element(value):
            value = self._try_cast(value)

        if not isinstance(to_replace, (list, np.ndarray)):
            if self._can_hold_element(to_replace):
                to_replace = self._try_cast(to_replace)
                msk = com.mask_missing(new_values, to_replace)
                np.putmask(new_values, msk, value)
        else:
            try:
                to_replace = np.array(to_replace, dtype=self.dtype)
                msk = com.mask_missing(new_values, to_replace)
                np.putmask(new_values, msk, value)
            except Exception:
                to_replace = np.array(to_replace, dtype=object)
                for r in to_replace:
                    if self._can_hold_element(r):
                        r = self._try_cast(r)
                msk = com.mask_missing(new_values, to_replace)
                np.putmask(new_values, msk, value)

        if inplace:
            return self
        else:
            return make_block(new_values, self.items, self.ref_items)
예제 #7
0
파일: missing.py 프로젝트: bpw1621/pandas
def interpolate_2d(values, method='pad', axis=0, limit=None, fill_value=None,
                   dtype=None):
    """ perform an actual interpolation of values, values will be make 2-d if
    needed fills inplace, returns the result
    """

    transf = (lambda x: x) if axis == 0 else (lambda x: x.T)

    # reshape a 1 dim if needed
    ndim = values.ndim
    if values.ndim == 1:
        if axis != 0:  # pragma: no cover
            raise AssertionError("cannot interpolate on a ndim == 1 with "
                                 "axis != 0")
        values = values.reshape(tuple((1, ) + values.shape))

    if fill_value is None:
        mask = None
    else:  # todo create faster fill func without masking
        mask = com.mask_missing(transf(values), fill_value)

    method = clean_fill_method(method)
    if method == 'pad':
        values = transf(pad_2d(
            transf(values), limit=limit, mask=mask, dtype=dtype))
    else:
        values = transf(backfill_2d(
            transf(values), limit=limit, mask=mask, dtype=dtype))

    # reshape back
    if ndim == 1:
        values = values[0]

    return values
예제 #8
0
def interpolate_2d(values, method='pad', axis=0, limit=None, fill_value=None):
    """
    Copied from the 0.15.2. This did not exist in 0.12.0.

    Differences:
        - Don't depend on pad_2d and backfill_2d to return values
        - Removed dtype kwarg. 0.12.0 did not have this option.
    """
    transf = (lambda x: x) if axis == 0 else (lambda x: x.T)

    # reshape a 1 dim if needed
    ndim = values.ndim
    if values.ndim == 1:
        if axis != 0:  # pragma: no cover
            raise AssertionError("cannot interpolate on a ndim == 1 with "
                                 "axis != 0")
        values = values.reshape(tuple((1, ) + values.shape))

    if fill_value is None:
        mask = None
    else:  # todo create faster fill func without masking
        mask = mask_missing(transf(values), fill_value)

    # Note: pad_2d and backfill_2d work inplace in 0.12.0 and 0.15.2
    # in 0.15.2 they also return a reference to values
    if method == 'pad':
        pad_2d(transf(values), limit=limit, mask=mask)
    else:
        backfill_2d(transf(values), limit=limit, mask=mask)

    # reshape back
    if ndim == 1:
        values = values[0]

    return values
예제 #9
0
파일: munge.py 프로젝트: 280185386/zipline
def interpolate_2d(values, method='pad', axis=0, limit=None, fill_value=None):
    """
    Copied from the 0.15.2. This did not exist in 0.12.0.

    Differences:
        - Don't depend on pad_2d and backfill_2d to return values
        - Removed dtype kwarg. 0.12.0 did not have this option.
    """
    transf = (lambda x: x) if axis == 0 else (lambda x: x.T)

    # reshape a 1 dim if needed
    ndim = values.ndim
    if values.ndim == 1:
        if axis != 0:  # pragma: no cover
            raise AssertionError("cannot interpolate on a ndim == 1 with "
                                 "axis != 0")
        values = values.reshape(tuple((1,) + values.shape))

    if fill_value is None:
        mask = None
    else:  # todo create faster fill func without masking
        mask = mask_missing(transf(values), fill_value)

    # Note: pad_2d and backfill_2d work inplace in 0.12.0 and 0.15.2
    # in 0.15.2 they also return a reference to values
    if method == 'pad':
        pad_2d(transf(values), limit=limit, mask=mask)
    else:
        backfill_2d(transf(values), limit=limit, mask=mask)

    # reshape back
    if ndim == 1:
        values = values[0]

    return values
예제 #10
0
    def replace(self, to_replace, value, inplace=False, filter=None,
                regex=False):
        """ replace the to_replace value with value, possible to create new
        blocks here this is just a call to putmask. regex is not used here.
        It is used in ObjectBlocks.  It is here for API
        compatibility."""
        mask = com.mask_missing(self.values, to_replace)
        if filter is not None:
            filtered_out = ~self.mgr_locs.isin(filter)
            mask[filtered_out.nonzero()[0]] = False

        if not mask.any():
            if inplace:
                return [self]
            return [self.copy()]
        return self.putmask(mask, value, inplace=inplace)
예제 #11
0
def _single_replace(self, to_replace, method, inplace, limit):
    orig_dtype = self.dtype
    result = self if inplace else self.copy()
    fill_f = com._get_fill_func(method)

    mask = com.mask_missing(result.values, to_replace)
    values = fill_f(result.values, limit=limit, mask=mask)

    if values.dtype == orig_dtype and inplace:
        return

    result = pd.Series(values, index=self.index, name=self.name,
                       dtype=self.dtype)

    if inplace:
        self._data = result._data
        return

    return result
예제 #12
0
def interpolate_2d(values,
                   method='pad',
                   axis=0,
                   limit=None,
                   fill_value=None,
                   dtype=None):
    """ perform an actual interpolation of values, values will be make 2-d if
    needed fills inplace, returns the result
    """

    transf = (lambda x: x) if axis == 0 else (lambda x: x.T)

    # reshape a 1 dim if needed
    ndim = values.ndim
    if values.ndim == 1:
        if axis != 0:  # pragma: no cover
            raise AssertionError("cannot interpolate on a ndim == 1 with "
                                 "axis != 0")
        values = values.reshape(tuple((1, ) + values.shape))

    if fill_value is None:
        mask = None
    else:  # todo create faster fill func without masking
        mask = com.mask_missing(transf(values), fill_value)

    method = _clean_fill_method(method)
    if method == 'pad':
        values = transf(
            pad_2d(transf(values), limit=limit, mask=mask, dtype=dtype))
    else:
        values = transf(
            backfill_2d(transf(values), limit=limit, mask=mask, dtype=dtype))

    # reshape back
    if ndim == 1:
        values = values[0]

    return values