Skip to content

REF: check can_hold_element instead of try/except #27298

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 2 commits into from
Jul 10, 2019

Conversation

jbrockmendel
Copy link
Member

_can_hold_element is not well-documented, and in local testing it does not match my intution of "we could do self.values[:] = element without raising". But adding two assertions in fillna does work in all the tests:

         # fillna, but if we cannot coerce, then try again as an ObjectBlock
        try:
            # equivalent: self._try_coerce_args(value) would not raise
            # Note: we only call try_coerce_args to let it raise
            self._try_coerce_args(value)
            assert self._can_hold_element(value)
        except (TypeError, ValueError): 
            assert not self._can_hold_element(value)

(at least after patching the DatetimeBlock and TimedeltaBlock implementations)

Confirm that the try/except behavior for fillna is equivalent to checking _can_hold_element
@@ -2275,7 +2271,13 @@ def _can_hold_element(self, element):
tipo = maybe_infer_dtype_type(element)
if tipo is not None:
return tipo == _NS_DTYPE or tipo == np.int64
return is_integer(element) or isinstance(element, datetime) or isna(element)
if isinstance(element, datetime):
Copy link
Contributor

Choose a reason for hiding this comment

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

in followup, can you make these elif

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure

@@ -2627,6 +2629,8 @@ def _can_hold_element(self, element):
tipo = maybe_infer_dtype_type(element)
if tipo is not None:
return issubclass(tipo.type, (np.timedelta64, np.int64))
if element is NaT:
Copy link
Contributor

Choose a reason for hiding this comment

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

same

Copy link
Contributor

Choose a reason for hiding this comment

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

though maybe just add this one below on 2634

@jreback jreback added Clean Internals Related to non-user accessible pandas implementation labels Jul 9, 2019
@jreback jreback added this to the 0.25.0 milestone Jul 9, 2019
@jreback
Copy link
Contributor

jreback commented Jul 9, 2019

if you can update to those comments, otherwise lgtm.

@jbrockmendel
Copy link
Member Author

Follow up coming shortly, let’s make elif change there

@jbrockmendel
Copy link
Member Author

@jreback gentle ping for this and #27229; next-steps in the works for both of these

@jreback jreback merged commit 823af76 into pandas-dev:master Jul 10, 2019
@jreback
Copy link
Contributor

jreback commented Jul 10, 2019

thanks @jbrockmendel if you can do those followups in next PRs

@jbrockmendel jbrockmendel deleted the tryless branch July 10, 2019 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Clean Internals Related to non-user accessible pandas implementation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants