Repository: pandas-dev/pandas

Issue:
BUG: resample and apply modify the index type for empty Series
#### A small, complete example of the issue

Consider the following two resamplings on empty Series:

``` python
>>> import pandas as pd

# These work:

>>> pd.Series(index=pd.DatetimeIndex([]), data=[]).resample('H').apply(np.sum).index
DatetimeIndex([], dtype='datetime64[ns]', freq='H')

>>> pd.Series(index=pd.DatetimeIndex([]), data=[], name="foobar").resample('H').apply(np.sum).name
'foobar'

# These don't work properly:

>>> pd.Series(index=pd.DatetimeIndex([]), data=[]).resample('H').apply(lambda x: 1).index
Index([], dtype='object')

>>> pd.Series(index=pd.DatetimeIndex([]), data=[], name="foobar").resample('H').apply(lambda x: 1).name
# no output
```
#### Expected Output

I would have expected that the index in the second example would be the same as in the first one. That is, the expected output:

``` python
>>> pd.Series(index=pd.DatetimeIndex([]), data=[]).resample('H').apply(lambda x: 1).index
DatetimeIndex([], dtype='datetime64[ns]', freq='H')
```

Also, `name` attribute shouldn't get lost:

``` python
>>> pd.Series(index=pd.DatetimeIndex([]), data=[], name="foobar").resample('H').apply(lambda x: 1).name
'foobar'
```

I suspect that there might be other attributes / metadata that is lost too.
#### Output of `pd.show_versions()`

<details>
## INSTALLED VERSIONS

commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Linux
OS-release: 4.7.5-gnu-1
machine: x86_64
processor: 
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.19.0rc1
nose: 1.3.6
pip: 8.1.2
setuptools: 27.3.0
Cython: 0.24.1
numpy: 1.11.1
scipy: 0.18.1
statsmodels: None
xarray: None
IPython: 5.0.0
sphinx: 1.4.5
patsy: None
dateutil: 2.5.3
pytz: 2016.6.1
blosc: None
bottleneck: None
tables: 3.2.3.1
numexpr: 2.6.1
matplotlib: 1.5.2
openpyxl: None
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.5.1
html5lib: None
httplib2: 0.9.2
apiclient: 1.5.1
sqlalchemy: 1.0.15
pymysql: None
psycopg2: 2.6.2 (dt dec pq3 ext lo64)
jinja2: 2.8
boto: None
pandas_datareader: None
</details>



Discussion:
yep, looks like a bug, pull-requests are appreciated.

Probably caused by the same bug: `name` attribute is also lost in the latter example. I'll update the original post.


Resolving patch:
diff --git a/pandas/core/resample.py b/pandas/core/resample.py
--- a/pandas/core/resample.py
+++ b/pandas/core/resample.py
@@ -17,7 +17,7 @@
 from pandas.core.indexes.period import PeriodIndex, period_range
 import pandas.core.common as com
 import pandas.core.algorithms as algos
-from pandas.core.dtypes.generic import ABCDataFrame
+from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
 
 import pandas.compat as compat
 from pandas.compat.numpy import function as nv
@@ -439,6 +439,11 @@ def _wrap_result(self, result):
         if isinstance(result, com.ABCSeries) and self._selection is not None:
             result.name = self._selection
 
+        if isinstance(result, ABCSeries) and result.empty:
+            obj = self.obj
+            result.index = obj.index._shallow_copy(freq=to_offset(self.freq))
+            result.name = getattr(obj, 'name', None)
+
         return result
 
     def pad(self, limit=None):


Test patch:
diff --git a/pandas/tests/test_resample.py b/pandas/tests/test_resample.py
--- a/pandas/tests/test_resample.py
+++ b/pandas/tests/test_resample.py
@@ -852,6 +852,16 @@ def test_resample_loffset_arg_type(self):
                 assert_frame_equal(result_agg, expected)
                 assert_frame_equal(result_how, expected)
 
+    def test_apply_to_empty_series(self):
+        # GH 14313
+        series = self.create_series()[:0]
+
+        for freq in ['M', 'D', 'H']:
+            result = series.resample(freq).apply(lambda x: 1)
+            expected = series.resample(freq).apply(np.sum)
+
+            assert_series_equal(result, expected, check_dtype=False)
+
 
 class TestDatetimeIndex(Base):
     _index_factory = lambda x: date_range
@@ -2794,6 +2804,14 @@ def test_evenly_divisible_with_no_extra_bins(self):
         result = df.resample('7D').sum()
         assert_frame_equal(result, expected)
 
+    def test_apply_to_empty_series(self):
+        # GH 14313
+        series = self.create_series()[:0]
+
+        for freq in ['M', 'D', 'H']:
+            with pytest.raises(TypeError):
+                series.resample(freq).apply(lambda x: 1)
+
 
 class TestTimedeltaIndex(Base):
     _index_factory = lambda x: timedelta_range