Skip to content

Commit

Permalink
FIX: compatibility issue with pandas 1.3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
theOehrly committed Dec 27, 2023
1 parent 3474b03 commit 1b7fe9c
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions fastf1/internals/pandas_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@

from pandas import DataFrame, Index, RangeIndex
try:
# import internal pandas functions and objects
from pandas.core.internals.construction import \
_get_axes, \
BlockPlacement, \
create_block_manager_from_blocks, \
new_block_2d
from pandas.core.internals.managers import \
_consolidate
except ImportError as import_exc:

# verify existence of non-public methods of public objects
if not hasattr(Index, '_with_infer'):
raise AttributeError

except (AttributeError, ImportError) as import_exc:
logger.warning("Import of pandas internals failed", exc_info=import_exc)
CREATE_DF_FAST = False
else:
CREATE_DF_FAST = True


def create_df_fast(
Expand All @@ -39,7 +48,8 @@ def create_df_fast(
of errors
Returns:
Pandas DataFrame equivalent to the one create with the default
dataframe constructor
"""
try:
Expand All @@ -50,10 +60,25 @@ def create_df_fast(
# in case of error, use the usual but slower method
logger.warning("Falling back to slow data frame creation!")
logger.debug("Error during fast DataFrame creation", exc_info=exc)
data = {col: arr for col, arr in zip(columns, arrays)}
return DataFrame(data)
return _fallback_create_df(arrays, columns)


def _fallback_create_df(
arrays: List[np.ndarray],
columns: list
) -> DataFrame:
data = {col: arr for col, arr in zip(columns, arrays)}
return DataFrame(data)


def _fallback_if_unsupported(func):
if not CREATE_DF_FAST:
return _fallback_create_df
else:
return func


@_fallback_if_unsupported
def _unsafe_create_df_fast(
arrays: List[np.ndarray],
columns: list
Expand Down

0 comments on commit 1b7fe9c

Please sign in to comment.