# Buggy code
def _infer_freq(dates):
    if hasattr(dates, "freqstr"):
        return dates.freqstr
    try:
        from pandas.tseries.api import infer_freq

        freq = infer_freq(dates)
        return freq
    except ImportError:
        pass

    timedelta = datetime.timedelta
    nobs = min(len(dates), 6)
    if nobs == 1:
        raise ValueError("Cannot infer frequency from one date")
    if hasattr(dates, "values"):
        dates = dates.values  # can't do a diff on a DateIndex
    diff = np.diff(dates[:nobs])
    delta = _add_datetimes(diff)
    nobs -= 1  # after diff
    if delta == timedelta(nobs):  # greedily assume 'D'
        return "D"
    elif delta == timedelta(nobs + 2):
        return "B"
    elif delta == timedelta(7 * nobs):
        return "W"
    elif delta >= timedelta(28 * nobs) and delta <= timedelta(31 * nobs):
        return "M"
    elif delta >= timedelta(90 * nobs) and delta <= timedelta(92 * nobs):
        return "Q"
    elif delta >= timedelta(365 * nobs) and delta <= timedelta(366 * nobs):
        return "A"
    else:
        return


# Traceback (AttributeError)
======================================================================
ERROR: statsmodels.graphics.tests.test_tsaplots.test_plot_month
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/usr/lib/python2.7/dist-packages/numpy/testing/decorators.py", line 146, in skipper_func
return f(*args, **kwargs)
File "/build/buildd/statsmodels-0.6.0~ppa18~revno/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/graphics/tests/test_tsaplots.py", line 47, in test_plot_month
fig = month_plot(dta)
File "/build/buildd/statsmodels-0.6.0~ppa18~revno/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/graphics/tsaplots.py", line 246, in month_plot
_check_period_index(x, freq="M")
File "/build/buildd/statsmodels-0.6.0~ppa18~revno/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/tools/data.py", line 22, in _check_period_index
if not inferred_freq.startswith(freq):
AttributeError: 'NoneType' object has no attribute 'startswith'

======================================================================
ERROR: statsmodels.graphics.tests.test_tsaplots.test_plot_quarter
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/usr/lib/python2.7/dist-packages/numpy/testing/decorators.py", line 146, in skipper_func
return f(*args, **kwargs)
File "/build/buildd/statsmodels-0.6.0~ppa18~revno/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/graphics/tests/test_tsaplots.py", line 72, in test_plot_quarter
quarter_plot(dta.unemp)
File "/build/buildd/statsmodels-0.6.0~ppa18~revno/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/graphics/tsaplots.py", line 280, in quarter_plot
_check_period_index(x, freq="Q")
File "/build/buildd/statsmodels-0.6.0~ppa18~revno/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/tools/data.py", line 22, in _check_period_index
if not inferred_freq.startswith(freq):
AttributeError: 'NoneType' object has no attribute 'startswith'

======================================================================
ERROR: statsmodels.tsa.tests.test_arima.test_arma_predict_indices
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/build/buildd/statsmodels-0.6.0~ppa18~revno/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/tsa/tests/test_arima.py", line 971, in test_arma_predict_indices
_check_start(*((model,) + case))
File "/build/buildd/statsmodels-0.6.0~ppa18~revno/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/tsa/tests/test_arima.py", line 917, in _check_start
start = model._get_predict_start(given, dynamic)
File "/build/buildd/statsmodels-0.6.0~ppa18~revno/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/tsa/arima_model.py", line 581, in _get_predict_start
method)
File "/build/buildd/statsmodels-0.6.0~ppa18~revno/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/tsa/arima_model.py", line 306, in _validate
start = _index_date(start, dates)
File "/build/buildd/statsmodels-0.6.0~ppa18~revno/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/tsa/base/datetools.py", line 57, in _index_date
"an integer" % date)
ValueError: There is no frequency for these dates and date 2009-12-31 00:00:00 is not in dates index. Try giving a date that is in the dates index or use an integer

======================================================================
ERROR: statsmodels.tsa.tests.test_arima.test_arima_predict_indices
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/build/buildd/statsmodels-0.6.0~ppa18~revno/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/tsa/tests/test_arima.py", line 1043, in test_arima_predict_indices
_check_start(*((model,) + case))
File "/build/buildd/statsmodels-0.6.0~ppa18~revno/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/tsa/tests/test_arima.py", line 917, in _check_start
start = model._get_predict_start(given, dynamic)
File "/build/buildd/statsmodels-0.6.0~ppa18~revno/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/tsa/arima_model.py", line 959, in _get_predict_start
method)
File "/build/buildd/statsmodels-0.6.0~ppa18~revno/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/tsa/arima_model.py", line 306, in _validate
start = _index_date(start, dates)
File "/build/buildd/statsmodels-0.6.0~ppa18~revno/debian/python-statsmodels/usr/lib/python2.7/dist-packages/statsmodels/tsa/base/datetools.py", line 57, in _index_date
"an integer" % date)
ValueError: There is no frequency for these dates and date 2009-12-31 00:00:00 is not in dates index. Try giving a date that is in the dates index or use an integer

# Fixed code
def _infer_freq(dates):
    maybe_freqstr = getattr(dates, "freqstr", None)
    if maybe_freqstr is not None:
        return maybe_freqstr
    try:
        from pandas.tseries.api import infer_freq

        freq = infer_freq(dates)
        return freq
    except ImportError:
        pass

    timedelta = datetime.timedelta
    nobs = min(len(dates), 6)
    if nobs == 1:
        raise ValueError("Cannot infer frequency from one date")
    if hasattr(dates, "values"):
        dates = dates.values  # can't do a diff on a DateIndex
    diff = np.diff(dates[:nobs])
    delta = _add_datetimes(diff)
    nobs -= 1  # after diff
    if delta == timedelta(nobs):  # greedily assume 'D'
        return "D"
    elif delta == timedelta(nobs + 2):
        return "B"
    elif delta == timedelta(7 * nobs):
        return "W"
    elif delta >= timedelta(28 * nobs) and delta <= timedelta(31 * nobs):
        return "M"
    elif delta >= timedelta(90 * nobs) and delta <= timedelta(92 * nobs):
        return "Q"
    elif delta >= timedelta(365 * nobs) and delta <= timedelta(366 * nobs):
        return "A"
    else:
        return