Skip to content

Commit

Permalink
Fixes compatibility issue with Python 3.6 (with pandas 1.1.5) (#1692)
Browse files Browse the repository at this point in the history
Fixes the compatibility issue with Python 3.6 (and pandas 1.1.5, which
is the highest version that is available for Python 3.6).

Fixes #1691

Signed-off-by: Tao He <[email protected]>
  • Loading branch information
sighingnow authored Dec 25, 2023
1 parent 4495203 commit e5995d3
Showing 1 changed file with 48 additions and 11 deletions.
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

0 comments on commit e5995d3

Please sign in to comment.