Skip to content

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

Merged
merged 9 commits into from
Aug 4, 2019
Merged

Conversation

jbrockmendel
Copy link
Member

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.

result = to_datetime(result).tz_localize("utc")
result = result.tz_convert(dtype.tz)

elif dtype.type == Period:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about isintance instead

Copy link
Member Author

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):
Copy link
Contributor

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

Copy link
Member Author

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):
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea

@jreback jreback added the Dtype Conversions Unexpected or buggy dtype conversions label Aug 2, 2019
@jreback jreback added this to the 1.0 milestone Aug 4, 2019
@jreback jreback merged commit 6af6d51 into pandas-dev:master Aug 4, 2019
@jreback
Copy link
Contributor

jreback commented Aug 4, 2019

thanks, this is pretty dense code, good to simplify a bit.

@jbrockmendel jbrockmendel deleted the dcast2 branch August 4, 2019 23:12
quintusdias pushed a commit to quintusdias/pandas_dev that referenced this pull request Aug 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants