Skip to content

Commit fe03b01

Browse files
committed
Merge pull request #3934 from jreback/GH3920
CLN: cleaned up _try_cast in core/groupby.py to eliminate cruft (GH3920)
2 parents 010f7a4 + 8ffa9b2 commit fe03b01

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

pandas/core/groupby.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -432,23 +432,13 @@ def picker(arr):
432432
def _try_cast(self, result, obj):
433433
""" try to cast the result to our obj original type,
434434
we may have roundtripped thru object in the mean-time """
435-
try:
436-
if obj.ndim > 1:
437-
dtype = obj.values.dtype
438-
else:
439-
dtype = obj.dtype
440-
441-
if _is_numeric_dtype(dtype):
442-
443-
# need to respect a non-number here (e.g. Decimal)
444-
if len(result) and issubclass(type(result[0]),(np.number,float,int)):
445-
result = _possibly_downcast_to_dtype(result, dtype)
435+
if obj.ndim > 1:
436+
dtype = obj.values.dtype
437+
else:
438+
dtype = obj.dtype
446439

447-
elif issubclass(dtype.type, np.datetime64):
448-
if is_datetime64_dtype(obj.dtype):
449-
result = result.astype(obj.dtype)
450-
except:
451-
pass
440+
if not np.isscalar(result):
441+
result = _possibly_downcast_to_dtype(result, dtype)
452442

453443
return result
454444

0 commit comments

Comments
 (0)