Skip to content

Commit

Permalink
WIP: fix passing block manager to frame or series
Browse files Browse the repository at this point in the history
  • Loading branch information
theOehrly committed Feb 5, 2024
1 parent 93a0db4 commit ebd143d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
24 changes: 23 additions & 1 deletion fastf1/internals/pandas_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@
import pandas as pd


# dangerous import of pandas internals
# these imports are covered by
# test_internals.py::test_pandas_base_internal_imports
# to detect any failures as soon as possible
try:
from pandas.core.internals import SingleBlockManager
except ImportError as exc:
_mgr_instance = getattr(pd.Series(), '_mgr')
if _mgr_instance is None:
raise ImportError("Import of Pandas internals failed. You are likely "
"using a recently released version of Pandas that "
"isn't yet supported. Please report this issue. In"
"the meantime, you can try downgrading to an older "
"version of Pandas.") from exc
SingleBlockManager = type(_mgr_instance)


class BaseDataFrame(pd.DataFrame):
"""Base class for objects that inherit from ``pandas.DataFrame``.
Expand Down Expand Up @@ -97,7 +114,12 @@ def __new__(cls, data=None, index=None, *args, **kwargs) -> pd.Series:
# the data is a row of the parent DataFrame
constructor = parent._constructor_sliced_horizontal

obj = constructor(data=data, index=index, *args, **kwargs)
if (isinstance(data, SingleBlockManager)
and hasattr(constructor, '_from_mgr')
and pd.__version__.startswith('2.')):
obj = constructor._from_mgr(data, axes=data.axes)
else:
obj = constructor(data=data, index=index, *args, **kwargs)

if parent is not None:
# catch-all fix for some missing __finalize__ calls in Pandas
Expand Down
9 changes: 9 additions & 0 deletions fastf1/tests/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
from fastf1.internals.pandas_extensions import _unsafe_create_df_fast


def test_pandas_base_internal_imports():
# ensure that the internal import (still) works and ensure that the
# fallback resolves to the same type
from pandas.core.internals import SingleBlockManager

FallbackSingleBlockManager = type(getattr(pd.Series(), '_mgr'))
assert SingleBlockManager is FallbackSingleBlockManager


def test_fast_df_creation():
data = {'A': [1, 2, 3], 'B': [1.0, 2.0, 3.0], 3: ['a', 'b', 'c']}

Expand Down

0 comments on commit ebd143d

Please sign in to comment.