Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support Pandas future.infer_string=True in report generation #1674

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


def get_character_counts_vc(vc: pd.Series) -> pd.Series:
series = pd.Series(vc.index, index=vc)
series = pd.Series(vc.index, index=vc, dtype=object)
characters = series[series != ""].apply(list)
characters = characters.explode()

Expand Down Expand Up @@ -169,7 +169,7 @@ def word_summary_vc(vc: pd.Series, stop_words: List[str] = []) -> dict:
# TODO: configurable lowercase/punctuation etc.
# TODO: remove punctuation in words

series = pd.Series(vc.index, index=vc)
series = pd.Series(vc.index, index=vc, dtype=object)
word_lists = series.str.lower().str.split()
words = word_lists.explode().str.strip(string.punctuation + string.whitespace)
word_counts = pd.Series(words.index, index=words)
Expand All @@ -187,7 +187,7 @@ def word_summary_vc(vc: pd.Series, stop_words: List[str] = []) -> dict:


def length_summary_vc(vc: pd.Series) -> dict:
series = pd.Series(vc.index, index=vc)
series = pd.Series(vc.index, index=vc, dtype=object)
length = series.str.len()
length_counts = pd.Series(length.index, index=length)
length_counts = length_counts.groupby(level=0, sort=False).sum()
Expand Down
4 changes: 3 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,8 @@ def pandas_describe_1d(
"""

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

has_cast_type = _is_cast_type_defined(typeset, series.name)
cast_type = str(typeset.type_schema[series.name]) if has_cast_type else None
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
25 changes: 25 additions & 0 deletions tests/unit/test_pd_future_infer_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pandas as pd
import pytest

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


@pytest.fixture()
def df():
df = pd.DataFrame(
{
"foo": [1, 2, 3],
"bar": ["", "", ""],
}
)
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)
assert len(profile_report.to_html()) > 0