Skip to content
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

Pandas 1.3.0 compatibility #49

Merged
merged 3 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions partd/compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
unicode = unicode
import cPickle as pickle
from Queue import Queue, Empty

15 changes: 13 additions & 2 deletions partd/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,25 @@
from .encode import Encode
from .utils import extend, framesplit, frame


try:
# pandas >= 0.24.0
from pandas.api.types import is_extension_array_dtype
except ImportError:
def is_extension_array_dtype(dtype):
return False

try:
# Some `ExtensionArray`s can have a `.dtype` which is not a `ExtensionDtype`
# (e.g. they can be backed by a NumPy dtype). For these cases we check
# whether the instance is a `ExtensionArray`.
# https://github.com/dask/partd/issues/48
from pandas.api.extensions import ExtensionArray
def is_extension_array(x):
return isinstance(x, ExtensionArray)
except ImportError:
def is_extension_array(x):
return False


dumps = partial(pickle.dumps, protocol=pickle.HIGHEST_PROTOCOL)

Expand Down Expand Up @@ -129,7 +140,7 @@ def block_to_header_bytes(block):
elif is_datetime64tz_dtype(block):
extension = ('datetime64_tz_type', (block.values.tzinfo,))
values = values.view('i8')
elif is_extension_array_dtype(block.dtype):
elif is_extension_array_dtype(block.dtype) or is_extension_array(values):
Copy link
Member

Choose a reason for hiding this comment

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

I suppose you could also replace this with elif isinstance(values, ExtensionArray), and I would think this should work for older versions of pandas as well.

(but the current PR should work as well, I think)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for reviewing @jorisvandenbossche! I avoided a pure isinstance(values, ExtensionArray) check since partd supports older versions of pandas which predate the ExtensionArray class

Copy link
Member

Choose a reason for hiding this comment

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

OK, sounds good then!

Choose a reason for hiding this comment

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

could also do a not isinstance(values, np.ndarray) (unless you support pandas so old that it can still be a CategoricalIndex or something)

extension = ("other", ())
else:
extension = ('numpy_type', ())
Expand Down