# Buggy code
def insert(self, loc, item):
    """
    Make new Index inserting new item at location

    Parameters
    ----------
    loc : int
    item : object
        if not either a Python datetime or a numpy integer-like, returned
        Index dtype will be object rather than datetime.

    Returns
    -------
    new_index : Index
    """

    freq = None
    if isinstance(item, (datetime, np.datetime64)):
        zone = tslib.get_timezone(self.tz)
        izone = tslib.get_timezone(getattr(item, "tzinfo", None))
        if zone != izone:
            raise ValueError("Passed item and index have different timezone")
        # check freq can be preserved on edge cases
        if self.freq is not None:
            if (loc == 0 or loc == -len(self)) and item + self.freq == self[0]:
                freq = self.freq
            elif (loc == len(self)) and item - self.freq == self[-1]:
                freq = self.freq
        item = _to_m8(item, tz=self.tz)
    try:
        new_dates = np.concatenate(
            (self[:loc].asi8, [item.view(np.int64)], self[loc:].asi8)
        )
        if self.tz is not None:
            new_dates = tslib.tz_convert(new_dates, "UTC", self.tz)
        return DatetimeIndex(new_dates, name=self.name, freq=freq, tz=self.tz)

    except (AttributeError, TypeError):
        # fall back to object index
        if isinstance(item, compat.string_types):
            return self.asobject.insert(loc, item)
        raise TypeError("cannot insert DatetimeIndex with incompatible label")


# Traceback (IndexError)
INSTALLED VERSIONS
------------------
commit: None
python: 2.7.9.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 26 Stepping 5, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.16.1-46-g0aceb38
nose: 1.3.6
Cython: 0.22
numpy: 1.9.2
scipy: 0.14.0
statsmodels: 0.6.1
IPython: 3.1.0
sphinx: 1.3.1
patsy: 0.3.0
dateutil: 2.4.2
pytz: 2015.4
bottleneck: 0.8.0
tables: 3.1.1
numexpr: 2.3.1
matplotlib: 1.4.3
openpyxl: None
xlrd: 0.9.3
xlwt: None
xlsxwriter: 0.7.2
lxml: None
bs4: 4.3.2
html5lib: 0.999
httplib2: None
apiclient: None
sqlalchemy: 1.0.4
pymysql: None
psycopg2: None

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\test_set_empty_series_with_freq.py in <module>()
11 ts2 = pd.TimeSeries(0, pd.date_range('2011-01-01', '2011-01-01'))[:0]
12
---> 13 ts2[pd.datetime(2012, 1, 1)] = 47

c:\python\envs\pandas-0.16.1\lib\site-packages\pandas\core\series.pyc in __setitem__(self, key, value)
687         # do the setitem
688         cacher_needs_updating = self._check_is_chained_assignment_possible()
--> 689         setitem(key, value)
690         if cacher_needs_updating:
691             self._maybe_update_cacher()

c:\python\envs\pandas-0.16.1\lib\site-packages\pandas\core\series.pyc in setitem(key, value)
660                             pass
661                 try:
--> 662                     self.loc[key] = value
663                 except:
664                     print ""

c:\python\envs\pandas-0.16.1\lib\site-packages\pandas\core\indexing.pyc in __setitem__(self, key, value)
113     def __setitem__(self, key, value):
114         indexer = self._get_setitem_indexer(key)
--> 115         self._setitem_with_indexer(indexer, value)
116
117     def _has_valid_type(self, k, axis):

c:\python\envs\pandas-0.16.1\lib\site-packages\pandas\core\indexing.pyc in _setitem_with_indexer(self, indexer, value)
272                 if self.ndim == 1:
273                     index = self.obj.index
--> 274                     new_index = index.insert(len(index),indexer)
275
276                     # this preserves dtype of the value

c:\python\envs\pandas-0.16.1\lib\site-packages\pandas\tseries\index.pyc in insert(self, loc, item)
1523             # check freq can be preserved on edge cases
1524             if self.freq is not None:
-> 1525                 if (loc == 0 or loc == -len(self)) and item + self.freq == self[0]:
1526                     freq = self.freq
1527                 elif (loc == len(self)) and item - self.freq == self[-1]:

c:\python\envs\pandas-0.16.1\lib\site-packages\pandas\tseries\index.pyc in __getitem__(self, key)
1351         getitem = self._data.__getitem__
1352         if np.isscalar(key):
-> 1353             val = getitem(key)
1354             return Timestamp(val, offset=self.offset, tz=self.tz)
1355         else:

IndexError: index 0 is out of bounds for axis 0 with size 0

# Fixed code
def insert(self, loc, item):
    """
    Make new Index inserting new item at location

    Parameters
    ----------
    loc : int
    item : object
        if not either a Python datetime or a numpy integer-like, returned
        Index dtype will be object rather than datetime.

    Returns
    -------
    new_index : Index
    """

    freq = None
    if isinstance(item, (datetime, np.datetime64)):
        zone = tslib.get_timezone(self.tz)
        izone = tslib.get_timezone(getattr(item, "tzinfo", None))
        if zone != izone:
            raise ValueError("Passed item and index have different timezone")
        # check freq can be preserved on edge cases
        if self.size and self.freq is not None:
            if (loc == 0 or loc == -len(self)) and item + self.freq == self[0]:
                freq = self.freq
            elif (loc == len(self)) and item - self.freq == self[-1]:
                freq = self.freq
        item = _to_m8(item, tz=self.tz)
    try:
        new_dates = np.concatenate(
            (self[:loc].asi8, [item.view(np.int64)], self[loc:].asi8)
        )
        if self.tz is not None:
            new_dates = tslib.tz_convert(new_dates, "UTC", self.tz)
        return DatetimeIndex(new_dates, name=self.name, freq=freq, tz=self.tz)

    except (AttributeError, TypeError):
        # fall back to object index
        if isinstance(item, compat.string_types):
            return self.asobject.insert(loc, item)
        raise TypeError("cannot insert DatetimeIndex with incompatible label")