Skip to content

CLN: cleaned up _try_cast in core/groupby.py to eliminate cruft (GH3920) #3934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,23 +432,13 @@ def picker(arr):
def _try_cast(self, result, obj):
""" try to cast the result to our obj original type,
we may have roundtripped thru object in the mean-time """
try:
if obj.ndim > 1:
dtype = obj.values.dtype
else:
dtype = obj.dtype

if _is_numeric_dtype(dtype):

# need to respect a non-number here (e.g. Decimal)
if len(result) and issubclass(type(result[0]),(np.number,float,int)):
result = _possibly_downcast_to_dtype(result, dtype)
if obj.ndim > 1:
dtype = obj.values.dtype
else:
dtype = obj.dtype

elif issubclass(dtype.type, np.datetime64):
if is_datetime64_dtype(obj.dtype):
result = result.astype(obj.dtype)
except:
pass
if not np.isscalar(result):
result = _possibly_downcast_to_dtype(result, dtype)

return result

Expand Down