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

Fixes compatibility issue with Python 3.6 (with pandas 1.1.5) #1692

Merged
merged 1 commit into from
Dec 25, 2023
Merged
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
59 changes: 48 additions & 11 deletions python/vineyard/data/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,27 @@
except ImportError:
from pandas.core.arrays import NumpyExtensionArray as PandasArray

try:
from pandas.core.arrays._mixins import NDArrayBackedExtensionArray

NDArrayBacked = None
except ImportError:
from pandas._libs.arrays import NDArrayBacked

NDArrayBackedExtensionArray = None

try:
from pandas.core.internals.blocks import BlockPlacement
from pandas.core.internals.blocks import NumpyBlock as Block
except ImportError:
BlockPlacement = None
from pandas.core.internals.blocks import Block

try:
from pandas.core.internals.blocks import NDArrayBackedExtensionBlock
except ImportError:
NDArrayBackedExtensionBlock = None

try:
from pandas.core.indexes.base import ensure_index
except ImportError:
Expand Down Expand Up @@ -175,9 +189,15 @@ def __from_numpy__(self, array: np.ndarray) -> pd.api.extensions.ExtensionArray:


class NDArrayArray(
pd.api.extensions.ExtensionArray,
pd._libs.arrays.NDArrayBacked,
NDArrayOperatorsMixin,
*(
(NDArrayBackedExtensionArray, NDArrayOperatorsMixin)
if NDArrayBackedExtensionArray
else (
pd.api.extensions.ExtensionArray,
NDArrayBacked,
NDArrayOperatorsMixin,
)
),
):
def __init__(self, data, dim=1):
"""Initialize from an nd-array or list of arrays."""
Expand All @@ -192,7 +212,10 @@ def __init__(self, data, dim=1):
else:
ndarray = np.stack(data)
self._dim = dim
pd._libs.arrays.NDArrayBacked.__init__(self, ndarray, ndarray.dtype)
if NDArrayBackedExtensionArray:
NDArrayBackedExtensionArray.__init__(self, ndarray, ndarray.dtype)
else:
NDArrayBacked.__init__(self, ndarray, ndarray.dtype)

# Attributes
@property
Expand Down Expand Up @@ -393,15 +416,29 @@ def shape(self):
return self.ndarray.ndarray_shape


class NDArrayBlock(pd.core.internals.blocks.NDArrayBackedExtensionBlock):
"""Block for multi-dimensional ndarray."""
if NDArrayBackedExtensionBlock:

class NDArrayBlock(pd.core.internals.blocks.NDArrayBackedExtensionBlock):
"""Block for multi-dimensional ndarray."""

__slots__ = ()
is_numeric = False
values: NDArrayArray

def values_for_json(self) -> np.ndarray:
return self.values._ndarray

else:

class NDArrayBlock(Block):
"""Block for multi-dimensional ndarray."""

__slots__ = ()
is_numeric = False
values: NDArrayArray
__slots__ = ()
is_numeric = False
values: NDArrayArray

def values_for_json(self) -> np.ndarray:
return self.values._ndarray
def get_block_values_for_json(self) -> np.ndarray:
return np.asarray(self.values).reshape(self.shape)


def pandas_dataframe_builder(client, value, builder, **kw):
Expand Down