# Buggy code
def replace(
    self,
    to_replace,
    value,
    inplace=False,
    filter=None,
    regex=False,
    convert=True,
    mgr=None,
):
    """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."""

    original_to_replace = to_replace

    # try to replace, if we raise an error, convert to ObjectBlock and retry
    try:
        values, _, to_replace, _ = self._try_coerce_args(self.values, to_replace)
        mask = com.mask_missing(values, to_replace)
        if filter is not None:
            filtered_out = ~self.mgr_locs.isin(filter)
            mask[filtered_out.nonzero()[0]] = False

        blocks = self.putmask(mask, value, inplace=inplace)
        if convert:
            blocks = [
                b.convert(by_item=True, numeric=False, copy=not inplace) for b in blocks
            ]
        return blocks
    except (TypeError, ValueError):
        # we can't process the value, but nothing to do
        if not mask.any():
            return self if inplace else self.copy()

        return self.to_object_block(mgr=mgr).replace(
            to_replace=original_to_replace,
            value=value,
            inplace=inplace,
            filter=filter,
            regex=regex,
            convert=convert,
        )


# Traceback (UnboundLocalError)
pandas.DataFrame([('-', pandas.to_datetime('20150101')), ('a', pandas.to_datetime('20150102')), ('b', pandas.to_datetime('20150103'))], columns=['a', 'b']).replace('-', numpy.nan)
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-11-bce7dc78fc44> in <module>()
----> 1 pandas.DataFrame([('-', pandas.to_datetime('20150101')), ('a', pandas.to_datetime('20150102')), ('b', pandas.to_datetime('20150103'))], columns=['a', 'b']).replace('-', numpy.nan)

/.../pandas/core/generic.pyc in replace(self, to_replace, value, inplace, limit, regex, method, axis)
3108                 elif not com.is_list_like(value):  # NA -> 0
3109                     new_data = self._data.replace(to_replace=to_replace, value=value,
-> 3110                                                   inplace=inplace, regex=regex)
3111                 else:
3112                     msg = ('Invalid "to_replace" type: '

/.../pandas/core/internals.pyc in replace(self, **kwargs)
2868
2869     def replace(self, **kwargs):
-> 2870         return self.apply('replace', **kwargs)
2871
2872     def replace_list(self, src_list, dest_list, inplace=False, regex=False, mgr=None):

/.../pandas/core/internals.pyc in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
2821
2822             kwargs['mgr'] = self
-> 2823             applied = getattr(b, f)(**kwargs)
2824             result_blocks = _extend_blocks(applied, result_blocks)
2825

/.../pandas/core/internals.pyc in replace(self, to_replace, value, inplace, filter, regex, convert, mgr)
605
606             # we can't process the value, but nothing to do
--> 607             if not mask.any():
608                 return self if inplace else self.copy()
609

UnboundLocalError: local variable 'mask' referenced before assignment

# Fixed code
def replace(
    self,
    to_replace,
    value,
    inplace=False,
    filter=None,
    regex=False,
    convert=True,
    mgr=None,
):
    """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."""

    original_to_replace = to_replace
    mask = isnull(self.values)

    # try to replace, if we raise an error, convert to ObjectBlock and retry
    try:
        values, _, to_replace, _ = self._try_coerce_args(self.values, to_replace)
        mask = com.mask_missing(values, to_replace)
        if filter is not None:
            filtered_out = ~self.mgr_locs.isin(filter)
            mask[filtered_out.nonzero()[0]] = False

        blocks = self.putmask(mask, value, inplace=inplace)
        if convert:
            blocks = [
                b.convert(by_item=True, numeric=False, copy=not inplace) for b in blocks
            ]
        return blocks
    except (TypeError, ValueError):
        # we can't process the value, but nothing to do
        if not mask.any():
            return self if inplace else self.copy()

        return self.to_object_block(mgr=mgr).replace(
            to_replace=original_to_replace,
            value=value,
            inplace=inplace,
            filter=filter,
            regex=regex,
            convert=convert,
        )