Repository: pandas-dev/pandas

Issue:
OverflowError in resample+aggregate for tz-aware index and list-like aggregation
#### Code Sample, a copy-pastable example if possible

```python
df = pd.DataFrame(np.random.rand(200, 1),
                  index=pd.DatetimeIndex(start='2017-01-01', freq='15min', periods=200, tz='Europe/Berlin'),
                  columns=['t2p'])
df.resample('1d').aggregate(['mean'])

...

OverflowError [full traceback in 'details' below]
```
<details>
<pre>
TypeError                                 Traceback (most recent call last)
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

TypeError: an integer is required

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
~/.conda/envs/everything/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3077             try:
-> 3078                 return self._engine.get_loc(key)
   3079             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine._date_check_type()

KeyError: 't2p'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

TypeError: an integer is required

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
~/.conda/envs/everything/lib/python3.6/site-packages/pandas/core/indexes/datetimes.py in get_loc(self, key, method, tolerance)
   1612         try:
-> 1613             return Index.get_loc(self, key, method, tolerance)
   1614         except (KeyError, ValueError, TypeError):

~/.conda/envs/everything/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3079             except KeyError:
-> 3080                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   3081 

pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine._date_check_type()

KeyError: 't2p'

During handling of the above exception, another exception occurred:

OverflowError                             Traceback (most recent call last)
<ipython-input-5-96f75edb7e05> in <module>()
      2                   index=pd.DatetimeIndex(start='2017-01-01', freq='1h', periods=100, tz='Europe/Berlin'),
      3                   columns=['t2p'])
----> 4 df.resample('1d').aggregate(['mean'])
      5 
      6 

~/.conda/envs/everything/lib/python3.6/site-packages/pandas/core/resample.py in aggregate(self, arg, *args, **kwargs)
    238 
    239         self._set_binner()
--> 240         result, how = self._aggregate(arg, *args, **kwargs)
    241         if result is None:
    242             result = self._groupby_and_aggregate(arg,

~/.conda/envs/everything/lib/python3.6/site-packages/pandas/core/base.py in _aggregate(self, arg, *args, **kwargs)
    549             return self._aggregate_multiple_funcs(arg,
    550                                                   _level=_level,
--> 551                                                   _axis=_axis), None
    552         else:
    553             result = None

~/.conda/envs/everything/lib/python3.6/site-packages/pandas/core/base.py in _aggregate_multiple_funcs(self, arg, _level, _axis)
    594                 try:
    595                     colg = self._gotitem(col, ndim=1,
--> 596                                          subset=obj.iloc[:, index])
    597                     results.append(colg.aggregate(arg))
    598                     keys.append(col)

~/.conda/envs/everything/lib/python3.6/site-packages/pandas/core/resample.py in _gotitem(self, key, ndim, subset)
    298         # try the key selection
    299         try:
--> 300             return grouped[key]
    301         except KeyError:
    302             return grouped

~/.conda/envs/everything/lib/python3.6/site-packages/pandas/core/base.py in __getitem__(self, key)
    264 
    265         else:
--> 266             if key not in self.obj:
    267                 raise KeyError("Column not found: {key}".format(key=key))
    268             return self._gotitem(key, ndim=1)

~/.conda/envs/everything/lib/python3.6/site-packages/pandas/core/generic.py in __contains__(self, key)
   1520     def __contains__(self, key):
   1521         """True if the key is in the info axis"""
-> 1522         return key in self._info_axis
   1523 
   1524     @property

~/.conda/envs/everything/lib/python3.6/site-packages/pandas/core/indexes/datetimelike.py in __contains__(self, key)
    379     def __contains__(self, key):
    380         try:
--> 381             res = self.get_loc(key)
    382             return (is_scalar(res) or isinstance(res, slice) or
    383                     (is_list_like(res) and len(res)))

~/.conda/envs/everything/lib/python3.6/site-packages/pandas/core/indexes/datetimes.py in get_loc(self, key, method, tolerance)
   1619 
   1620             try:
-> 1621                 stamp = Timestamp(key, tz=self.tz)
   1622                 return Index.get_loc(self, stamp, method, tolerance)
   1623             except KeyError:

pandas/_libs/tslibs/timestamps.pyx in pandas._libs.tslibs.timestamps.Timestamp.__new__()

pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.convert_to_tsobject()

pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.convert_str_to_tsobject()

pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.convert_to_tsobject()

pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.convert_datetime_to_tsobject()

pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion._localize_pydatetime()

~/.conda/envs/everything/lib/python3.6/site-packages/pytz/tzinfo.py in localize(self, dt, is_dst)
    321         possible_loc_dt = set()
    322         for delta in [timedelta(days=-1), timedelta(days=1)]:
--> 323             loc_dt = dt + delta
    324             idx = max(0, bisect_right(
    325                 self._utc_transition_times, loc_dt) - 1)

OverflowError: date value out of range
</pre>
</details>

#### Problem description

Here are some changes I have tried in order to work out which situations trigger the bug:

* No error for tz-naive or UTC, error for 'Europe/Berlin' and 'America/New_York'
* No error for column name 't2', 't2x', 't2q', 'T_2M', error for 't2p', 't2m', 't2m1', 'T2M'
* The frequency of the DatetimeIndex and the resample period do not seem to matter
* No error for `.resample().mean()` etc., the error only happens when using `.resample().aggregate()`

#### Expected Output
```
                                 t2p
                               mean
2017-01-01 00:00:00+01:00  0.397067
2017-01-02 00:00:00+01:00  0.519352
2017-01-03 00:00:00+01:00  0.534746
2017-01-04 00:00:00+01:00  0.587625
2017-01-05 00:00:00+01:00  0.497514
```
#### Output of ``pd.show_versions()``

<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.6.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-33-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8

pandas: 0.23.4
pytest: 3.7.4
pip: 10.0.1
setuptools: 40.2.0
Cython: 0.28.5
numpy: 1.15.1
scipy: 1.1.0
pyarrow: None
xarray: 0.10.8
IPython: 6.5.0
sphinx: 1.7.8
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: 1.2.1
tables: 3.4.3
numexpr: 2.6.8
feather: None
matplotlib: 2.2.3
openpyxl: 2.5.6
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.1.0
lxml: 4.2.4
bs4: 4.6.3
html5lib: 1.0.1
sqlalchemy: 1.2.11
pymysql: None
psycopg2: 2.7.5 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
</details>



Discussion:
That does seem odd indeed!  Investigation and PR are welcome!

Resolving patch:
diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py
--- a/pandas/core/indexes/datetimes.py
+++ b/pandas/core/indexes/datetimes.py
@@ -1010,7 +1010,7 @@ def get_loc(self, key, method=None, tolerance=None):
         except (KeyError, ValueError, TypeError):
             try:
                 return self._get_string_slice(key)
-            except (TypeError, KeyError, ValueError):
+            except (TypeError, KeyError, ValueError, OverflowError):
                 pass
 
             try:


Test patch:
diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py
--- a/pandas/tests/resample/test_resample_api.py
+++ b/pandas/tests/resample/test_resample_api.py
@@ -549,3 +549,25 @@ def test_selection_api_validation():
 
     exp.index.name = 'd'
     assert_frame_equal(exp, df.resample('2D', level='d').sum())
+
+
+@pytest.mark.parametrize('col_name', ['t2', 't2x', 't2q', 'T_2M',
+                                      't2p', 't2m', 't2m1', 'T2M'])
+def test_agg_with_datetime_index_list_agg_func(col_name):
+    # GH 22660
+    # The parametrized column names would get converted to dates by our
+    # date parser. Some would result in OutOfBoundsError (ValueError) while
+    # others would result in OverflowError when passed into Timestamp.
+    # We catch these errors and move on to the correct branch.
+    df = pd.DataFrame(list(range(200)),
+                      index=pd.date_range(start='2017-01-01', freq='15min',
+                                          periods=200, tz='Europe/Berlin'),
+                      columns=[col_name])
+    result = df.resample('1d').aggregate(['mean'])
+    expected = pd.DataFrame([47.5, 143.5, 195.5],
+                            index=pd.date_range(start='2017-01-01', freq='D',
+                                                periods=3, tz='Europe/Berlin'),
+                            columns=pd.MultiIndex(levels=[[col_name],
+                                                          ['mean']],
+                                                  codes=[[0], [0]]))
+    assert_frame_equal(result, expected)