gurudesh/pytracebugs huggingface dataset:
# Buggy code
def f(arg, window, min_periods=None, freq=None, center=False, **kwargs):
    def call_cython(arg, window, minp, args=(), kwargs={}, **kwds):
        minp = check_minp(minp, window)
        return func(arg, window, minp, **kwds)

    return _rolling_moment(
        arg, window, call_cython, min_periods, freq=freq, center=center, **kwargs
    )


# Traceback (AssertionError)
In [118]: import pandas

In [119]: indices = [datetime.datetime(1975, 1, i, 12, 0) for i in range(1, 6)]

In [120]: indices.append(datetime.datetime(1975, 1, 3, 6, 0))  # So that we can have 2 datapoints on one of the days

In [121]: series = pandas.Series(range(1, 7), index=indices)

In [122]: series = series.map(lambda x: float(x))  # Use floats instead of ints as values

In [123]: series = series.sort_index()  # Sort chronologically

In [124]: expected_result = pandas.Series([1.0, 2.0, 6.0, 4.0, 5.0], index=[datetime.datetime(1975, 1, i, 12, 0) for i in range(1, 6)])

In [125]: actual_result = pandas.rolling_max(series, window=1, freq='D')

In [126]: assert((actual_result==expected_result).all())
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-126-cc436c4798a7> in <module>()
----> 1 assert((actual_result==expected_result).all())

AssertionError:

In [127]: expected_result
Out[127]:
1975-01-01 12:00:00    1
1975-01-02 12:00:00    2
1975-01-03 12:00:00    6
1975-01-04 12:00:00    4
1975-01-05 12:00:00    5
dtype: float64

In [128]: actual_result
Out[128]:
1975-01-01    1.0
1975-01-02    2.0
1975-01-03    4.5
1975-01-04    4.0
1975-01-05    5.0
Freq: D, dtype: float64

# Fixed code
def f(arg, window, min_periods=None, freq=None, center=False, how=how, **kwargs):
    def call_cython(arg, window, minp, args=(), kwargs={}, **kwds):
        minp = check_minp(minp, window)
        return func(arg, window, minp, **kwds)

    return _rolling_moment(
        arg,
        window,
        call_cython,
        min_periods,
        freq=freq,
        center=center,
        how=how,
        **kwargs,
    )
# Buggy code
def reshape(self, newshape, order="C"):
    """
    See numpy.ndarray.reshape
    """
    if order not in ["C", "F"]:
        raise TypeError("must specify a tuple / singular length to reshape")

    if isinstance(newshape, tuple) and len(newshape) > 1:
        return self.values.reshape(newshape, order=order)
    else:
        return ndarray.reshape(self, newshape, order)


# Traceback (TypeError)
import pandas as pd
x = pd.Series(range(5))
x.reshape(x.shape)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/pymodules/python2.7/pandas/core/series.py", line 981, in reshape
return ndarray.reshape(self, newshape, order)
TypeError: an integer is required

# Fixed code
def reshape(self, *args, **kwargs):
    """
    See numpy.ndarray.reshape
    """
    if len(args) == 1 and hasattr(args[0], "__iter__"):
        shape = args[0]
    else:
        shape = args

    if tuple(shape) == self.shape:
        # XXX ignoring the "order" keyword.
        return self

    return self.values.reshape(shape, **kwargs)
# Buggy code
def __getitem__(self, key):
    try:
        return self.index.get_value(self, key)
    except InvalidIndexError:
        pass
    except KeyError:
        if isinstance(key, tuple) and isinstance(self.index, MultiIndex):
            # kludge
            pass
        else:
            raise
    except Exception:
        raise

    if com.is_iterator(key):
        key = list(key)

    # boolean
    # special handling of boolean data with NAs stored in object
    # arrays. Since we can't represent NA with dtype=bool
    if _is_bool_indexer(key):
        key = self._check_bool_indexer(key)
        key = np.asarray(key, dtype=bool)

    return self._get_with(key)


# Traceback (ValueError)
In [13]: s = Series(np.arange(10))

In [14]: np.diff(s)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/wesm/code/pandas/<ipython-input-14-b5f9fe77ab7c> in <module>()
----> 1 np.diff(s)

/usr/lib/epd-7.1/lib/python2.7/site-packages/numpy/lib/function_base.pyc in diff(a, n, axis)
975         return diff(a[slice1]-a[slice2], n-1, axis=axis)
976     else:
--> 977         return a[slice1]-a[slice2]
978
979 def interp(x, xp, fp, left=None, right=None):

/home/wesm/code/pandas/pandas/core/series.pyc in __getitem__(self, key)
392             key = np.asarray(key, dtype=bool)
393
--> 394         return self._get_with(key)
395
396     def _get_with(self, key):

/home/wesm/code/pandas/pandas/core/series.pyc in _get_with(self, key)
406         else:
407             if isinstance(key, tuple):
--> 408                 return self._get_values_tuple(key)
409
410             if not isinstance(key, (list, np.ndarray)):  # pragma: no cover

/home/wesm/code/pandas/pandas/core/series.pyc in _get_values_tuple(self, key)
437
438         if not isinstance(self.index, MultiIndex):
--> 439             raise ValueError('Can only tuple-index with a MultiIndex')
440
441         # If key is contained, would have returned by now

ValueError: Can only tuple-index with a MultiIndex

# Fixed code
def __getitem__(self, key):
    try:
        if type(key) == tuple and len(key) == 1:
            key = key[0]
        return self.index.get_value(self, key)
    except InvalidIndexError:
        pass
    except KeyError:
        if isinstance(key, tuple) and isinstance(self.index, MultiIndex):
            # kludge
            pass
        else:
            raise
    except Exception:
        raise

    if com.is_iterator(key):
        key = list(key)

    # boolean
    # special handling of boolean data with NAs stored in object
    # arrays. Since we can't represent NA with dtype=bool
    if _is_bool_indexer(key):
        key = self._check_bool_indexer(key)
        key = np.asarray(key, dtype=bool)

    return self._get_with(key)
# Buggy code
def __getitem__(self, key):
    try:
        if type(key) == tuple and len(key) == 1:
            key = key[0]
        return self.index.get_value(self, key)
    except InvalidIndexError:
        pass
    except KeyError:
        if isinstance(key, tuple) and isinstance(self.index, MultiIndex):
            # kludge
            pass
        else:
            raise
    except Exception:
        raise

    if com.is_iterator(key):
        key = list(key)

    # boolean
    # special handling of boolean data with NAs stored in object
    # arrays. Since we can't represent NA with dtype=bool
    if _is_bool_indexer(key):
        key = self._check_bool_indexer(key)
        key = np.asarray(key, dtype=bool)

    return self._get_with(key)


# Traceback (ValueError)
In [13]: s = Series(np.arange(10))

In [14]: np.diff(s)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/wesm/code/pandas/<ipython-input-14-b5f9fe77ab7c> in <module>()
----> 1 np.diff(s)

/usr/lib/epd-7.1/lib/python2.7/site-packages/numpy/lib/function_base.pyc in diff(a, n, axis)
975         return diff(a[slice1]-a[slice2], n-1, axis=axis)
976     else:
--> 977         return a[slice1]-a[slice2]
978
979 def interp(x, xp, fp, left=None, right=None):

/home/wesm/code/pandas/pandas/core/series.pyc in __getitem__(self, key)
392             key = np.asarray(key, dtype=bool)
393
--> 394         return self._get_with(key)
395
396     def _get_with(self, key):

/home/wesm/code/pandas/pandas/core/series.pyc in _get_with(self, key)
406         else:
407             if isinstance(key, tuple):
--> 408                 return self._get_values_tuple(key)
409
410             if not isinstance(key, (list, np.ndarray)):  # pragma: no cover

/home/wesm/code/pandas/pandas/core/series.pyc in _get_values_tuple(self, key)
437
438         if not isinstance(self.index, MultiIndex):
--> 439             raise ValueError('Can only tuple-index with a MultiIndex')
440
441         # If key is contained, would have returned by now

ValueError: Can only tuple-index with a MultiIndex

# Fixed code
def __getitem__(self, key):
    try:
        return self.index.get_value(self, key)
    except InvalidIndexError:
        pass
    except KeyError:
        if isinstance(key, tuple) and isinstance(self.index, MultiIndex):
            # kludge
            pass
        else:
            raise
    except Exception:
        raise

    if com.is_iterator(key):
        key = list(key)

    # boolean
    # special handling of boolean data with NAs stored in object
    # arrays. Since we can't represent NA with dtype=bool
    if _is_bool_indexer(key):
        key = self._check_bool_indexer(key)
        key = np.asarray(key, dtype=bool)

    return self._get_with(key)
# Buggy code
def match(to_match, values, na_sentinel=-1):
    """
    Compute locations of to_match into values

    Parameters
    ----------
    to_match : array-like
        values to find positions of
    values : array-like
        Unique set of values
    na_sentinel : int, default -1
        Value to mark "not found"

    Examples
    --------

    Returns
    -------
    match : ndarray of integers
    """
    values = np.asarray(values)
    if issubclass(values.dtype.type, basestring):
        values = np.array(values, dtype="O")

    f = lambda htype, caster: _match_generic(to_match, values, htype, caster)
    return _hashtable_algo(f, values.dtype)


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def match(to_match, values, na_sentinel=-1):
    """
    Compute locations of to_match into values

    Parameters
    ----------
    to_match : array-like
        values to find positions of
    values : array-like
        Unique set of values
    na_sentinel : int, default -1
        Value to mark "not found"

    Examples
    --------

    Returns
    -------
    match : ndarray of integers
    """
    values = com._asarray_tuplesafe(values)
    if issubclass(values.dtype.type, basestring):
        values = np.array(values, dtype="O")

    f = lambda htype, caster: _match_generic(to_match, values, htype, caster)
    return _hashtable_algo(f, values.dtype)
# Buggy code
def _isnull_ndarraylike(obj):
    from pandas import Series

    values = np.asarray(obj)

    if values.dtype.kind in ("O", "S"):
        # Working around NumPy ticket 1542
        shape = values.shape
        result = np.empty(shape, dtype=bool)
        vec = lib.isnullobj(values.ravel())
        result[:] = vec.reshape(shape)

        if isinstance(obj, Series):
            result = Series(result, index=obj.index, copy=False)
    elif values.dtype == np.dtype("M8[ns]"):
        # this is the NaT pattern
        result = values.view("i8") == lib.iNaT
    else:
        result = -np.isfinite(obj)
    return result


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def _isnull_ndarraylike(obj):
    from pandas import Series

    values = np.asarray(obj)

    if values.dtype.kind in ("O", "S", "U"):
        # Working around NumPy ticket 1542
        shape = values.shape

        if values.dtype.kind in ("S", "U"):
            result = np.zeros(values.shape, dtype=bool)
        else:
            result = np.empty(shape, dtype=bool)
            vec = lib.isnullobj(values.ravel())
            result[:] = vec.reshape(shape)

        if isinstance(obj, Series):
            result = Series(result, index=obj.index, copy=False)
    elif values.dtype == np.dtype("M8[ns]"):
        # this is the NaT pattern
        result = values.view("i8") == lib.iNaT
    else:
        result = -np.isfinite(obj)
    return result
# Buggy code
def pad_2d(values, limit=None, mask=None):
    if is_float_dtype(values):
        _method = _algos.pad_2d_inplace_float64
    elif is_datetime64_dtype(values):
        _method = _pad_2d_datetime
    elif values.dtype == np.object_:
        _method = _algos.pad_2d_inplace_object
    else:  # pragma: no cover
        raise ValueError("Invalid dtype for padding")

    if mask is None:
        mask = isnull(values)
    mask = mask.view(np.uint8)

    _method(values, mask, limit=limit)


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def pad_2d(values, limit=None, mask=None):
    if is_float_dtype(values):
        _method = _algos.pad_2d_inplace_float64
    elif is_datetime64_dtype(values):
        _method = _pad_2d_datetime
    elif values.dtype == np.object_:
        _method = _algos.pad_2d_inplace_object
    else:  # pragma: no cover
        raise ValueError("Invalid dtype for padding")

    if mask is None:
        mask = isnull(values)
    mask = mask.view(np.uint8)

    if np.all(values.shape):
        _method(values, mask, limit=limit)
    else:
        # for test coverage
        pass
# Buggy code
def backfill_2d(values, limit=None, mask=None):
    if is_float_dtype(values):
        _method = _algos.backfill_2d_inplace_float64
    elif is_datetime64_dtype(values):
        _method = _backfill_2d_datetime
    elif values.dtype == np.object_:
        _method = _algos.backfill_2d_inplace_object
    else:  # pragma: no cover
        raise ValueError("Invalid dtype for padding")

    if mask is None:
        mask = isnull(values)
    mask = mask.view(np.uint8)

    _method(values, mask, limit=limit)


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def backfill_2d(values, limit=None, mask=None):
    if is_float_dtype(values):
        _method = _algos.backfill_2d_inplace_float64
    elif is_datetime64_dtype(values):
        _method = _backfill_2d_datetime
    elif values.dtype == np.object_:
        _method = _algos.backfill_2d_inplace_object
    else:  # pragma: no cover
        raise ValueError("Invalid dtype for padding")

    if mask is None:
        mask = isnull(values)
    mask = mask.view(np.uint8)

    if np.all(values.shape):
        _method(values, mask, limit=limit)
    else:
        # for test coverage
        pass
# Buggy code
def _asarray_tuplesafe(values, dtype=None):
    if not isinstance(values, (list, tuple, np.ndarray)):
        values = list(values)

    if isinstance(values, list) and dtype in [np.object_, object]:
        return lib.list_to_object_array(values)

    result = np.asarray(values, dtype=dtype)

    if issubclass(result.dtype.type, basestring):
        result = np.asarray(values, dtype=object)

    if result.ndim == 2:
        if isinstance(values, list):
            return lib.list_to_object_array(values)
        else:
            # Making a 1D array that safely contains tuples is a bit tricky
            # in numpy, leading to the following
            result = np.empty(len(values), dtype=object)
            result[:] = values

    return result


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def _asarray_tuplesafe(values, dtype=None):
    from pandas.core.index import Index

    if not isinstance(values, (list, tuple, np.ndarray)):
        values = list(values)
    elif isinstance(values, Index):
        return values.values

    if isinstance(values, list) and dtype in [np.object_, object]:
        return lib.list_to_object_array(values)

    result = np.asarray(values, dtype=dtype)

    if issubclass(result.dtype.type, basestring):
        result = np.asarray(values, dtype=object)

    if result.ndim == 2:
        if isinstance(values, list):
            return lib.list_to_object_array(values)
        else:
            # Making a 1D array that safely contains tuples is a bit tricky
            # in numpy, leading to the following
            result = np.empty(len(values), dtype=object)
            result[:] = values

    return result
# Buggy code
def console_encode(value):
    if py3compat.PY3 or not isinstance(value, unicode):
        return value

    try:
        import sys

        return value.encode(sys.stdin.encoding, "replace")
    except (AttributeError, TypeError):
        return value.encode("ascii", "replace")


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def console_encode(value):
    if py3compat.PY3 or not isinstance(value, unicode):
        return value

    try:
        import sys

        return value.encode(sys.stdin.encoding or "utf-8", "replace")
    except (AttributeError, TypeError):
        return value.encode("ascii", "replace")
# Buggy code
def reindex_axis(self, new_axis, method=None, axis=0, copy=True):
    new_axis = _ensure_index(new_axis)
    cur_axis = self.axes[axis]

    if new_axis.equals(cur_axis):
        if copy:
            result = self.copy(deep=True)
            result.axes[axis] = new_axis
            return result
        else:
            return self

    if axis == 0:
        assert method is None
        return self.reindex_items(new_axis)

    new_axis, indexer = cur_axis.reindex(new_axis, method)
    return self.reindex_indexer(new_axis, indexer, axis=axis)


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def reindex_axis(self, new_axis, method=None, axis=0, copy=True):
    new_axis = _ensure_index(new_axis)
    cur_axis = self.axes[axis]

    if new_axis.equals(cur_axis):
        if copy:
            result = self.copy(deep=True)
            result.axes[axis] = new_axis

            if axis == 0:
                # patch ref_items, #1823
                for blk in result.blocks:
                    blk.ref_items = new_axis

            return result
        else:
            return self

    if axis == 0:
        assert method is None
        return self.reindex_items(new_axis)

    new_axis, indexer = cur_axis.reindex(new_axis, method)
    return self.reindex_indexer(new_axis, indexer, axis=axis)
# Buggy code
def __setitem__(self, key, value):
    _, N, K = self.shape
    if isinstance(value, DataFrame):
        value = value.reindex(index=self.major_axis, columns=self.minor_axis)
        mat = value.values
    elif isinstance(value, np.ndarray):
        assert value.shape == (N, K)
        mat = np.asarray(value)
    elif np.isscalar(value):
        dtype = _infer_dtype(value)
        mat = np.empty((N, K), dtype=dtype)
        mat.fill(value)

    mat = mat.reshape((1, N, K))
    NDFrame._set_item(self, key, mat)


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def __setitem__(self, key, value):
    _, N, K = self.shape
    if isinstance(value, DataFrame):
        value = value.reindex(index=self.major_axis, columns=self.minor_axis)
        mat = value.values
    elif isinstance(value, np.ndarray):
        assert value.shape == (N, K)
        mat = np.asarray(value)
    elif np.isscalar(value):
        dtype = _infer_dtype(value)
        mat = np.empty((N, K), dtype=dtype)
        mat.fill(value)
    else:
        raise TypeError("Cannot set item of type: %s" % str(type(value)))

    mat = mat.reshape((1, N, K))
    NDFrame._set_item(self, key, mat)
# Buggy code
def get_new_values(self):
    values = self.values
    # place the values
    length, width = self.full_shape
    stride = values.shape[1]
    result_width = width * stride

    new_values = np.empty((length, result_width), dtype=values.dtype)
    new_mask = np.zeros((length, result_width), dtype=bool)

    if issubclass(values.dtype.type, np.integer):
        new_values = new_values.astype(float)

    new_values.fill(np.nan)

    # is there a simpler / faster way of doing this?
    for i in xrange(values.shape[1]):
        chunk = new_values[:, i * width : (i + 1) * width]
        mask_chunk = new_mask[:, i * width : (i + 1) * width]

        chunk.flat[self.mask] = self.sorted_values[:, i]
        mask_chunk.flat[self.mask] = True

    new_values = new_values.take(self.unique_groups, axis=0)
    return new_values, new_mask


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def get_new_values(self):
    values = self.values
    # place the values
    length, width = self.full_shape
    stride = values.shape[1]
    result_width = width * stride

    new_values = np.empty((length, result_width), dtype=values.dtype)
    new_mask = np.zeros((length, result_width), dtype=bool)

    new_values = com._maybe_upcast(new_values)
    new_values.fill(np.nan)

    # is there a simpler / faster way of doing this?
    for i in xrange(values.shape[1]):
        chunk = new_values[:, i * width : (i + 1) * width]
        mask_chunk = new_mask[:, i * width : (i + 1) * width]

        chunk.flat[self.mask] = self.sorted_values[:, i]
        mask_chunk.flat[self.mask] = True

    new_values = new_values.take(self.unique_groups, axis=0)

    return new_values, new_mask
# Buggy code
def str_contains(arr, pat, case=True, flags=0):
    """
    Check whether given pattern is contained in each string in the array

    Parameters
    ----------
    pat : string
        Character sequence or regular expression
    case : boolean, default True
        If True, case sensitive
    flags : int, default 0 (no flags)
        re module flags, e.g. re.IGNORECASE

    Returns
    -------

    """
    if not case:
        flags |= re.IGNORECASE

    regex = re.compile(pat, flags=flags)

    f = lambda x: bool(regex.search(x))
    return _na_map(f, arr)


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def str_contains(arr, pat, case=True, flags=0, na=np.nan):
    """
    Check whether given pattern is contained in each string in the array

    Parameters
    ----------
    pat : string
        Character sequence or regular expression
    case : boolean, default True
        If True, case sensitive
    flags : int, default 0 (no flags)
        re module flags, e.g. re.IGNORECASE
    na : bool, default NaN

    Returns
    -------

    """
    if not case:
        flags |= re.IGNORECASE

    regex = re.compile(pat, flags=flags)

    f = lambda x: bool(regex.search(x))
    return _na_map(f, arr, na)
# Buggy code
def str_startswith(arr, pat):
    """
    Return boolean array indicating whether each string starts with passed
    pattern

    Parameters
    ----------
    pat : string
        Character sequence

    Returns
    -------
    startswith : array (boolean)
    """
    f = lambda x: x.startswith(pat)
    return _na_map(f, arr)


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def str_startswith(arr, pat, na=np.nan):
    """
    Return boolean array indicating whether each string starts with passed
    pattern

    Parameters
    ----------
    pat : string
        Character sequence
    na : bool, default NaN

    Returns
    -------
    startswith : array (boolean)
    """
    f = lambda x: x.startswith(pat)
    return _na_map(f, arr, na)
# Buggy code
def str_endswith(arr, pat):
    """
    Return boolean array indicating whether each string ends with passed
    pattern

    Parameters
    ----------
    pat : string
        Character sequence

    Returns
    -------
    endswith : array (boolean)
    """
    f = lambda x: x.endswith(pat)
    return _na_map(f, arr)


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def str_endswith(arr, pat, na=np.nan):
    """
    Return boolean array indicating whether each string ends with passed
    pattern

    Parameters
    ----------
    pat : string
        Character sequence
    na : bool, default NaN

    Returns
    -------
    endswith : array (boolean)
    """
    f = lambda x: x.endswith(pat)
    return _na_map(f, arr, na)
# Buggy code
def str_split(arr, pat, n=0):
    """
    Split each string (a la re.split) in array by given pattern, propagating NA
    values

    Parameters
    ----------
    pat : string
        String or regular expression to split on
    n : int, default 0 (all)

    Returns
    -------
    split : array
    """
    regex = re.compile(pat)
    f = lambda x: regex.split(x, maxsplit=n)
    return _na_map(f, arr)


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def str_split(arr, pat=None, n=0):
    """
    Split each string (a la re.split) in array by given pattern, propagating NA
    values

    Parameters
    ----------
    pat : string, default None
        String or regular expression to split on. If None, splits on whitespace
    n : int, default 0 (all)

    Returns
    -------
    split : array
    """
    if pat is None:
        f = lambda x: x.split()
    else:
        regex = re.compile(pat)
        f = lambda x: regex.split(x, maxsplit=n)

    return _na_map(f, arr)
# Buggy code
def _pat_wrapper(f, flags=False):
    def wrapper1(self, pat):
        result = f(self.series, pat)
        return self._wrap_result(result)

    def wrapper2(self, pat, flags=0):
        result = f(self.series, pat, flags=flags)
        return self._wrap_result(result)

    wrapper = wrapper2 if flags else wrapper1

    wrapper.__name__ = f.__name__
    if f.__doc__:
        wrapper.__doc__ = f.__doc__

    return wrapper


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def _pat_wrapper(f, flags=False, na=False):
    def wrapper1(self, pat):
        result = f(self.series, pat)
        return self._wrap_result(result)

    def wrapper2(self, pat, flags=0):
        result = f(self.series, pat, flags=flags)
        return self._wrap_result(result)

    def wrapper3(self, pat, na=np.nan):
        result = f(self.series, pat, na=na)
        return self._wrap_result(result)

    wrapper = wrapper3 if na else wrapper2 if flags else wrapper1

    wrapper.__name__ = f.__name__
    if f.__doc__:
        wrapper.__doc__ = f.__doc__

    return wrapper
# Buggy code
def split(self, pat, n=0):
    result = str_split(self.series, pat, n=n)
    return self._wrap_result(result)


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def split(self, pat=None, n=0):
    result = str_split(self.series, pat, n=n)
    return self._wrap_result(result)
# Buggy code
def contains(self, pat, case=True, flags=0):
    result = str_contains(self.series, pat, case=case, flags=flags)
    return self._wrap_result(result)


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def contains(self, pat, case=True, flags=0, na=np.nan):
    result = str_contains(self.series, pat, case=case, flags=flags, na=np.nan)
    return self._wrap_result(result)
# Buggy code
def as_matrix(self, columns=None):
    """
    Convert the frame to its Numpy-array matrix representation

    Columns are presented in sorted order unless a specific list
    of columns is provided.
    """
    if columns is None:
        columns = self.columns

    if len(columns) == 0:
        return np.zeros((len(self.index), 0), dtype=float)

    return np.array([self[col].values for col in columns]).T


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def as_matrix(self, columns=None):
    """
    Convert the frame to its Numpy-array matrix representation

    Columns are presented in sorted order unless a specific list
    of columns is provided.
    """
    if columns is None:
        columns = self.columns

    if len(columns) == 0:
        return np.zeros((len(self.index), 0), dtype=float)

    return np.array([self.icol(i).values for i in range(len(self.columns))]).T
# Buggy code
def _first_valid_index(arr):
    # argmax scans from left
    return notnull(arr).argmax()


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def _first_valid_index(arr):
    # argmax scans from left
    return notnull(arr).argmax() if len(arr) else 0
# Buggy code
def ewmstd(arg, com=None, span=None, min_periods=0, bias=False, time_rule=None):
    result = ewmvar(
        arg, com=com, span=span, time_rule=time_rule, min_periods=min_periods, bias=bias
    )
    return np.sqrt(result)


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def ewmstd(arg, com=None, span=None, min_periods=0, bias=False, time_rule=None):
    result = ewmvar(
        arg, com=com, span=span, time_rule=time_rule, min_periods=min_periods, bias=bias
    )
    return _zsqrt(result)
# Buggy code
def ewmcorr(arg1, arg2, com=None, span=None, min_periods=0, freq=None, time_rule=None):
    X, Y = _prep_binary(arg1, arg2)

    X = _conv_timerule(X, freq, time_rule)
    Y = _conv_timerule(Y, freq, time_rule)

    mean = lambda x: ewma(x, com=com, span=span, min_periods=min_periods)
    var = lambda x: ewmvar(x, com=com, span=span, min_periods=min_periods, bias=True)
    return (mean(X * Y) - mean(X) * mean(Y)) / np.sqrt(var(X) * var(Y))


# Traceback (Exception)
In [25]: df = pandas.DataFrame(np.random.randn(4,4), columns=list('AABC'))

In [26]: df
Out[26]:
A         A         B         C
0 -0.174905  0.332522  1.134984 -0.201270
1  1.730445  0.382556 -0.607761  1.221815
2  0.513049  0.196231 -1.746732 -0.252282
3 -0.297577 -1.000121 -0.090442 -2.129467

In [27]: df.ix[:,['A', 'B']]
Out[27]:
A         A         B
0 -0.174905  0.332522  1.134984
1  1.730445  0.382556 -0.607761
2  0.513049  0.196231 -1.746732
3 -0.297577 -1.000121 -0.090442

In [28]: df[['A', 'B']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

In [29]: df[['B', 'C']]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...
Exception: Reindexing only valid with uniquely valued Index objects

# Fixed code
def ewmcorr(arg1, arg2, com=None, span=None, min_periods=0, freq=None, time_rule=None):
    X, Y = _prep_binary(arg1, arg2)

    X = _conv_timerule(X, freq, time_rule)
    Y = _conv_timerule(Y, freq, time_rule)

    mean = lambda x: ewma(x, com=com, span=span, min_periods=min_periods)
    var = lambda x: ewmvar(x, com=com, span=span, min_periods=min_periods, bias=True)
    return (mean(X * Y) - mean(X) * mean(Y)) / _zsqrt(var(X) * var(Y))