Skip to content

REF: hold PeriodArray in NDArrayBackedExtensionBlock #44681

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 12 commits into from
Dec 14, 2021

Conversation

jbrockmendel
Copy link
Member

  • closes #xxxx
  • tests added / passed
  • Ensure all linting tests pass, see here for how to run them
  • whatsnew entry

After this, Categorical will be the only NDArrayBackedExtensionArray not held in NDarrayBackedExtensionBlock.

(this also fixes a Series.where bug for PeriodDtype, the general fix (and tests) are blocked by #44514)

@jreback jreback added the Period Period data type label Dec 4, 2021
stacklevel=find_stack_level(),
)
else:
warnings.warn(
Copy link
Contributor

Choose a reason for hiding this comment

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

these warnings tested?

Copy link
Member Author

Choose a reason for hiding this comment

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

not the new one; need to revert it since ive xfailed the cases that get here

@jreback jreback added this to the 1.4 milestone Dec 14, 2021
@jreback jreback merged commit b8d5e9c into pandas-dev:master Dec 14, 2021
@jbrockmendel jbrockmendel deleted the enh-ea2d branch December 14, 2021 15:42
@jorisvandenbossche
Copy link
Member

@jbrockmendel what's the direct advantage of using NDArrayBackedExtensionBlock instead of ExtensionBlock for PeriodArray?

Because this broke the pyarrow integration for period dtype up to pyarrow 3.0 (released just less than a year ago)

@jbrockmendel
Copy link
Member Author

what's the direct advantage of using NDArrayBackedExtensionBlock instead of ExtensionBlock for PeriodArray?

2D support!

@jorisvandenbossche
Copy link
Member

And can you do that without breaking pyarrow <= 3.0?

@jbrockmendel
Copy link
Member Author

And can you do that without breaking pyarrow <= 3.0?

I dont know what private APIs it uses. Might be able to put in a shim.

@jorisvandenbossche
Copy link
Member

They are already using the pd.core.internals.make_block function, but so in older versions pyarrow is also passing the block class (https://github.com/apache/arrow/blob/d613aa68789288d3503dfbd8376a41f2d28b6c9d/python/pyarrow/pandas_compat.py#L752-L753):

Letting it infer it works fine (what happens on the latest pyarrow releases):

In [10]: pd.core.internals.make_block(arr, placement=slice(0, 1, 1))
Out[10]: NDArrayBackedExtensionBlock: slice(0, 1, 1), 1 x 3, dtype: period[D]

but explicitly asking for an ExtensionBlock gives a mangled block with a 2D array under the hood (and for that reason it thinks it is shape (1, 1), because the underlying array is (1, 3)):

In [11]: pd.core.internals.make_block(
    ...:     arr, placement=slice(0, 1, 1), klass=pd.core.internals.ExtensionBlock
    ...: )
Out[11]: ExtensionBlock: slice(0, 1, 1), 1 x 1, dtype: period[D]

In [12]: _.shape
Out[12]: (1, 1)

@jbrockmendel
Copy link
Member Author

but explicitly asking for an ExtensionBlock gives a mangled block

yah it shouldn't do that. we could put a shim in ExtensionBlock.__init__ to check for that and raise, maybe telling users to upgrade pyarrow.

@jorisvandenbossche
Copy link
Member

And it already doesn't do that anymore.

we could put a shim in ExtensionBlock.init to check for that and raise, maybe telling users to upgrade pyarrow.

Can't we handle that inside make_block? If you pass a period array, and specify ExtensionBlock, we can return the proper block instead?

@jbrockmendel
Copy link
Member Author

Can't we handle that inside make_block? If you pass a period array, and specify ExtensionBlock, we can return the proper block instead?

makes sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Period Period data type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants