gurudesh/pytracebugs huggingface dataset:
# Buggy code
def f(x):
    if not isinstance(x, string_types):
        x = str(x)
    return like in x


# Traceback (UnicodeEncodeError)
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
<ipython-input-10-9de5a19c260e> in <module>()
----> 1 df.filter(regex=u'a')

C:\Users\...\AppData\Local\Continuum\32bit\Anaconda\envs\test\lib\site-packages\pandas\core\generic.pyc in filter(self, items, like, regex, axis)
2013             matcher = re.compile(regex)
2014             return self.select(lambda x: matcher.search(str(x)) is not None,
-> 2015                                axis=axis_name)
2016         else:
2017             raise TypeError('Must pass either `items`, `like`, or `regex`')

C:\Users\...\AppData\Local\Continuum\32bit\Anaconda\envs\test\lib\site-packages\pandas\core\generic.pyc in select(self, crit, axis)
1545         if len(axis_values) > 0:
1546             new_axis = axis_values[
-> 1547                 np.asarray([bool(crit(label)) for label in axis_values])]
1548         else:
1549             new_axis = axis_values

C:\Users\...\AppData\Local\Continuum\32bit\Anaconda\envs\test\lib\site-packages\pandas\core\generic.pyc in <lambda>(x)
2012         elif regex:
2013             matcher = re.compile(regex)
-> 2014             return self.select(lambda x: matcher.search(str(x)) is not None,
2015                                axis=axis_name)
2016         else:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 0: ordinal not in range(128)

# Fixed code
def f(x):
    return matcher.search(to_str(x)) is not None
# Buggy code
def count(self):
    """Compute count of group, excluding missing values"""
    from functools import partial
    from pandas.core.dtypes.missing import _isna_ndarraylike as isna

    data, _ = self._get_data_to_aggregate()
    ids, _, ngroups = self.grouper.group_info
    mask = ids != -1

    val = ((mask & ~isna(blk.get_values())) for blk in data.blocks)
    loc = (blk.mgr_locs for blk in data.blocks)

    counter = partial(count_level_2d, labels=ids, max_bin=ngroups, axis=1)
    blk = map(make_block, map(counter, val), loc)

    return self._wrap_agged_blocks(data.items, list(blk))


# Traceback (ValueError)
ValueError                                Traceback (most recent call last)
<ipython-input-5-3119045de5b1> in <module>()
2 df = pd.DataFrame({'x': ['a', 'a', 'b'],
3                    'y': [pd.Timestamp('2016-05-07 20:09:25+00:00'), pd.Timestamp('2016-05-07 20:09:29+00:00'), pd.Timestamp('2016-05-07 20:09:29+00:00')]})
----> 4 print df.groupby('x').count()

/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.pyc in count(self)
3754         blk = map(make_block, map(counter, val), loc)
3755
-> 3756         return self._wrap_agged_blocks(data.items, list(blk))
3757
3758

pandas/lib.pyx in pandas.lib.count_level_2d (pandas/lib.c:23068)()

ValueError: Buffer has wrong number of dimensions (expected 2, got 1)

# Fixed code
def count(self):
    """Compute count of group, excluding missing values"""
    from functools import partial
    from pandas.core.dtypes.missing import _isna_ndarraylike as isna

    data, _ = self._get_data_to_aggregate()
    ids, _, ngroups = self.grouper.group_info
    mask = ids != -1

    val = ((mask & ~isna(np.atleast_2d(blk.get_values()))) for blk in data.blocks)
    loc = (blk.mgr_locs for blk in data.blocks)

    counter = partial(count_level_2d, labels=ids, max_bin=ngroups, axis=1)
    blk = map(make_block, map(counter, val), loc)

    return self._wrap_agged_blocks(data.items, list(blk))
# Buggy code
def _add_datelike(self, other):
    # adding a timedeltaindex to a datetimelike
    from pandas import Timestamp, DatetimeIndex

    if other is NaT:
        result = self._nat_new(box=False)
    else:
        other = Timestamp(other)
        i8 = self.asi8
        result = checked_add_with_arr(i8, other.value)
        result = self._maybe_mask_results(result, fill_value=iNaT)
    return DatetimeIndex(result, name=self.name, copy=False)


# Traceback (OverflowError)
In [9]: pd.to_timedelta(['24658 days 11:15:00', 'NaT']) + pd.Timestamp('1950-01-01')
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
<ipython-input-9-cc287bf4c401> in <module>()
----> 1 pd.to_timedelta(['24658 days 11:15:00', 'NaT']) + pd.Timestamp('1950-01-01')

~/conda/envs/xarray-py36/lib/python3.6/site-packages/pandas/core/indexes/datetimelike.py in __add__(self, other)
658                 return self.shift(other)
659             elif isinstance(other, (Timestamp, datetime)):
--> 660                 return self._add_datelike(other)
661             else:  # pragma: no cover
662                 return NotImplemented

~/conda/envs/xarray-py36/lib/python3.6/site-packages/pandas/core/indexes/timedeltas.py in _add_datelike(self, other)
354             other = Timestamp(other)
355             i8 = self.asi8
--> 356             result = checked_add_with_arr(i8, other.value)
357             result = self._maybe_mask_results(result, fill_value=iNaT)
358         return DatetimeIndex(result, name=self.name, copy=False)

~/conda/envs/xarray-py36/lib/python3.6/site-packages/pandas/core/algorithms.py in checked_add_with_arr(arr, b, arr_mask, b_mask)
889
890     if to_raise:
--> 891         raise OverflowError("Overflow in int64 addition")
892     return arr + b
893

OverflowError: Overflow in int64 addition

# Fixed code
def _add_datelike(self, other):
    # adding a timedeltaindex to a datetimelike
    from pandas import Timestamp, DatetimeIndex

    if other is NaT:
        result = self._nat_new(box=False)
    else:
        other = Timestamp(other)
        i8 = self.asi8
        result = checked_add_with_arr(i8, other.value, arr_mask=self._isnan)
        result = self._maybe_mask_results(result, fill_value=iNaT)
    return DatetimeIndex(result, name=self.name, copy=False)
# Buggy code
def first_valid_index(self):
    """
    Return label for first non-NA/null value
    """
    if len(self) == 0:
        return None

    return self.index[self.count(1) > 0][0]


# Traceback (IndexError)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
df.first_valid_index()
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 3859, in first_valid_index
return self.index[self.count(1) > 0][0]
File "C:\Python27\lib\site-packages\pandas\indexes\base.py", line 1423, in __getitem__
return getitem(key)
IndexError: index 0 is out of bounds for axis 0 with size 0

# Fixed code
def first_valid_index(self):
    if len(self) == 0:
        return None

    valid_indices = self._get_valid_indices()
    return valid_indices[0] if len(valid_indices) else None
# Buggy code
def last_valid_index(self):
    """
    Return label for last non-NA/null value
    """
    if len(self) == 0:
        return None

    return self.index[self.count(1) > 0][-1]


# Traceback (IndexError)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
df.first_valid_index()
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 3859, in first_valid_index
return self.index[self.count(1) > 0][0]
File "C:\Python27\lib\site-packages\pandas\indexes\base.py", line 1423, in __getitem__
return getitem(key)
IndexError: index 0 is out of bounds for axis 0 with size 0

# Fixed code
def last_valid_index(self):
    if len(self) == 0:
        return None

    valid_indices = self._get_valid_indices()
    return valid_indices[-1] if len(valid_indices) else None
# Buggy code
def first_valid_index(self):
    """
    Return label for first non-NA/null value
    """
    if len(self) == 0:
        return None

    mask = isna(self._values)
    i = mask.argmin()
    if mask[i]:
        return None
    else:
        return self.index[i]


# Traceback (IndexError)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
df.first_valid_index()
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 3859, in first_valid_index
return self.index[self.count(1) > 0][0]
File "C:\Python27\lib\site-packages\pandas\indexes\base.py", line 1423, in __getitem__
return getitem(key)
IndexError: index 0 is out of bounds for axis 0 with size 0

# Fixed code
def first_valid_index(self):
    if len(self) == 0:
        return None

    mask = isna(self._values)
    i = mask.argmin()
    if mask[i]:
        return None
    else:
        return self.index[i]
# Buggy code
def last_valid_index(self):
    """
    Return label for last non-NA/null value
    """
    if len(self) == 0:
        return None

    mask = isna(self._values[::-1])
    i = mask.argmin()
    if mask[i]:
        return None
    else:
        return self.index[len(self) - i - 1]


# Traceback (IndexError)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
df.first_valid_index()
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 3859, in first_valid_index
return self.index[self.count(1) > 0][0]
File "C:\Python27\lib\site-packages\pandas\indexes\base.py", line 1423, in __getitem__
return getitem(key)
IndexError: index 0 is out of bounds for axis 0 with size 0

# Fixed code
def last_valid_index(self):
    if len(self) == 0:
        return None

    mask = isna(self._values[::-1])
    i = mask.argmin()
    if mask[i]:
        return None
    else:
        return self.index[len(self) - i - 1]
# Buggy code
def setup(self):
    self.ts = Timestamp("2017-08-25 08:16:14")


# Traceback (pytz.exceptions.NonExistentTimeError)
pytz.timezone('CET').localize(pd.Timestamp(datetime(2016, 3, 27, 1)), is_dst=None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/localhome/stefan/emsconda/envs/popeye/lib/python3.6/site-packages/pytz/tzinfo.py", line 327, in localize
raise NonExistentTimeError(dt)
pytz.exceptions.NonExistentTimeError: 2016-03-27 01:00:00

# Fixed code
def setup(self):
    self.ts = Timestamp("2017-08-25 08:16:14")
    self.ts_tz = Timestamp("2017-08-25 08:16:14", tz="US/Eastern")

    dt = datetime.datetime(2016, 3, 27, 1)
    self.tzinfo = pytz.timezone("CET").localize(dt, is_dst=False).tzinfo
    self.ts2 = Timestamp(dt)
# Buggy code
def _try_aggregate_string_function(self, arg, *args, **kwargs):
    """
    if arg is a string, then try to operate on it:
    - try to find a function on ourselves
    - try to find a numpy function
    - raise

    """
    assert isinstance(arg, compat.string_types)

    f = getattr(self, arg, None)
    if f is not None:
        return f(*args, **kwargs)

    f = getattr(np, arg, None)
    if f is not None:
        return f(self, *args, **kwargs)

    raise ValueError("{} is an unknown string function".format(arg))


# Traceback (RecursionError)
pd.DataFrame({'g': [0, 0, 1], 'v': [1, 2, None]}).agg({'v': 'size'})
---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
<ipython-input-15-5fc5c38cdb5f> in <module>()
----> 1 pd.DataFrame({'g': [0, 0, 1], 'v': [1, 2, 3]}).agg({'v': 'size'})

/Volumes/Home/venvs/py3-data-science/lib/python3.5/site-packages/pandas/core/frame.py in aggregate(self, func, axis, *args, **kwargs)
4250                 pass
4251         if result is None:
-> 4252             return self.apply(func, axis=axis, args=args, **kwargs)
4253         return result
4254

/Volumes/Home/venvs/py3-data-science/lib/python3.5/site-packages/pandas/core/frame.py in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
4322         # dispatch to agg
4323         if axis == 0 and isinstance(func, (list, dict)):
-> 4324             return self.aggregate(func, axis=axis, *args, **kwds)
4325
4326         if len(self.columns) == 0 and len(self.index) == 0:

... last 2 frames repeated, from the frame below ...

/Volumes/Home/venvs/py3-data-science/lib/python3.5/site-packages/pandas/core/frame.py in aggregate(self, func, axis, *args, **kwargs)
4250                 pass
4251         if result is None:
-> 4252             return self.apply(func, axis=axis, args=args, **kwargs)
4253         return result
4254

RecursionError: maximum recursion depth exceeded

# Fixed code
def _try_aggregate_string_function(self, arg, *args, **kwargs):
    """
    if arg is a string, then try to operate on it:
    - try to find a function (or attribute) on ourselves
    - try to find a numpy function
    - raise

    """
    assert isinstance(arg, compat.string_types)

    f = getattr(self, arg, None)
    if f is not None:
        if callable(f):
            return f(*args, **kwargs)

        # people may try to aggregate on a non-callable attribute
        # but don't let them think they can pass args to it
        assert len(args) == 0
        assert len([kwarg for kwarg in kwargs if kwarg not in ["axis", "_level"]]) == 0
        return f

    f = getattr(np, arg, None)
    if f is not None:
        return f(self, *args, **kwargs)

    raise ValueError("{} is an unknown string function".format(arg))
# Buggy code
def to_clipboard(self, excel=None, sep=None, **kwargs):
    """
    Attempt to write text representation of object to the system clipboard
    This can be pasted into Excel, for example.

    Parameters
    ----------
    excel : boolean, defaults to True
            if True, use the provided separator, writing in a csv
            format for allowing easy pasting into excel.
            if False, write a string representation of the object
            to the clipboard
    sep : optional, defaults to tab
    other keywords are passed to to_csv

    Notes
    -----
    Requirements for your platform
      - Linux: xclip, or xsel (with gtk or PyQt4 modules)
      - Windows: none
      - OS X: none
    """
    from pandas.io.clipboard import clipboard

    clipboard.to_clipboard(self, excel=excel, sep=sep, **kwargs)


# Traceback (ImportError)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/smiel/.venvs/foo/local/lib/python2.7/site-packages/pandas/__init__.py", line 58, in <module>
from pandas.io.api import *
File "/home/smiel/.venvs/foo/local/lib/python2.7/site-packages/pandas/io/api.py", line 8, in <module>
from pandas.io.clipboard.clipboard import read_clipboard
File "/home/smiel/.venvs/foo/local/lib/python2.7/site-packages/pandas/io/clipboard/__init__.py", line 103, in <module>
copy, paste = determine_clipboard()
File "/home/smiel/.venvs/foo/local/lib/python2.7/site-packages/pandas/io/clipboard/__init__.py", line 76, in determine_clipboard
return init_qt_clipboard()
File "/home/smiel/.venvs/foo/local/lib/python2.7/site-packages/pandas/io/clipboard/clipboards.py", line 49, in init_qt_clipboard
from PyQt4.QtGui import QApplication
ImportError: No module named sip

# Fixed code
def to_clipboard(self, excel=None, sep=None, **kwargs):
    """
    Attempt to write text representation of object to the system clipboard
    This can be pasted into Excel, for example.

    Parameters
    ----------
    excel : boolean, defaults to True
            if True, use the provided separator, writing in a csv
            format for allowing easy pasting into excel.
            if False, write a string representation of the object
            to the clipboard
    sep : optional, defaults to tab
    other keywords are passed to to_csv

    Notes
    -----
    Requirements for your platform
      - Linux: xclip, or xsel (with gtk or PyQt4 modules)
      - Windows: none
      - OS X: none
    """
    from pandas.io import clipboards

    clipboards.to_clipboard(self, excel=excel, sep=sep, **kwargs)
# Buggy code
def _get_labels_for_sorting(self):
    """
    we categorizing our labels by using the
    available catgories (all, not just observed)
    excluding any missing ones (-1); this is in preparation
    for sorting, where we need to disambiguate that -1 is not
    a valid valid
    """
    from pandas.core.categorical import Categorical

    return [
        Categorical.from_codes(
            label, np.arange(np.array(label).max() + 1, dtype=label.dtype), ordered=True
        )
        for label in self.labels
    ]


# Traceback (ValueError)
import pandas as pd
df = pd.DataFrame(index=[0], columns=['a', 'b', 'c'])
df.groupby('a').nth(10)

Empty DataFrame
Columns: [b, c]
Index: []

df.groupby(['a', 'b']).nth(10)

Traceback (most recent call last):
File "<ipython-input-3-ae8299c3984e>", line 1, in <module>
df.groupby(['a', 'b']).nth(10)
File "~/anaconda3/lib/python3.5/site-packages/pandas/core/groupby.py", line 1390, in nth
return out.sort_index() if self.sort else out
File "~/anaconda3/lib/python3.5/site-packages/pandas/core/frame.py", line 3344, in sort_index
indexer = lexsort_indexer(labels._get_labels_for_sorting(),
File "~/anaconda3/lib/python3.5/site-packages/pandas/core/indexes/multi.py", line 1652, in _get_labels_for_sorting
for label in self.labels]
File "~/anaconda3/lib/python3.5/site-packages/pandas/core/indexes/multi.py", line 1652, in <listcomp>
for label in self.labels]
File "~/anaconda3/lib/python3.5/site-packages/numpy/core/_methods.py", line 26, in _amax
return umr_maximum(a, axis, None, out, keepdims)
ValueError: zero-size array to reduction operation maximum which has no identity

# Fixed code
def _get_labels_for_sorting(self):
    """
    we categorizing our labels by using the
    available catgories (all, not just observed)
    excluding any missing ones (-1); this is in preparation
    for sorting, where we need to disambiguate that -1 is not
    a valid valid
    """
    from pandas.core.categorical import Categorical

    def cats(label):
        return np.arange(
            np.array(label).max() + 1 if len(label) else 0, dtype=label.dtype
        )

    return [
        Categorical.from_codes(label, cats(label), ordered=True)
        for label in self.labels
    ]
# Buggy code
def _aggregate_series_fast(self, obj, func):
    func = self._is_builtin_func(func)

    if obj.index._has_complex_internals:
        raise TypeError("Incompatible index for Cython grouper")

    group_index, _, ngroups = self.group_info

    # avoids object / Series creation overhead
    dummy = obj._get_values(slice(None, 0)).to_dense()
    indexer = get_group_index_sorter(group_index, ngroups)
    obj = obj.take(indexer, convert=False)
    group_index = algorithms.take_nd(group_index, indexer, allow_fill=False)
    grouper = lib.SeriesGrouper(obj, func, group_index, ngroups, dummy)
    result, counts = grouper.get_result()
    return result, counts


# Traceback (KeyError)
df2.sort_index().loc['b':'d']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)

# a lot of lines omitted here.

/Users/yimengzh/miniconda/envs/cafferc3/lib/python2.7/site-packages/pandas/indexes/multi.py in _partial_tup_index(self, tup, side)
1488             raise KeyError('Key length (%d) was greater than MultiIndex'
1489                            ' lexsort depth (%d)' %
-> 1490                            (len(tup), self.lexsort_depth))
1491
1492         n = len(tup)

KeyError: 'Key length (1) was greater than MultiIndex lexsort depth (0)'

# Fixed code
def _aggregate_series_fast(self, obj, func):
    func = self._is_builtin_func(func)

    if obj.index._has_complex_internals:
        raise TypeError("Incompatible index for Cython grouper")

    group_index, _, ngroups = self.group_info

    # avoids object / Series creation overhead
    dummy = obj._get_values(slice(None, 0)).to_dense()
    indexer = get_group_index_sorter(group_index, ngroups)
    obj = obj.take(indexer, convert=False).to_dense()
    group_index = algorithms.take_nd(group_index, indexer, allow_fill=False)
    grouper = lib.SeriesGrouper(obj, func, group_index, ngroups, dummy)
    result, counts = grouper.get_result()
    return result, counts
# Buggy code
def _partial_tup_index(self, tup, side="left"):
    if len(tup) > self.lexsort_depth:
        raise KeyError(
            "Key length (%d) was greater than MultiIndex"
            " lexsort depth (%d)" % (len(tup), self.lexsort_depth)
        )

    n = len(tup)
    start, end = 0, len(self)
    zipped = zip(tup, self.levels, self.labels)
    for k, (lab, lev, labs) in enumerate(zipped):
        section = labs[start:end]

        if lab not in lev:
            if not lev.is_type_compatible(lib.infer_dtype([lab])):
                raise TypeError("Level type mismatch: %s" % lab)

            # short circuit
            loc = lev.searchsorted(lab, side=side)
            if side == "right" and loc >= 0:
                loc -= 1
            return start + section.searchsorted(loc, side=side)

        idx = lev.get_loc(lab)
        if k < n - 1:
            end = start + section.searchsorted(idx, side="right")
            start = start + section.searchsorted(idx, side="left")
        else:
            return start + section.searchsorted(idx, side=side)


# Traceback (KeyError)
df2.sort_index().loc['b':'d']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)

# a lot of lines omitted here.

/Users/yimengzh/miniconda/envs/cafferc3/lib/python2.7/site-packages/pandas/indexes/multi.py in _partial_tup_index(self, tup, side)
1488             raise KeyError('Key length (%d) was greater than MultiIndex'
1489                            ' lexsort depth (%d)' %
-> 1490                            (len(tup), self.lexsort_depth))
1491
1492         n = len(tup)

KeyError: 'Key length (1) was greater than MultiIndex lexsort depth (0)'

# Fixed code
def _partial_tup_index(self, tup, side="left"):
    if len(tup) > self.lexsort_depth:
        raise UnsortedIndexError(
            "Key length (%d) was greater than MultiIndex"
            " lexsort depth (%d)" % (len(tup), self.lexsort_depth)
        )

    n = len(tup)
    start, end = 0, len(self)
    zipped = zip(tup, self.levels, self.labels)
    for k, (lab, lev, labs) in enumerate(zipped):
        section = labs[start:end]

        if lab not in lev:
            if not lev.is_type_compatible(lib.infer_dtype([lab])):
                raise TypeError("Level type mismatch: %s" % lab)

            # short circuit
            loc = lev.searchsorted(lab, side=side)
            if side == "right" and loc >= 0:
                loc -= 1
            return start + section.searchsorted(loc, side=side)

        idx = lev.get_loc(lab)
        if k < n - 1:
            end = start + section.searchsorted(idx, side="right")
            start = start + section.searchsorted(idx, side="left")
        else:
            return start + section.searchsorted(idx, side=side)
# Buggy code
def cummin(self, axis=0, **kwargs):
    """Cumulative min for each group"""
    if axis != 0:
        return self.apply(lambda x: np.minimum.accumulate(x, axis))

    return self._cython_transform("cummin", **kwargs)


# Traceback (DataError)
import pandas as pd
x = pd.DataFrame(dict(a=[1], b=pd.to_datetime(['2001'])))
x.groupby('a').b.cummax()

Traceback (most recent call last):
File "<ipython-input-9-316257648d5f>", line 1, in <module>
x.groupby('a').b.cummax()
File "~/anaconda3/lib/python3.5/site-packages/pandas/core/groupby.py", line 1454, in cummax
return self._cython_transform('cummax', **kwargs)
File "~/anaconda3/lib/python3.5/site-packages/pandas/core/groupby.py", line 806, in _cython_transform
raise DataError('No numeric types to aggregate')
DataError: No numeric types to aggregate

# Fixed code
def cummin(self, axis=0, **kwargs):
    """Cumulative min for each group"""
    if axis != 0:
        return self.apply(lambda x: np.minimum.accumulate(x, axis))

    return self._cython_transform("cummin", numeric_only=False)
# Buggy code
def cummax(self, axis=0, **kwargs):
    """Cumulative max for each group"""
    if axis != 0:
        return self.apply(lambda x: np.maximum.accumulate(x, axis))

    return self._cython_transform("cummax", **kwargs)


# Traceback (DataError)
import pandas as pd
x = pd.DataFrame(dict(a=[1], b=pd.to_datetime(['2001'])))
x.groupby('a').b.cummax()

Traceback (most recent call last):
File "<ipython-input-9-316257648d5f>", line 1, in <module>
x.groupby('a').b.cummax()
File "~/anaconda3/lib/python3.5/site-packages/pandas/core/groupby.py", line 1454, in cummax
return self._cython_transform('cummax', **kwargs)
File "~/anaconda3/lib/python3.5/site-packages/pandas/core/groupby.py", line 806, in _cython_transform
raise DataError('No numeric types to aggregate')
DataError: No numeric types to aggregate

# Fixed code
def cummax(self, axis=0, **kwargs):
    """Cumulative max for each group"""
    if axis != 0:
        return self.apply(lambda x: np.maximum.accumulate(x, axis))

    return self._cython_transform("cummax", numeric_only=False)
# Buggy code
def _aggregate(self, result, counts, values, comp_ids, agg_func, is_numeric):
    if values.ndim > 3:
        # punting for now
        raise NotImplementedError("number of dimensions is currently limited to 3")
    elif values.ndim > 2:
        for i, chunk in enumerate(values.transpose(2, 0, 1)):
            chunk = chunk.squeeze()
            agg_func(result[:, :, i], counts, chunk, comp_ids)
    else:
        agg_func(result, counts, values, comp_ids)

    return result


# Traceback (AssertionError)
(pandas) bash-3.2$ nosetests pandas/tests/groupby/test_groupby.py  -s -m cummin_cummax
F
======================================================================
FAIL: test_cummin_cummax (test_groupby.TestGroupBy)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/jreback/pandas/pandas/tests/groupby/test_groupby.py", line 5798, in test_cummin_cummax
tm.assert_frame_equal(result, expected)
File "/Users/jreback/pandas/pandas/util/testing.py", line 1313, in assert_frame_equal
obj='DataFrame.iloc[:, {0}]'.format(i))
File "/Users/jreback/pandas/pandas/util/testing.py", line 1154, in assert_series_equal
assert_attr_equal('dtype', left, right)
File "/Users/jreback/pandas/pandas/util/testing.py", line 878, in assert_attr_equal
left_attr, right_attr)
File "/Users/jreback/pandas/pandas/util/testing.py", line 1018, in raise_assert_detail
raise AssertionError(msg)
AssertionError: Attributes are different

Attribute "dtype" are different
[left]:  float64
[right]: int64

----------------------------------------------------------------------
Ran 1 test in 0.046s

FAILED (failures=1)

# Fixed code
def _aggregate(
    self, result, counts, values, comp_ids, agg_func, is_numeric, is_datetimelike
):
    if values.ndim > 3:
        # punting for now
        raise NotImplementedError("number of dimensions is currently limited to 3")
    elif values.ndim > 2:
        for i, chunk in enumerate(values.transpose(2, 0, 1)):
            chunk = chunk.squeeze()
            agg_func(result[:, :, i], counts, chunk, comp_ids)
    else:
        agg_func(result, counts, values, comp_ids)

    return result
# Buggy code
def _transform(self, result, accum, values, comp_ids, transform_func, is_numeric):
    comp_ids, _, ngroups = self.group_info
    if values.ndim > 3:
        # punting for now
        raise NotImplementedError("number of dimensions is currently limited to 3")
    elif values.ndim > 2:
        for i, chunk in enumerate(values.transpose(2, 0, 1)):
            chunk = chunk.squeeze()
            transform_func(result[:, :, i], values, comp_ids, accum)
    else:
        transform_func(result, values, comp_ids, accum)

    return result


# Traceback (AssertionError)
(pandas) bash-3.2$ nosetests pandas/tests/groupby/test_groupby.py  -s -m cummin_cummax
F
======================================================================
FAIL: test_cummin_cummax (test_groupby.TestGroupBy)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/jreback/pandas/pandas/tests/groupby/test_groupby.py", line 5798, in test_cummin_cummax
tm.assert_frame_equal(result, expected)
File "/Users/jreback/pandas/pandas/util/testing.py", line 1313, in assert_frame_equal
obj='DataFrame.iloc[:, {0}]'.format(i))
File "/Users/jreback/pandas/pandas/util/testing.py", line 1154, in assert_series_equal
assert_attr_equal('dtype', left, right)
File "/Users/jreback/pandas/pandas/util/testing.py", line 878, in assert_attr_equal
left_attr, right_attr)
File "/Users/jreback/pandas/pandas/util/testing.py", line 1018, in raise_assert_detail
raise AssertionError(msg)
AssertionError: Attributes are different

Attribute "dtype" are different
[left]:  float64
[right]: int64

----------------------------------------------------------------------
Ran 1 test in 0.046s

FAILED (failures=1)

# Fixed code
def _transform(
    self, result, values, comp_ids, transform_func, is_numeric, is_datetimelike
):
    comp_ids, _, ngroups = self.group_info
    if values.ndim > 3:
        # punting for now
        raise NotImplementedError("number of dimensions is currently limited to 3")
    elif values.ndim > 2:
        for i, chunk in enumerate(values.transpose(2, 0, 1)):
            chunk = chunk.squeeze()
            transform_func(result[:, :, i], values, comp_ids, is_datetimelike)
    else:
        transform_func(result, values, comp_ids, is_datetimelike)

    return result
# Buggy code
def _gotitem(self, key, ndim, subset=None):
    """
    sub-classes to define
    return a sliced object

    Parameters
    ----------
    key : string / list of selections
    ndim : 1,2
        requested ndim of result
    subset : object, default None
        subset to act on
    """

    # create a new object to prevent aliasing
    if subset is None:
        subset = self.obj
    self = self._shallow_copy(subset)
    self._reset_cache()
    if subset.ndim == 2:
        if isscalar(key) and key in subset or is_list_like(key):
            self._selection = key
    return self


# Traceback (ValueError)
In [7]: dates_df.groupby('name').rolling('180D', on='date')['amount'].sum()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-8896cb99a66a> in <module>()
----> 1 dates_df.groupby('name').rolling('180D', on='date')['amount'].sum()

/Users/jreback/pandas/pandas/core/groupby.py in rolling(self, *args, **kwargs)
1148         """
1149         from pandas.core.window import RollingGroupby
-> 1150         return RollingGroupby(self, *args, **kwargs)
1151
1152     @Substitution(name='groupby')

/Users/jreback/pandas/pandas/core/window.py in __init__(self, obj, *args, **kwargs)
635         self._groupby.mutated = True
636         self._groupby.grouper.mutated = True
--> 637         super(GroupByMixin, self).__init__(obj, *args, **kwargs)
638
639     count = GroupByMixin._dispatch('count')

/Users/jreback/pandas/pandas/core/window.py in __init__(self, obj, window, min_periods, freq, center, win_type, axis, on, **kwargs)
76         self.win_type = win_type
77         self.axis = obj._get_axis_number(axis) if axis is not None else None
---> 78         self.validate()
79
80     @property

/Users/jreback/pandas/pandas/core/window.py in validate(self)
1030                 formatted = self.on or 'index'
1031                 raise ValueError("{0} must be "
-> 1032                                  "monotonic".format(formatted))
1033
1034             from pandas.tseries.frequencies import to_offset

ValueError: date must be monotonic

# Fixed code
def _gotitem(self, key, ndim, subset=None):
    # we are setting the index on the actual object
    # here so our index is carried thru to the selected obj
    # when we do the splitting for the groupby
    if self.on is not None:
        self._groupby.obj = self._groupby.obj.set_index(self._on)
        self.on = None
    return super(RollingGroupby, self)._gotitem(key, ndim, subset=subset)
# Buggy code
def read(self, nrows=None):
    if (nrows is None) and (self.chunksize is not None):
        nrows = self.chunksize
    elif nrows is None:
        nrows = self.row_count

    if self._current_row_in_file_index >= self.row_count:
        return None

    nd = (self.column_types == b"d").sum()
    ns = (self.column_types == b"s").sum()

    self._string_chunk = np.empty((ns, nrows), dtype=np.object)
    self._byte_chunk = np.empty((nd, 8 * nrows), dtype=np.uint8)

    self._current_row_in_chunk_index = 0
    p = Parser(self)
    p.read(nrows)

    rslt = self._chunk_to_dataframe()
    if self.index is not None:
        rslt = rslt.set_index(self.index)

    return rslt


# Traceback (AbstractMethodError)
import pandas as pd

f = pd.read_sas('test.sas7bdat', iterator=True)
for chunk in f:
print(f)

---------------------------------------------------------------------------
AbstractMethodError                       Traceback (most recent call last)
<ipython-input-3-bedf2769bffe> in <module>()
----> 1 for chunk in f:
2     print(chunk)

C:\Anaconda3\lib\site-packages\pandas\io\common.py in __next__(self)
99
100     def __next__(self):
--> 101         raise AbstractMethodError(self)
102
103 if not compat.PY3:

AbstractMethodError: This method must be defined in the concrete class of SAS7BDATReader

# Fixed code
def read(self, nrows=None):
    if (nrows is None) and (self.chunksize is not None):
        nrows = self.chunksize
    elif nrows is None:
        nrows = self.row_count

    if self._current_row_in_file_index >= self.row_count:
        return None

    m = self.row_count - self._current_row_in_file_index
    if nrows > m:
        nrows = m

    nd = (self.column_types == b"d").sum()
    ns = (self.column_types == b"s").sum()

    self._string_chunk = np.empty((ns, nrows), dtype=np.object)
    self._byte_chunk = np.empty((nd, 8 * nrows), dtype=np.uint8)

    self._current_row_in_chunk_index = 0
    p = Parser(self)
    p.read(nrows)

    rslt = self._chunk_to_dataframe()
    if self.index is not None:
        rslt = rslt.set_index(self.index)

    return rslt
# Buggy code
def _groupby_indices(values):
    if is_categorical_dtype(values):
        # we have a categorical, so we can do quite a bit
        # bit better than factorizing again
        reverse = dict(enumerate(values.categories))
        codes = values.codes.astype("int64")
        _, counts = _hash.value_count_int64(codes, False)
    else:
        reverse, codes, counts = _algos.group_labels(
            _values_from_object(_ensure_object(values))
        )

    return _algos.groupby_indices(reverse, codes, counts)


# Traceback (KeyError)
0
0
(0, 5]     3.333333
(5, 10]    7.500000
(10, 15]  11.000000
(15, 20]        NaN
(20, 25]  24.500000
(25, 30]        NaN
(30, 35]        NaN
(35, 40]  36.000000
(40, 45]        NaN
(45, 50]        NaN
(50, 55]        NaN
0
0
(0, 5]     3.5
(5, 10]    7.5
(10, 15]  11.0
(15, 20]  18.0
(20, 25]  24.5
(25, 30]  30.5
(30, 35]  30.5
(35, 40]  36.0
(40, 45]  18.0
(45, 50]  18.0
(50, 55]  18.0
0    3.5
dtype: float64
Traceback (most recent call last):

File "<ipython-input-9-0663486889da>", line 1, in <module>
runfile('C:/PythonDir/test04.py', wdir='C:/PythonDir')

File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
execfile(filename, namespace)

File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)

File "C:/PythonDir/test04.py", line 20, in <module>
print g.get_group('(40, 45]').median()

File "C:\Anaconda2\lib\site-packages\pandas\core\groupby.py", line 587, in get_group
raise KeyError(name)

KeyError: '(40, 45]'

# Fixed code
def _groupby_indices(values):
    if is_categorical_dtype(values):
        # we have a categorical, so we can do quite a bit
        # bit better than factorizing again
        reverse = dict(enumerate(values.categories))
        codes = values.codes.astype("int64")

        mask = 0 <= codes
        counts = np.bincount(codes[mask], minlength=values.categories.size)
    else:
        reverse, codes, counts = _algos.group_labels(
            _values_from_object(_ensure_object(values))
        )

    return _algos.groupby_indices(reverse, codes, counts)
# Buggy code
def _is_offset(self, arr_or_obj):
    """check if obj or all elements of list-like is DateOffset"""
    if isinstance(arr_or_obj, pd.DateOffset):
        return True
    elif is_list_like(arr_or_obj):
        return all(isinstance(x, pd.DateOffset) for x in arr_or_obj)
    else:
        return False


# Traceback (TypeError)
Date_Time  Item  Relative_Time
0  2/8/2015  6:00:30     1             20
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/ops.py:477: PerformanceWarning: Adding/subtracting array of DateOffsets to Series not vectorized
"Series not vectorized", PerformanceWarning)
Traceback (most recent call last):
File "bug.py", line 12, in <module>
dtf.Date_Time = dtf.Date_Time + dtf.Relative_Time
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/ops.py", line 641, in wrapper
arr = na_op(lvalues, rvalues)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/ops.py", line 481, in <lambda>
self.na_op = lambda x, y: getattr(x, self.name)(y)
TypeError: ufunc add cannot use operands with types dtype('<M8[ns]') and dtype('O')

# Fixed code
def _is_offset(self, arr_or_obj):
    """check if obj or all elements of list-like is DateOffset"""
    if isinstance(arr_or_obj, pd.DateOffset):
        return True
    elif is_list_like(arr_or_obj) and len(arr_or_obj):
        return all(isinstance(x, pd.DateOffset) for x in arr_or_obj)
    return False
# Buggy code
def _validate_usecols_arg(usecols):
    """
    Check whether or not the 'usecols' parameter
    contains all integers (column selection by index)
    or strings (column by name). Raises a ValueError
    if that is not the case.
    """
    msg = (
        "The elements of 'usecols' must "
        "either be all strings, all unicode, or all integers"
    )

    if usecols is not None:
        usecols_dtype = lib.infer_dtype(usecols)
        if usecols_dtype not in ("empty", "integer", "string", "unicode"):
            raise ValueError(msg)

    return usecols


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-e39439e21b50> in <module>()
17                     iterator = True,   chunksize = chunksize  , engine = "python" )
18
---> 19 print(list(ite)[0])

/home/.usr/py3/lib/pandas-0.18.0rc1+80.g820e110.dirty-py3.3-linux-x86_64.egg/pandas/io/parsers.py in __next__(self)
739
740     def __next__(self):
--> 741         return self.get_chunk()
742
743     def _make_engine(self, engine='c'):

/home/.usr/py3/lib/pandas-0.18.0rc1+80.g820e110.dirty-py3.3-linux-x86_64.egg/pandas/io/parsers.py in get_chunk(self, size)
780         if size is None:
781             size = self.chunksize
--> 782         return self.read(nrows=size)
783
784

/home/.usr/py3/lib/pandas-0.18.0rc1+80.g820e110.dirty-py3.3-linux-x86_64.egg/pandas/io/parsers.py in read(self, nrows)
759                 raise ValueError('skip_footer not supported for iteration')
760
--> 761         ret = self._engine.read(nrows)
762
763         if self.options.get('as_recarray'):

/home/.usr/py3/lib/pandas-0.18.0rc1+80.g820e110.dirty-py3.3-linux-x86_64.egg/pandas/io/parsers.py in read(self, rows)
1617             content = content[1:]
1618
-> 1619         alldata = self._rows_to_cols(content)
1620         data = self._exclude_implicit_index(alldata)
1621

/home/.usr/py3/lib/pandas-0.18.0rc1+80.g820e110.dirty-py3.3-linux-x86_64.egg/pandas/io/parsers.py in _rows_to_cols(self, content)
1997             raise ValueError(msg)
1998
-> 1999         if self.usecols:
2000             if self._implicit_index:
2001                 zipped_content = [

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

# Fixed code
def _validate_usecols_arg(usecols):
    """
    Check whether or not the 'usecols' parameter
    contains all integers (column selection by index)
    or strings (column by name). Raises a ValueError
    if that is not the case.
    """
    msg = (
        "The elements of 'usecols' must "
        "either be all strings, all unicode, or all integers"
    )

    if usecols is not None:
        usecols_dtype = lib.infer_dtype(usecols)
        if usecols_dtype not in ("empty", "integer", "string", "unicode"):
            raise ValueError(msg)

        return set(usecols)
    return usecols
# Buggy code
def _possibly_convert_platform(values):
    """try to do platform conversion, allow ndarray or list here"""

    if isinstance(values, (list, tuple)):
        values = lib.list_to_object_array(values)
    if getattr(values, "dtype", None) == np.object_:
        if hasattr(values, "_values"):
            values = values._values
        values = lib.maybe_convert_objects(values)

    return values


# Traceback (AssertionError)
======================================================================
FAIL: test_alignment_non_pandas (pandas.tests.frame.test_operators.TestDataFrameOperators)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\conda\Documents\pandas3.5\pandas\tests\frame\test_operators.py", line 1203, in test_alignment_non_pandas
Series([1, 2, 3], index=df.index))
File "C:\Users\conda\Documents\pandas3.5\pandas\util\testing.py", line 1157, in assert_series_equal
assert_attr_equal('dtype', left, right)
File "C:\Users\conda\Documents\pandas3.5\pandas\util\testing.py", line 882, in assert_attr_equal
left_attr, right_attr)
File "C:\Users\conda\Documents\pandas3.5\pandas\util\testing.py", line 1021, in raise_assert_detail
raise AssertionError(msg)
AssertionError: Attributes are different

Attribute "dtype" are different
[left]:  int32
[right]: int64None

# Fixed code
def _possibly_convert_platform(values):
    """try to do platform conversion, allow ndarray or list here"""

    if isinstance(values, (list, tuple)):
        values = lib.list_to_object_array(list(values))
    if getattr(values, "dtype", None) == np.object_:
        if hasattr(values, "_values"):
            values = values._values
        values = lib.maybe_convert_objects(values)

    return values
# Buggy code
def get_service(self):
    import httplib2
    from apiclient.discovery import build

    http = httplib2.Http()
    http = self.credentials.authorize(http)
    bigquery_service = build("bigquery", "v2", http=http)

    return bigquery_service


# Traceback (AttributeError)
import apiclient
apiclient.__version__
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-90-662a0f56e34b> in <module>()
1 import apiclient
----> 2 apiclient.__version__

AttributeError: 'module' object has no attribute '__version__'

# Fixed code
def get_service(self):
    import httplib2

    try:
        from googleapiclient.discovery import build
    except:
        from apiclient.discovery import build

    http = httplib2.Http()
    http = self.credentials.authorize(http)
    bigquery_service = build("bigquery", "v2", http=http)

    return bigquery_service
# Buggy code
def verify_schema(self, dataset_id, table_id, schema):
    from apiclient.errors import HttpError

    try:
        return (
            (
                self.service.tables()
                .get(projectId=self.project_id, datasetId=dataset_id, tableId=table_id)
                .execute()["schema"]
            )
            == schema
        )

    except HttpError as ex:
        self.process_http_error(ex)


# Traceback (AttributeError)
import apiclient
apiclient.__version__
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-90-662a0f56e34b> in <module>()
1 import apiclient
----> 2 apiclient.__version__

AttributeError: 'module' object has no attribute '__version__'

# Fixed code
def verify_schema(self, dataset_id, table_id, schema):
    try:
        from googleapiclient.errors import HttpError
    except:
        from apiclient.errors import HttpError

    try:
        return (
            (
                self.service.tables()
                .get(projectId=self.project_id, datasetId=dataset_id, tableId=table_id)
                .execute()["schema"]
            )
            == schema
        )

    except HttpError as ex:
        self.process_http_error(ex)
# Buggy code
def __init__(
    self, project_id, dataset_id, reauth=False, verbose=False, private_key=None
):
    from apiclient.errors import HttpError

    self.http_error = HttpError
    self.dataset_id = dataset_id
    super(_Table, self).__init__(project_id, reauth, verbose, private_key)


# Traceback (AttributeError)
import apiclient
apiclient.__version__
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-90-662a0f56e34b> in <module>()
1 import apiclient
----> 2 apiclient.__version__

AttributeError: 'module' object has no attribute '__version__'

# Fixed code
def __init__(
    self, project_id, dataset_id, reauth=False, verbose=False, private_key=None
):
    try:
        from googleapiclient.errors import HttpError
    except:
        from apiclient.errors import HttpError
    self.http_error = HttpError
    self.dataset_id = dataset_id
    super(_Table, self).__init__(project_id, reauth, verbose, private_key)
# Buggy code
def __init__(self, project_id, reauth=False, verbose=False, private_key=None):
    from apiclient.errors import HttpError

    self.http_error = HttpError
    super(_Dataset, self).__init__(project_id, reauth, verbose, private_key)


# Traceback (AttributeError)
import apiclient
apiclient.__version__
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-90-662a0f56e34b> in <module>()
1 import apiclient
----> 2 apiclient.__version__

AttributeError: 'module' object has no attribute '__version__'

# Fixed code
def __init__(self, project_id, reauth=False, verbose=False, private_key=None):
    try:
        from googleapiclient.errors import HttpError
    except:
        from apiclient.errors import HttpError
    self.http_error = HttpError
    super(_Dataset, self).__init__(project_id, reauth, verbose, private_key)
# Buggy code
def get_resampler_for_grouping(
    groupby, rule, how=None, fill_method=None, limit=None, kind=None, **kwargs
):
    """return our appropriate resampler when grouping as well"""
    tg = TimeGrouper(freq=rule, **kwargs)
    resampler = tg._get_resampler(groupby.obj, kind=kind)
    r = resampler._get_resampler_for_grouping(groupby=groupby)
    return _maybe_process_deprecations(
        r, how=how, fill_method=fill_method, limit=limit, **kwargs
    )


# Traceback (TypeError)
TypeError                                 Traceback (most recent call last)
<ipython-input-53-6e7ac0fde8b3> in <module>()
----> 1 df.groupby('col1').resample('1W', label='left').sum()

/Users/roycoding/venv-lib-upgrade/lib/python2.7/site-packages/pandas/core/groupby.pyc in resample(self, rule, *args, **kwargs)
1080         """
1081         from pandas.tseries.resample import get_resampler_for_grouping
-> 1082         return get_resampler_for_grouping(self, rule, *args, **kwargs)
1083
1084     @Substitution(name='groupby')

/Users/roycoding/venv-lib-upgrade/lib/python2.7/site-packages/pandas/tseries/resample.pyc in get_resampler_for_grouping(groupby, rule, how, fill_method, limit, kind, **kwargs)
910                                        fill_method=fill_method,
911                                        limit=limit,
--> 912                                        **kwargs)
913
914

TypeError: _maybe_process_deprecations() got an unexpected keyword argument 'label'

# Fixed code
def get_resampler_for_grouping(
    groupby, rule, how=None, fill_method=None, limit=None, kind=None, **kwargs
):
    """return our appropriate resampler when grouping as well"""
    tg = TimeGrouper(freq=rule, **kwargs)
    resampler = tg._get_resampler(groupby.obj, kind=kind)
    r = resampler._get_resampler_for_grouping(groupby=groupby)
    return _maybe_process_deprecations(r, how=how, fill_method=fill_method, limit=limit)