-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
REF/CLN: maybe_downcast_to_dtype #27714
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
Conversation
pandas/core/dtypes/cast.py
Outdated
result = to_datetime(result).tz_localize("utc") | ||
result = result.tz_convert(dtype.tz) | ||
|
||
elif dtype.type == Period: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about isintance instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually i think the ==
should be is
, will update
# earlier | ||
result = np.array(result) | ||
|
||
def trans(x): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rather than doing this, I would pass in a callable directly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that gives a lot more degrees of freedom to the caller, I'd rather it just be a bool kwarg until/unless we need something more
return result | ||
|
||
|
||
def maybe_downcast_numeric(result, dtype, do_round: bool = False): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is very similar to to_numeric; would plan as a followup to move to_numeric logic here and call this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea
thanks, this is pretty dense code, good to simplify a bit. |
Separate out a numeric-only maybe_downcast_to_dtype_numeric from maybe_downcast_to_dtype. Avoid a giant try/except by checking the correct things up front.
The non-numeric portion of maybe_downcast_to_dtype casts to datetime64, datetime64tz, or PeriodArray. Following #27683 the next step in cleaning up internals is going to be getting rid of _try_cast_result, which will involve replacing a usage of maybe_cast_to_dtype with the less-broadly-scoped maybe_cast_to_dtype_numeric.