|
19 | 19 | import pandas.core.algorithms as algos
|
20 | 20 | import pandas.core.common as com
|
21 | 21 | from pandas.core.common import(_possibly_downcast_to_dtype, isnull,
|
22 |
| - notnull, _DATELIKE_DTYPES) |
| 22 | + notnull, _DATELIKE_DTYPES, is_numeric_dtype, |
| 23 | + is_timedelta64_dtype, is_datetime64_dtype) |
23 | 24 |
|
24 | 25 | import pandas.lib as lib
|
25 | 26 | from pandas.lib import Timestamp
|
@@ -260,9 +261,13 @@ def indices(self):
|
260 | 261 |
|
261 | 262 | def _get_index(self, name):
|
262 | 263 | """ safe get index """
|
263 |
| - if isinstance(name, Timestamp): |
264 |
| - name = name.value |
265 |
| - return self.indices[name] |
| 264 | + try: |
| 265 | + return self.indices[name] |
| 266 | + except: |
| 267 | + if isinstance(name, Timestamp): |
| 268 | + name = name.value |
| 269 | + return self.indices[name] |
| 270 | + raise |
266 | 271 |
|
267 | 272 | @property
|
268 | 273 | def name(self):
|
@@ -683,7 +688,7 @@ def _try_cast(self, result, obj):
|
683 | 688 | def _cython_agg_general(self, how, numeric_only=True):
|
684 | 689 | output = {}
|
685 | 690 | for name, obj in self._iterate_slices():
|
686 |
| - is_numeric = _is_numeric_dtype(obj.dtype) |
| 691 | + is_numeric = is_numeric_dtype(obj.dtype) |
687 | 692 | if numeric_only and not is_numeric:
|
688 | 693 | continue
|
689 | 694 |
|
@@ -721,7 +726,7 @@ def _python_agg_general(self, func, *args, **kwargs):
|
721 | 726 |
|
722 | 727 | # since we are masking, make sure that we have a float object
|
723 | 728 | values = result
|
724 |
| - if _is_numeric_dtype(values.dtype): |
| 729 | + if is_numeric_dtype(values.dtype): |
725 | 730 | values = com.ensure_float(values)
|
726 | 731 |
|
727 | 732 | output[name] = self._try_cast(values[mask], result)
|
@@ -1087,7 +1092,7 @@ def aggregate(self, values, how, axis=0):
|
1087 | 1092 | raise NotImplementedError
|
1088 | 1093 | out_shape = (self.ngroups,) + values.shape[1:]
|
1089 | 1094 |
|
1090 |
| - if _is_numeric_dtype(values.dtype): |
| 1095 | + if is_numeric_dtype(values.dtype): |
1091 | 1096 | values = com.ensure_float(values)
|
1092 | 1097 | is_numeric = True
|
1093 | 1098 | else:
|
@@ -1481,6 +1486,15 @@ def __init__(self, index, grouper=None, name=None, level=None,
|
1481 | 1486 | self.grouper = None # Try for sanity
|
1482 | 1487 | raise AssertionError(errmsg)
|
1483 | 1488 |
|
| 1489 | + # if we have a date/time-like grouper, make sure that we have Timestamps like |
| 1490 | + if getattr(self.grouper,'dtype',None) is not None: |
| 1491 | + if is_datetime64_dtype(self.grouper): |
| 1492 | + from pandas import to_datetime |
| 1493 | + self.grouper = to_datetime(self.grouper) |
| 1494 | + elif is_timedelta64_dtype(self.grouper): |
| 1495 | + from pandas import to_timedelta |
| 1496 | + self.grouper = to_timedelta(self.grouper) |
| 1497 | + |
1484 | 1498 | def __repr__(self):
|
1485 | 1499 | return 'Grouping(%s)' % self.name
|
1486 | 1500 |
|
@@ -1928,7 +1942,7 @@ def _cython_agg_blocks(self, how, numeric_only=True):
|
1928 | 1942 | for block in data.blocks:
|
1929 | 1943 | values = block.values
|
1930 | 1944 |
|
1931 |
| - is_numeric = _is_numeric_dtype(values.dtype) |
| 1945 | + is_numeric = is_numeric_dtype(values.dtype) |
1932 | 1946 |
|
1933 | 1947 | if numeric_only and not is_numeric:
|
1934 | 1948 | continue
|
@@ -2980,12 +2994,6 @@ def _reorder_by_uniques(uniques, labels):
|
2980 | 2994 | }
|
2981 | 2995 |
|
2982 | 2996 |
|
2983 |
| -def _is_numeric_dtype(dt): |
2984 |
| - typ = dt.type |
2985 |
| - return (issubclass(typ, (np.number, np.bool_)) |
2986 |
| - and not issubclass(typ, (np.datetime64, np.timedelta64))) |
2987 |
| - |
2988 |
| - |
2989 | 2997 | def _intercept_function(func):
|
2990 | 2998 | return _func_table.get(func, func)
|
2991 | 2999 |
|
|
0 commit comments