Skip to content

Commit

Permalink
refactor: Refactor pandas option usage for backward compatibility
Browse files Browse the repository at this point in the history
- Introduce the `optional_option_context` helper to replace the direct
  use of `pd.option_context("future.no_silent_downcasting", True)`,
  ensuring compatibility with older pandas versions that lack this
  option.
- Update the `future.infer_string` test to run only on pandas >= 2.1,
  where it is applicable.
  • Loading branch information
ssiegel committed Feb 1, 2025
1 parent 37cc39f commit 8a2a827
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ydata_profiling/model/pandas/summary_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ydata_profiling.model.summarizer import BaseSummarizer
from ydata_profiling.model.summary import describe_1d, get_series_descriptions
from ydata_profiling.model.typeset import ProfilingTypeSet
from ydata_profiling.utils.compat import optional_option_context
from ydata_profiling.utils.dataframe import sort_column_names


Expand Down Expand Up @@ -44,7 +45,7 @@ def pandas_describe_1d(
"""

# Make sure pd.NA is not in the series
with pd.option_context("future.no_silent_downcasting", True):
with optional_option_context("future.no_silent_downcasting", True):
series = series.fillna(np.nan)

has_cast_type = _is_cast_type_defined(typeset, series.name)
Expand Down
15 changes: 15 additions & 0 deletions src/ydata_profiling/utils/compat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Utility functions for (version) compatibility"""

from contextlib import contextmanager
from functools import lru_cache
from typing import Tuple

Expand All @@ -12,3 +14,16 @@ def pandas_version_info() -> Tuple[int, ...]:
akin to `sys.version_info` for the Python version.
"""
return tuple(int(s) for s in pd.__version__.split("."))


@contextmanager
def optional_option_context(option_key, value):
"""
A context manager that sets an option only if it is available in the
current pandas version; otherwise, it is a no-op.
"""
try:
with pd.option_context(option_key, value):
yield
except pd.errors.OptionError:
yield
4 changes: 4 additions & 0 deletions tests/unit/test_pd_future_infer_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from ydata_profiling import ProfileReport
from ydata_profiling.utils.compat import pandas_version_info


@pytest.fixture()
Expand All @@ -15,6 +16,9 @@ def df():
return df


@pytest.mark.skipif(
pandas_version_info() < (2, 1, 0), reason="requires pandas 2.1 or higher"
)
def test_pd_future_infer_string(df: pd.DataFrame):
with pd.option_context("future.infer_string", True):
profile_report = ProfileReport(df, title="Test Report", progress_bar=False)
Expand Down

0 comments on commit 8a2a827

Please sign in to comment.