Skip to content

Commit

Permalink
Make futureproof
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl committed May 6, 2024
1 parent c131955 commit dadc882
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions partd/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@
import pickle

import pandas as pd
from packaging.version import Version

try:
from pandas.core.internals.managers import create_block_manager_from_blocks
except ImportError:
from pandas.core.internals import create_block_manager_from_blocks
PANDAS_GE_210 = Version(pd.__version__).release >= (2, 1, 0)
PANDAS_GE_300 = Version(pd.__version__).major >= 3

from pandas.core.internals import make_block
from packaging.version import Version
if PANDAS_GE_300:
from pandas.api.internals import create_dataframe_from_blocks
create_block_manager_from_blocks= None
make_block = None
else:
try:
from pandas.core.internals.managers import create_block_manager_from_blocks
except ImportError:
from pandas.core.internals import create_block_manager_from_blocks

from pandas.core.internals import make_block

from . import numpy as pnp
from .core import Interface
Expand All @@ -24,7 +32,6 @@ def is_extension_array(x):

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

PANDAS_GE_210 = Version(pd.__version__).release >= (2, 1, 0)


class PandasColumns(Interface):
Expand Down Expand Up @@ -142,7 +149,7 @@ def block_to_header_bytes(block):
return header, bytes


def block_from_header_bytes(header, bytes):
def block_from_header_bytes(header, bytes, create_block: bool):
placement, dtype, shape, (extension_type, extension_values) = header

if extension_type == "other":
Expand All @@ -158,7 +165,9 @@ def block_from_header_bytes(header, bytes):
tz_info = extension_values[0]
values = pd.DatetimeIndex(values).tz_localize('utc').tz_convert(
tz_info)
return make_block(values, placement=placement)
if create_block:
return make_block(values, placement=placement)
return values, placement


def serialize(df):
Expand Down Expand Up @@ -187,9 +196,11 @@ def deserialize(bytes):
bytes = frames[1:]
axes = [index_from_header_bytes(headers[0], bytes[0]),
index_from_header_bytes(headers[1], bytes[1])]
blocks = [block_from_header_bytes(h, b)
blocks = [block_from_header_bytes(h, b, create_block=not PANDAS_GE_300)
for (h, b) in zip(headers[2:], bytes[2:])]
if PANDAS_GE_210:
if PANDAS_GE_300:
return pd.api.internals.create_dataframe_from_blocks(blocks, axes[1], axes[0])
elif PANDAS_GE_210:
return pd.DataFrame._from_mgr(create_block_manager_from_blocks(blocks, axes), axes=axes)
else:
return pd.DataFrame(create_block_manager_from_blocks(blocks, axes))
Expand Down

0 comments on commit dadc882

Please sign in to comment.