gurudesh/pytracebugs huggingface dataset:
# Buggy code
def _transform_fast(self, func):
    """
    fast version of transform, only applicable to
    builtin/cythonizable functions
    """
    if isinstance(func, compat.string_types):
        func = getattr(self, func)

    ids, _, ngroup = self.grouper.group_info
    mask = ids != -1

    out = func().values[ids]
    if not mask.all():
        out = np.where(mask, out, np.nan)

    obs = np.zeros(ngroup, dtype="bool")
    obs[ids[mask]] = True
    if not obs.all():
        out = self._try_cast(out, self._selected_obj)

    return Series(out, index=self.obj.index)


# Traceback (TypeError)
In [20]: df = pd.DataFrame({'grouping':[np.nan,1,1,3],
'v':[1.1, 2.1, 3.1, 4.5],
'd':pd.date_range('2014-1-1','2014-1-4')})

In [21]: df.groupby('grouping')['d'].transform('first')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-cb1ed73aabc3> in <module>()
----> 1 df.groupby('grouping')['d'].transform('first')

C:\Users\Chris\Anaconda\lib\site-packages\pandas\core\groupby.pyc in transform(self, func, *args, **kwargs)
2738                 # cythonized aggregation and merge
2739                 return self._transform_fast(
-> 2740                     lambda: getattr(self, func)(*args, **kwargs))
2741
2742         # reg transform

C:\Users\Chris\Anaconda\lib\site-packages\pandas\core\groupby.pyc in _transform_fast(self, func)
2781         out = func().values[ids]
2782         if not mask.all():
-> 2783             out = np.where(mask, out, np.nan)
2784
2785         obs = np.zeros(ngroup, dtype='bool')

TypeError: invalid type promotion

# Fixed code
def _transform_fast(self, func):
    """
    fast version of transform, only applicable to
    builtin/cythonizable functions
    """
    if isinstance(func, compat.string_types):
        func = getattr(self, func)

    ids, _, ngroup = self.grouper.group_info
    cast = (self.size().fillna(0) > 0).any()
    out = algos.take_1d(func().values, ids)
    if cast:
        out = self._try_cast(out, self.obj)
    return Series(out, index=self.obj.index, name=self.obj.name)
# Buggy code
def _parse_raw_thead(self, table):
    thead = self._parse_thead(table)
    res = []
    if thead:
        res = lmap(self._text_getter, self._parse_th(thead[0]))
    return np.array(res).squeeze() if res and len(res) == 1 else res


# Traceback (TypeError)
Traceback (most recent call last):
File "./posti.py", line 69, in <module>
dfs = read_html(html)
File "/usr/lib/python3.4/site-packages/pandas/io/html.py", line 851, in read_html
parse_dates, tupleize_cols, thousands, attrs, encoding)
File "/usr/lib/python3.4/site-packages/pandas/io/html.py", line 721, in _parse
infer_types, parse_dates, tupleize_cols, thousands))
File "/usr/lib/python3.4/site-packages/pandas/io/html.py", line 609, in _data_to_frame
_expand_elements(body)
File "/usr/lib/python3.4/site-packages/pandas/io/html.py", line 586, in _expand_elements
lens = Series(lmap(len, body))
File "/usr/lib/python3.4/site-packages/pandas/compat/__init__.py", line 87, in lmap
return list(map(*args, **kwargs))
TypeError: len() of unsized object

# Fixed code
def _parse_raw_thead(self, table):
    thead = self._parse_thead(table)
    res = []
    if thead:
        res = lmap(self._text_getter, self._parse_th(thead[0]))
    return np.atleast_1d(np.array(res).squeeze()) if res and len(res) == 1 else res
# Buggy code
def _parse_raw_tfoot(self, table):
    tfoot = self._parse_tfoot(table)
    res = []
    if tfoot:
        res = lmap(self._text_getter, self._parse_td(tfoot[0]))
    return np.array(res).squeeze() if res and len(res) == 1 else res


# Traceback (TypeError)
Traceback (most recent call last):
File "./posti.py", line 69, in <module>
dfs = read_html(html)
File "/usr/lib/python3.4/site-packages/pandas/io/html.py", line 851, in read_html
parse_dates, tupleize_cols, thousands, attrs, encoding)
File "/usr/lib/python3.4/site-packages/pandas/io/html.py", line 721, in _parse
infer_types, parse_dates, tupleize_cols, thousands))
File "/usr/lib/python3.4/site-packages/pandas/io/html.py", line 609, in _data_to_frame
_expand_elements(body)
File "/usr/lib/python3.4/site-packages/pandas/io/html.py", line 586, in _expand_elements
lens = Series(lmap(len, body))
File "/usr/lib/python3.4/site-packages/pandas/compat/__init__.py", line 87, in lmap
return list(map(*args, **kwargs))
TypeError: len() of unsized object

# Fixed code
def _parse_raw_tfoot(self, table):
    tfoot = self._parse_tfoot(table)
    res = []
    if tfoot:
        res = lmap(self._text_getter, self._parse_td(tfoot[0]))
    return np.atleast_1d(np.array(res).squeeze()) if res and len(res) == 1 else res
# Buggy code
def is_null(self):
    if self.block is None:
        return True

    if not self.block._can_hold_na:
        return False

    # Usually it's enough to check but a small fraction of values to see if
    # a block is NOT null, chunks should help in such cases.  1000 value
    # was chosen rather arbitrarily.
    values = self.block.values
    if self.block.is_categorical:
        values_flat = values.categories
    else:
        values_flat = values.ravel(order="K")
    total_len = values_flat.shape[0]
    chunk_len = max(total_len // 40, 1000)
    for i in range(0, total_len, chunk_len):
        if not isnull(values_flat[i : i + chunk_len]).all():
            return False

    return True


# Traceback (AssertionError)
.
Expected:
A  B  C
0  1  0  0
1  0  0  0
2  1  0  1
3  0  0  0
Got:
A   B  C
0  1 NaN  0
1  0 NaN  0
2  1 NaN  1
3  0 NaN  0
F
======================================================================
FAIL: test_concat_sparse_to_df (__main__.TestSparseConcat)
----------------------------------------------------------------------
Traceback (most recent call last):
File "example.py", line 28, in test_concat_sparse_to_df
assert_frame_equal(C, self.expected)
File "/home/vagrant/.virtualenvs/ai-modeling/lib/python2.7/site-packages/pandas/util/testing.py", line 748, in assert_frame_equal
check_exact=check_exact)
File "/home/vagrant/.virtualenvs/ai-modeling/lib/python2.7/site-packages/pandas/util/testing.py", line 692, in assert_series_equal
assert_almost_equal(left.values, right.values, check_less_precise)
File "das/src/testing.pyx", line 58, in pandas._testing.assert_almost_equal (pandas/src/testing.c:2758)
File "das/src/testing.pyx", line 93, in pandas._testing.assert_almost_equal (pandas/src/testing.c:1843)
File "das/src/testing.pyx", line 102, in pandas._testing.assert_almost_equal (pandas/src/testing.c:2010)
AssertionError: First object is null, second isn't: nan != 0.0

----------------------------------------------------------------------
Ran 2 tests in 0.024s

FAILED (failures=1)

# Fixed code
def is_null(self):
    if self.block is None:
        return True

    if not self.block._can_hold_na:
        return False

    # Usually it's enough to check but a small fraction of values to see if
    # a block is NOT null, chunks should help in such cases.  1000 value
    # was chosen rather arbitrarily.
    values = self.block.values
    if self.block.is_categorical:
        values_flat = values.categories
    elif self.block.is_sparse:
        # fill_value is not NaN and have holes
        if not values._null_fill_value and values.sp_index.ngaps > 0:
            return False
        values_flat = values.ravel(order="K")
    else:
        values_flat = values.ravel(order="K")
    total_len = values_flat.shape[0]
    chunk_len = max(total_len // 40, 1000)
    for i in range(0, total_len, chunk_len):
        if not isnull(values_flat[i : i + chunk_len]).all():
            return False

    return True
# Buggy code
def __getitem__(self, key):
    """ """
    if com.is_integer(key):
        return self._get_val_at(key)
    else:
        if isinstance(key, SparseArray):
            key = np.asarray(key)
        if hasattr(key, "__len__") and len(self) != len(key):
            return self.take(key)
        else:
            data_slice = self.values[key]
        return self._constructor(data_slice)


# Traceback (AssertionError)
.
Expected:
A  B  C
0  1  0  0
1  0  0  0
2  1  0  1
3  0  0  0
Got:
A   B  C
0  1 NaN  0
1  0 NaN  0
2  1 NaN  1
3  0 NaN  0
F
======================================================================
FAIL: test_concat_sparse_to_df (__main__.TestSparseConcat)
----------------------------------------------------------------------
Traceback (most recent call last):
File "example.py", line 28, in test_concat_sparse_to_df
assert_frame_equal(C, self.expected)
File "/home/vagrant/.virtualenvs/ai-modeling/lib/python2.7/site-packages/pandas/util/testing.py", line 748, in assert_frame_equal
check_exact=check_exact)
File "/home/vagrant/.virtualenvs/ai-modeling/lib/python2.7/site-packages/pandas/util/testing.py", line 692, in assert_series_equal
assert_almost_equal(left.values, right.values, check_less_precise)
File "das/src/testing.pyx", line 58, in pandas._testing.assert_almost_equal (pandas/src/testing.c:2758)
File "das/src/testing.pyx", line 93, in pandas._testing.assert_almost_equal (pandas/src/testing.c:1843)
File "das/src/testing.pyx", line 102, in pandas._testing.assert_almost_equal (pandas/src/testing.c:2010)
AssertionError: First object is null, second isn't: nan != 0.0

----------------------------------------------------------------------
Ran 2 tests in 0.024s

FAILED (failures=1)

# Fixed code
def __getitem__(self, key):
    """ """
    if com.is_integer(key):
        return self._get_val_at(key)
    elif isinstance(key, tuple):
        data_slice = self.values[key]
    else:
        if isinstance(key, SparseArray):
            key = np.asarray(key)

        if hasattr(key, "__len__") and len(self) != len(key):
            return self.take(key)
        else:
            data_slice = self.values[key]

    return self._constructor(data_slice)
# Buggy code
def _maybe_upcast(values, fill_value=np.nan, dtype=None, copy=False):
    """provide explict type promotion and coercion

    Parameters
    ----------
    values : the ndarray that we want to maybe upcast
    fill_value : what we want to fill with
    dtype : if None, then use the dtype of the values, else coerce to this type
    copy : if True always make a copy even if no upcast is required
    """

    if is_internal_type(values):
        if copy:
            values = values.copy()
    else:
        if dtype is None:
            dtype = values.dtype
        new_dtype, fill_value = _maybe_promote(dtype, fill_value)
        if new_dtype != values.dtype:
            values = values.astype(new_dtype)
        elif copy:
            values = values.copy()

    return values, fill_value


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-ba941613799a> in <module>()
----> 1 df.publication_timestamp.map(lambda x: x)

/Users/johria/.pyenv/versions/3.5.1/lib/python3.5/site-packages/pandas/core/series.py in map(self, arg, na_action)
2052                                      index=self.index).__finalize__(self)
2053         else:
-> 2054             mapped = map_f(values, arg)
2055             return self._constructor(mapped,
2056                                      index=self.index).__finalize__(self)

TypeError: Argument 'arr' has incorrect type (expected numpy.ndarray, got DatetimeIndex)

# Fixed code
def _maybe_upcast(values, fill_value=np.nan, dtype=None, copy=False):
    """provide explict type promotion and coercion

    Parameters
    ----------
    values : the ndarray that we want to maybe upcast
    fill_value : what we want to fill with
    dtype : if None, then use the dtype of the values, else coerce to this type
    copy : if True always make a copy even if no upcast is required
    """

    if is_extension_type(values):
        if copy:
            values = values.copy()
    else:
        if dtype is None:
            dtype = values.dtype
        new_dtype, fill_value = _maybe_promote(dtype, fill_value)
        if new_dtype != values.dtype:
            values = values.astype(new_dtype)
        elif copy:
            values = values.copy()

    return values, fill_value
# Buggy code
def should_store(self, value):
    return not (
        issubclass(
            value.dtype.type,
            (np.integer, np.floating, np.complexfloating, np.datetime64, np.bool_),
        )
        or is_internal_type(value)
    )


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-ba941613799a> in <module>()
----> 1 df.publication_timestamp.map(lambda x: x)

/Users/johria/.pyenv/versions/3.5.1/lib/python3.5/site-packages/pandas/core/series.py in map(self, arg, na_action)
2052                                      index=self.index).__finalize__(self)
2053         else:
-> 2054             mapped = map_f(values, arg)
2055             return self._constructor(mapped,
2056                                      index=self.index).__finalize__(self)

TypeError: Argument 'arr' has incorrect type (expected numpy.ndarray, got DatetimeIndex)

# Fixed code
def should_store(self, value):
    return not (
        issubclass(
            value.dtype.type,
            (np.integer, np.floating, np.complexfloating, np.datetime64, np.bool_),
        )
        or is_extension_type(value)
    )
# Buggy code
def map_f(values, f):
    return lib.map_infer_mask(values, f, mask.view(np.uint8))


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-ba941613799a> in <module>()
----> 1 df.publication_timestamp.map(lambda x: x)

/Users/johria/.pyenv/versions/3.5.1/lib/python3.5/site-packages/pandas/core/series.py in map(self, arg, na_action)
2052                                      index=self.index).__finalize__(self)
2053         else:
-> 2054             mapped = map_f(values, arg)
2055             return self._constructor(mapped,
2056                                      index=self.index).__finalize__(self)

TypeError: Argument 'arr' has incorrect type (expected numpy.ndarray, got DatetimeIndex)

# Fixed code
def map_f(values, f):
    return lib.map_infer_mask(values, f, isnull(values).view(np.uint8))
# Buggy code
def _try_cast(arr, take_fast_path):
    # perf shortcut as this is the most common case
    if take_fast_path:
        if _possibly_castable(arr) and not copy and dtype is None:
            return arr

    try:
        subarr = _possibly_cast_to_datetime(arr, dtype)
        if not is_internal_type(subarr):
            subarr = np.array(subarr, dtype=dtype, copy=copy)
    except (ValueError, TypeError):
        if is_categorical_dtype(dtype):
            subarr = Categorical(arr)
        elif dtype is not None and raise_cast_failure:
            raise
        else:
            subarr = np.array(arr, dtype=object, copy=copy)
    return subarr


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-ba941613799a> in <module>()
----> 1 df.publication_timestamp.map(lambda x: x)

/Users/johria/.pyenv/versions/3.5.1/lib/python3.5/site-packages/pandas/core/series.py in map(self, arg, na_action)
2052                                      index=self.index).__finalize__(self)
2053         else:
-> 2054             mapped = map_f(values, arg)
2055             return self._constructor(mapped,
2056                                      index=self.index).__finalize__(self)

TypeError: Argument 'arr' has incorrect type (expected numpy.ndarray, got DatetimeIndex)

# Fixed code
def _try_cast(arr, take_fast_path):
    # perf shortcut as this is the most common case
    if take_fast_path:
        if _possibly_castable(arr) and not copy and dtype is None:
            return arr

    try:
        subarr = _possibly_cast_to_datetime(arr, dtype)
        if not is_extension_type(subarr):
            subarr = np.array(subarr, dtype=dtype, copy=copy)
    except (ValueError, TypeError):
        if is_categorical_dtype(dtype):
            subarr = Categorical(arr)
        elif dtype is not None and raise_cast_failure:
            raise
        else:
            subarr = np.array(arr, dtype=object, copy=copy)
    return subarr
# Buggy code
def map(self, mapper):
    return self._arrmap(self.values, mapper)


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-ba941613799a> in <module>()
----> 1 df.publication_timestamp.map(lambda x: x)

/Users/johria/.pyenv/versions/3.5.1/lib/python3.5/site-packages/pandas/core/series.py in map(self, arg, na_action)
2052                                      index=self.index).__finalize__(self)
2053         else:
-> 2054             mapped = map_f(values, arg)
2055             return self._constructor(mapped,
2056                                      index=self.index).__finalize__(self)

TypeError: Argument 'arr' has incorrect type (expected numpy.ndarray, got DatetimeIndex)

# Fixed code
def map(self, mapper):
    """
    Apply mapper function to its values.

    Parameters
    ----------
    mapper : callable
        Function to be applied.

    Returns
    -------
    applied : array
    """
    return self._arrmap(self.values, mapper)
# Buggy code
def __getitem__(self, key):
    """ """
    try:
        return self._get_val_at(self.index.get_loc(key))

    except KeyError:
        if isinstance(key, (int, np.integer)):
            return self._get_val_at(key)
        raise Exception("Requested index not in this series!")

    except TypeError:
        # Could not hash item, must be array-like?
        pass

    # is there a case where this would NOT be an ndarray?
    # need to find an example, I took out the case for now

    key = _values_from_object(key)
    dataSlice = self.values[key]
    new_index = Index(self.index.view(ndarray)[key])
    return self._constructor(dataSlice, index=new_index).__finalize__(self)


# Traceback (Exception)
In [2]: import pandas as pd

In [3]: ss = pd.Series(range(10)).to_sparse()

In [4]: ss[...]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-4-f0ccf654123b> in <module>()
----> 1 ss[...]

/Users/shoyer/dev/pandas/pandas/sparse/series.pyc in __getitem__(self, key)
369             if isinstance(key, (int, np.integer)):
370                 return self._get_val_at(key)
--> 371             raise Exception('Requested index not in this series!')
372
373         except TypeError:

Exception: Requested index not in this series!

# Fixed code
def __getitem__(self, key):
    try:
        return self._get_val_at(self.index.get_loc(key))

    except KeyError:
        if isinstance(key, (int, np.integer)):
            return self._get_val_at(key)
        elif key is Ellipsis:
            return self
        raise Exception("Requested index not in this series!")

    except TypeError:
        # Could not hash item, must be array-like?
        pass

    # is there a case where this would NOT be an ndarray?
    # need to find an example, I took out the case for now

    key = _values_from_object(key)
    dataSlice = self.values[key]
    new_index = Index(self.index.view(ndarray)[key])
    return self._constructor(dataSlice, index=new_index).__finalize__(self)
# Buggy code
def _apply_filter(self, indices, dropna):
    if len(indices) == 0:
        indices = []
    else:
        indices = np.sort(np.concatenate(indices))
    if dropna:
        filtered = self._selected_obj.take(indices, axis=self.axis)
    else:
        mask = np.empty(len(self._selected_obj.index), dtype=bool)
        mask.fill(False)
        mask[indices.astype(int)] = True
        # mask fails to broadcast when passed to where; broadcast manually.
        mask = np.tile(mask, list(self._selected_obj.shape[1:]) + [1]).T
        filtered = self._selected_obj.where(mask)  # Fill with NaNs.
    return filtered


# Traceback (AttributeError)
AttributeError                            Traceback (most recent call last)
<ipython-input-11-ffb9adbc134a> in <module>()
----> 1 pd.DataFrame({'a': [1,1,2], 'b':[1,2,0]}).groupby('a').filter(lambda x: x['b'].sum() > 5, dropna=False)

....../local/lib/python2.7/site-packages/pandas/core/groupby.py in filter(self, func, dropna, *args, **kwargs)
3570                                 type(res).__name__)
3571
-> 3572         return self._apply_filter(indices, dropna)
3573
3574

....../local/lib/python2.7/site-packages/pandas/core/groupby.py in _apply_filter(self, indices, dropna)
831             mask = np.empty(len(self._selected_obj.index), dtype=bool)
832             mask.fill(False)
--> 833             mask[indices.astype(int)] = True
834             # mask fails to broadcast when passed to where; broadcast manually.
835             mask = np.tile(mask, list(self._selected_obj.shape[1:]) + [1]).T

AttributeError: 'list' object has no attribute 'astype'

# Fixed code
def _apply_filter(self, indices, dropna):
    if len(indices) == 0:
        indices = np.array([], dtype="int64")
    else:
        indices = np.sort(np.concatenate(indices))
    if dropna:
        filtered = self._selected_obj.take(indices, axis=self.axis)
    else:
        mask = np.empty(len(self._selected_obj.index), dtype=bool)
        mask.fill(False)
        mask[indices.astype(int)] = True
        # mask fails to broadcast when passed to where; broadcast manually.
        mask = np.tile(mask, list(self._selected_obj.shape[1:]) + [1]).T
        filtered = self._selected_obj.where(mask)  # Fill with NaNs.
    return filtered
# Buggy code
def __getitem__(self, key):
    """ """
    if com.is_integer(key):
        return self._get_val_at(key)
    else:
        if isinstance(key, SparseArray):
            key = np.asarray(key)
        if hasattr(key, "__len__") and len(self) != len(key):
            indices = self.sp_index
            if hasattr(indices, "to_int_index"):
                indices = indices.to_int_index()
            data_slice = self.values.take(indices.indices)[key]
        else:
            data_slice = self.values[key]
        return self._constructor(data_slice)


# Traceback (TypeError)
In [2]: pd.__version__
Out[2]: '0.16.2-123-gdf1f5cf'

In [3]: pd.options.display.max_rows = 3

In [4]: pd.Series(randn(3)).to_sparse()
Out[4]:
0    1.100684
1   -0.924482
2   -0.106069
dtype: float64
BlockIndex
Block locations: array([0], dtype=int32)
Block lengths: array([3], dtype=int32)

In [5]: pd.Series(randn(4)).to_sparse()
Out[5]: ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
...
TypeError: cannot concatenate a non-NDFrame object

# Fixed code
def __getitem__(self, key):
    """ """
    if com.is_integer(key):
        return self._get_val_at(key)
    else:
        if isinstance(key, SparseArray):
            key = np.asarray(key)
        if hasattr(key, "__len__") and len(self) != len(key):
            return self.take(key)
        else:
            data_slice = self.values[key]
        return self._constructor(data_slice)
# Buggy code
def take(self, indices, axis=0):
    """
    Sparse-compatible version of ndarray.take

    Returns
    -------
    taken : ndarray
    """
    if axis:
        raise ValueError("axis must be 0, input was {0}".format(axis))
    indices = np.atleast_1d(np.asarray(indices, dtype=int))

    # allow -1 to indicate missing values
    n = len(self)
    if ((indices >= n) | (indices < -1)).any():
        raise IndexError("out of bounds access")

    if self.sp_index.npoints > 0:
        locs = np.array(
            [self.sp_index.lookup(loc) if loc > -1 else -1 for loc in indices]
        )
        result = self.sp_values.take(locs)
        mask = locs == -1
        if mask.any():
            try:
                result[mask] = self.fill_value
            except ValueError:
                # wrong dtype
                result = result.astype("float64")
                result[mask] = self.fill_value

    else:
        result = np.empty(len(indices))
        result.fill(self.fill_value)

    return result


# Traceback (TypeError)
In [2]: pd.__version__
Out[2]: '0.16.2-123-gdf1f5cf'

In [3]: pd.options.display.max_rows = 3

In [4]: pd.Series(randn(3)).to_sparse()
Out[4]:
0    1.100684
1   -0.924482
2   -0.106069
dtype: float64
BlockIndex
Block locations: array([0], dtype=int32)
Block lengths: array([3], dtype=int32)

In [5]: pd.Series(randn(4)).to_sparse()
Out[5]: ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
...
TypeError: cannot concatenate a non-NDFrame object

# Fixed code
def take(self, indices, axis=0):
    """
    Sparse-compatible version of ndarray.take

    Returns
    -------
    taken : ndarray
    """
    if axis:
        raise ValueError("axis must be 0, input was {0}".format(axis))

    if com.is_integer(indices):
        # return scalar
        return self[indices]

    indices = np.atleast_1d(np.asarray(indices, dtype=int))

    # allow -1 to indicate missing values
    n = len(self)
    if ((indices >= n) | (indices < -1)).any():
        raise IndexError("out of bounds access")

    if self.sp_index.npoints > 0:
        locs = np.array(
            [self.sp_index.lookup(loc) if loc > -1 else -1 for loc in indices]
        )
        result = self.sp_values.take(locs)
        mask = locs == -1
        if mask.any():
            try:
                result[mask] = self.fill_value
            except ValueError:
                # wrong dtype
                result = result.astype("float64")
                result[mask] = self.fill_value

    else:
        result = np.empty(len(indices))
        result.fill(self.fill_value)

    return self._constructor(result)
# Buggy code
def _reindex_index(
    self, index, method, copy, level, fill_value=np.nan, limit=None, takeable=False
):
    if level is not None:
        raise TypeError("Reindex by level not supported for sparse")

    if self.index.equals(index):
        if copy:
            return self.copy()
        else:
            return self

    if len(self.index) == 0:
        return SparseDataFrame(index=index, columns=self.columns)

    indexer = self.index.get_indexer(index, method, limit=limit)
    indexer = com._ensure_platform_int(indexer)
    mask = indexer == -1
    need_mask = mask.any()

    new_series = {}
    for col, series in self.iteritems():
        if mask.all():
            continue

        values = series.values
        new = values.take(indexer)

        if need_mask:
            np.putmask(new, mask, fill_value)

        new_series[col] = new

    return SparseDataFrame(
        new_series,
        index=index,
        columns=self.columns,
        default_fill_value=self._default_fill_value,
    )


# Traceback (TypeError)
In [2]: pd.__version__
Out[2]: '0.16.2-123-gdf1f5cf'

In [3]: pd.options.display.max_rows = 3

In [4]: pd.Series(randn(3)).to_sparse()
Out[4]:
0    1.100684
1   -0.924482
2   -0.106069
dtype: float64
BlockIndex
Block locations: array([0], dtype=int32)
Block lengths: array([3], dtype=int32)

In [5]: pd.Series(randn(4)).to_sparse()
Out[5]: ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
...
TypeError: cannot concatenate a non-NDFrame object

# Fixed code
def _reindex_index(
    self, index, method, copy, level, fill_value=np.nan, limit=None, takeable=False
):
    if level is not None:
        raise TypeError("Reindex by level not supported for sparse")

    if self.index.equals(index):
        if copy:
            return self.copy()
        else:
            return self

    if len(self.index) == 0:
        return SparseDataFrame(index=index, columns=self.columns)

    indexer = self.index.get_indexer(index, method, limit=limit)
    indexer = com._ensure_platform_int(indexer)
    mask = indexer == -1
    need_mask = mask.any()

    new_series = {}
    for col, series in self.iteritems():
        if mask.all():
            continue

        values = series.values
        # .take returns SparseArray
        new = values.take(indexer)
        if need_mask:
            new = new.values
            np.putmask(new, mask, fill_value)

        new_series[col] = new

    return SparseDataFrame(
        new_series,
        index=index,
        columns=self.columns,
        default_fill_value=self._default_fill_value,
    )
# Buggy code
def reindex(self, index=None, method=None, copy=True, limit=None):
    """
    Conform SparseSeries to new Index

    See Series.reindex docstring for general behavior

    Returns
    -------
    reindexed : SparseSeries
    """
    new_index = _ensure_index(index)

    if self.index.equals(new_index):
        if copy:
            return self.copy()
        else:
            return self
    return self._constructor(
        self._data.reindex(new_index, method=method, limit=limit, copy=copy),
        index=new_index,
    ).__finalize__(self)


# Traceback (TypeError)
In [2]: pd.__version__
Out[2]: '0.16.2-123-gdf1f5cf'

In [3]: pd.options.display.max_rows = 3

In [4]: pd.Series(randn(3)).to_sparse()
Out[4]:
0    1.100684
1   -0.924482
2   -0.106069
dtype: float64
BlockIndex
Block locations: array([0], dtype=int32)
Block lengths: array([3], dtype=int32)

In [5]: pd.Series(randn(4)).to_sparse()
Out[5]: ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
...
TypeError: cannot concatenate a non-NDFrame object

# Fixed code
def reindex(self, index=None, method=None, copy=True, limit=None, **kwargs):
    """
    Conform SparseSeries to new Index

    See Series.reindex docstring for general behavior

    Returns
    -------
    reindexed : SparseSeries
    """
    new_index = _ensure_index(index)

    if self.index.equals(new_index):
        if copy:
            return self.copy()
        else:
            return self
    return self._constructor(
        self._data.reindex(new_index, method=method, limit=limit, copy=copy),
        index=new_index,
    ).__finalize__(self)
# Buggy code
def create_from_value(value, index, dtype):
    # return a new empty value suitable for the dtype

    if is_datetimetz(dtype):
        subarr = DatetimeIndex([value] * len(index), dtype=dtype)
    else:
        if not isinstance(dtype, (np.dtype, type(np.dtype))):
            dtype = dtype.dtype
        subarr = np.empty(len(index), dtype=dtype)
        subarr.fill(value)

    return subarr


# Traceback (AttributeError)
In [2]: pd.Series([0,0,0], dtype="category")
Out[2]:
0    0
1    0
2    0
dtype: category
Categories (1, int64): [0]

In [3]: pd.Series(0, index=range(3), dtype="category")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-2c1d7a241071> in <module>()
----> 1 pd.Series(0, index=range(3), dtype="category")

/home/nobackup/repo/pandas/pandas/core/series.pyc in __init__(self, data, index, dtype, name, copy, fastpath)
224             else:
225                 data = _sanitize_array(data, index, dtype, copy,
--> 226                                        raise_cast_failure=True)
227
228                 data = SingleBlockManager(data, index, fastpath=True)

/home/nobackup/repo/pandas/pandas/core/series.pyc in _sanitize_array(data, index, dtype, copy, raise_cast_failure)
2961             if len(subarr) != len(index) and len(subarr) == 1:
2962                 subarr = create_from_value(subarr[0], index,
-> 2963                                            subarr.dtype)
2964
2965     elif subarr.ndim > 1:

/home/nobackup/repo/pandas/pandas/core/series.pyc in create_from_value(value, index, dtype)
2929         else:
2930             if not isinstance(dtype, (np.dtype, type(np.dtype))):
-> 2931                 dtype = dtype.dtype
2932             subarr = np.empty(len(index), dtype=dtype)
2933             subarr.fill(value)

AttributeError: 'CategoricalDtype' object has no attribute 'dtype'

# Fixed code
def create_from_value(value, index, dtype):
    # return a new empty value suitable for the dtype

    if is_datetimetz(dtype):
        subarr = DatetimeIndex([value] * len(index), dtype=dtype)
    elif is_categorical_dtype(dtype):
        subarr = Categorical([value] * len(index))
    else:
        if not isinstance(dtype, (np.dtype, type(np.dtype))):
            dtype = dtype.dtype
        subarr = np.empty(len(index), dtype=dtype)
        subarr.fill(value)

    return subarr
# Buggy code
def set_precision(self, precision):
    """
    Set the precision used to render.

    .. versionadded:: 0.17.1

    Parameters
    ----------
    precision: int

    Returns
    -------
    self
    """
    self.precision = precision
    return self


# Traceback (KeyError)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.5/site-packages/IPython/core/formatters.py in __call__(self, obj)
339             method = _safe_get_formatter_method(obj, self.print_method)
340             if method is not None:
--> 341                 return method()
342             return None
343         else:

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in _repr_html_(self)
158         Hooks into Jupyter notebook rich display system.
159         '''
--> 160         return self.render()
161
162     def _translate(self):

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in render(self)
259         """
260         self._compute()
--> 261         d = self._translate()
262         # filter out empty styles, every cell will have a class
263         # but the list of props may just be [['', '']].

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in _translate(self)
220                 cs = [DATA_CLASS, "row%s" % r, "col%s" % c]
221                 cs.extend(cell_context.get("data", {}).get(r, {}).get(c, []))
--> 222                 row_es.append({"type": "td", "value": self.data.iloc[r][c],
223                                "class": " ".join(cs), "id": "_".join(cs[1:])})
224                 props = []

/usr/local/lib/python3.5/site-packages/pandas/core/series.py in __getitem__(self, key)
555     def __getitem__(self, key):
556         try:
--> 557             result = self.index.get_value(self, key)
558
559             if not np.isscalar(result):

/usr/local/lib/python3.5/site-packages/pandas/core/index.py in get_value(self, series, key)
1788
1789         try:
-> 1790             return self._engine.get_value(s, k)
1791         except KeyError as e1:
1792             if len(self) > 0 and self.inferred_type in ['integer','boolean']:

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3204)()

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:2903)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3843)()

pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6525)()

pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6463)()

KeyError: 0

# Fixed code
def set_precision(self, precision):
    """
    Set the precision used to render.

    .. versionadded:: 0.17.1

    Parameters
    ----------
    precision: int

    Returns
    -------
    self : Styler
    """
    self.precision = precision
    return self
# Buggy code
def set_table_attributes(self, attributes):
    """
    Set the table attributes. These are the items
    that show up in the opening ``<table>`` tag in addition
    to to automatic (by default) id.

    .. versionadded:: 0.17.1

    Parameters
    ----------
    precision: int

    Returns
    -------
    self
    """
    self.table_attributes = attributes
    return self


# Traceback (KeyError)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.5/site-packages/IPython/core/formatters.py in __call__(self, obj)
339             method = _safe_get_formatter_method(obj, self.print_method)
340             if method is not None:
--> 341                 return method()
342             return None
343         else:

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in _repr_html_(self)
158         Hooks into Jupyter notebook rich display system.
159         '''
--> 160         return self.render()
161
162     def _translate(self):

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in render(self)
259         """
260         self._compute()
--> 261         d = self._translate()
262         # filter out empty styles, every cell will have a class
263         # but the list of props may just be [['', '']].

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in _translate(self)
220                 cs = [DATA_CLASS, "row%s" % r, "col%s" % c]
221                 cs.extend(cell_context.get("data", {}).get(r, {}).get(c, []))
--> 222                 row_es.append({"type": "td", "value": self.data.iloc[r][c],
223                                "class": " ".join(cs), "id": "_".join(cs[1:])})
224                 props = []

/usr/local/lib/python3.5/site-packages/pandas/core/series.py in __getitem__(self, key)
555     def __getitem__(self, key):
556         try:
--> 557             result = self.index.get_value(self, key)
558
559             if not np.isscalar(result):

/usr/local/lib/python3.5/site-packages/pandas/core/index.py in get_value(self, series, key)
1788
1789         try:
-> 1790             return self._engine.get_value(s, k)
1791         except KeyError as e1:
1792             if len(self) > 0 and self.inferred_type in ['integer','boolean']:

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3204)()

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:2903)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3843)()

pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6525)()

pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6463)()

KeyError: 0

# Fixed code
def set_table_attributes(self, attributes):
    """
    Set the table attributes. These are the items
    that show up in the opening ``<table>`` tag in addition
    to to automatic (by default) id.

    .. versionadded:: 0.17.1

    Parameters
    ----------
    precision: int

    Returns
    -------
    self : Styler
    """
    self.table_attributes = attributes
    return self
# Buggy code
def use(self, styles):
    """
    Set the styles on the current Styler, possibly using styles
    from ``Styler.export``.

    .. versionadded:: 0.17.1

    Parameters
    ----------
    styles: list
        list of style functions

    Returns
    -------
    self

    See Also
    --------
    Styler.export
    """
    self._todo.extend(styles)
    return self


# Traceback (KeyError)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.5/site-packages/IPython/core/formatters.py in __call__(self, obj)
339             method = _safe_get_formatter_method(obj, self.print_method)
340             if method is not None:
--> 341                 return method()
342             return None
343         else:

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in _repr_html_(self)
158         Hooks into Jupyter notebook rich display system.
159         '''
--> 160         return self.render()
161
162     def _translate(self):

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in render(self)
259         """
260         self._compute()
--> 261         d = self._translate()
262         # filter out empty styles, every cell will have a class
263         # but the list of props may just be [['', '']].

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in _translate(self)
220                 cs = [DATA_CLASS, "row%s" % r, "col%s" % c]
221                 cs.extend(cell_context.get("data", {}).get(r, {}).get(c, []))
--> 222                 row_es.append({"type": "td", "value": self.data.iloc[r][c],
223                                "class": " ".join(cs), "id": "_".join(cs[1:])})
224                 props = []

/usr/local/lib/python3.5/site-packages/pandas/core/series.py in __getitem__(self, key)
555     def __getitem__(self, key):
556         try:
--> 557             result = self.index.get_value(self, key)
558
559             if not np.isscalar(result):

/usr/local/lib/python3.5/site-packages/pandas/core/index.py in get_value(self, series, key)
1788
1789         try:
-> 1790             return self._engine.get_value(s, k)
1791         except KeyError as e1:
1792             if len(self) > 0 and self.inferred_type in ['integer','boolean']:

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3204)()

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:2903)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3843)()

pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6525)()

pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6463)()

KeyError: 0

# Fixed code
def use(self, styles):
    """
    Set the styles on the current Styler, possibly using styles
    from ``Styler.export``.

    .. versionadded:: 0.17.1

    Parameters
    ----------
    styles: list
        list of style functions

    Returns
    -------
    self : Styler

    See Also
    --------
    Styler.export
    """
    self._todo.extend(styles)
    return self
# Buggy code
def set_uuid(self, uuid):
    """
    Set the uuid for a Styler.

    .. versionadded:: 0.17.1

    Parameters
    ----------
    uuid: str

    Returns
    -------
    self
    """
    self.uuid = uuid
    return self


# Traceback (KeyError)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.5/site-packages/IPython/core/formatters.py in __call__(self, obj)
339             method = _safe_get_formatter_method(obj, self.print_method)
340             if method is not None:
--> 341                 return method()
342             return None
343         else:

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in _repr_html_(self)
158         Hooks into Jupyter notebook rich display system.
159         '''
--> 160         return self.render()
161
162     def _translate(self):

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in render(self)
259         """
260         self._compute()
--> 261         d = self._translate()
262         # filter out empty styles, every cell will have a class
263         # but the list of props may just be [['', '']].

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in _translate(self)
220                 cs = [DATA_CLASS, "row%s" % r, "col%s" % c]
221                 cs.extend(cell_context.get("data", {}).get(r, {}).get(c, []))
--> 222                 row_es.append({"type": "td", "value": self.data.iloc[r][c],
223                                "class": " ".join(cs), "id": "_".join(cs[1:])})
224                 props = []

/usr/local/lib/python3.5/site-packages/pandas/core/series.py in __getitem__(self, key)
555     def __getitem__(self, key):
556         try:
--> 557             result = self.index.get_value(self, key)
558
559             if not np.isscalar(result):

/usr/local/lib/python3.5/site-packages/pandas/core/index.py in get_value(self, series, key)
1788
1789         try:
-> 1790             return self._engine.get_value(s, k)
1791         except KeyError as e1:
1792             if len(self) > 0 and self.inferred_type in ['integer','boolean']:

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3204)()

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:2903)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3843)()

pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6525)()

pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6463)()

KeyError: 0

# Fixed code
def set_uuid(self, uuid):
    """
    Set the uuid for a Styler.

    .. versionadded:: 0.17.1

    Parameters
    ----------
    uuid: str

    Returns
    -------
    self : Styler
    """
    self.uuid = uuid
    return self
# Buggy code
def set_caption(self, caption):
    """
    Se the caption on a Styler

    .. versionadded:: 0.17.1

    Parameters
    ----------
    caption: str

    Returns
    -------
    self
    """
    self.caption = caption
    return self


# Traceback (KeyError)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.5/site-packages/IPython/core/formatters.py in __call__(self, obj)
339             method = _safe_get_formatter_method(obj, self.print_method)
340             if method is not None:
--> 341                 return method()
342             return None
343         else:

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in _repr_html_(self)
158         Hooks into Jupyter notebook rich display system.
159         '''
--> 160         return self.render()
161
162     def _translate(self):

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in render(self)
259         """
260         self._compute()
--> 261         d = self._translate()
262         # filter out empty styles, every cell will have a class
263         # but the list of props may just be [['', '']].

/usr/local/lib/python3.5/site-packages/pandas/core/style.py in _translate(self)
220                 cs = [DATA_CLASS, "row%s" % r, "col%s" % c]
221                 cs.extend(cell_context.get("data", {}).get(r, {}).get(c, []))
--> 222                 row_es.append({"type": "td", "value": self.data.iloc[r][c],
223                                "class": " ".join(cs), "id": "_".join(cs[1:])})
224                 props = []

/usr/local/lib/python3.5/site-packages/pandas/core/series.py in __getitem__(self, key)
555     def __getitem__(self, key):
556         try:
--> 557             result = self.index.get_value(self, key)
558
559             if not np.isscalar(result):

/usr/local/lib/python3.5/site-packages/pandas/core/index.py in get_value(self, series, key)
1788
1789         try:
-> 1790             return self._engine.get_value(s, k)
1791         except KeyError as e1:
1792             if len(self) > 0 and self.inferred_type in ['integer','boolean']:

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3204)()

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:2903)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3843)()

pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6525)()

pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6463)()

KeyError: 0

# Fixed code
def set_caption(self, caption):
    """
    Se the caption on a Styler

    .. versionadded:: 0.17.1

    Parameters
    ----------
    caption: str

    Returns
    -------
    self : Styler
    """
    self.caption = caption
    return self