Skip to content

Commit

Permalink
Backport PR pandas-dev#60196: BUG: fix inspect usage when pyarrow or …
Browse files Browse the repository at this point in the history
…jinja2 is not installed
  • Loading branch information
jorisvandenbossche authored and meeseeksmachine committed Nov 5, 2024
1 parent a53604d commit 601d3f3
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 11 deletions.
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v2.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ Styler

Other
^^^^^
-
- Fixed usage of ``inspect`` when the optional dependencies ``pyarrow`` or ``jinja2``
are not installed (:issue:`60196`)
-

.. ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/arrow/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _is_valid_pyarrow_dtype(self, pyarrow_dtype) -> bool:

def _validate(self, data):
dtype = data.dtype
if not isinstance(dtype, ArrowDtype):
if pa_version_under10p1 or not isinstance(dtype, ArrowDtype):
# Raise AttributeError so that inspect can handle non-struct Series.
raise AttributeError(self._validation_msg.format(dtype=dtype))

Expand Down
5 changes: 5 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,11 @@ def style(self) -> Styler:
Please see
`Table Visualization <../../user_guide/style.ipynb>`_ for more examples.
"""
# Raise AttributeError so that inspect works even if jinja2 is not installed.
has_jinja2 = import_optional_dependency("jinja2", errors="ignore")
if not has_jinja2:
raise AttributeError("The '.style' accessor requires jinja2")

from pandas.io.formats.style import Styler

return Styler(self)
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/frame/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ def test_constructor_expanddim(self):

def test_inspect_getmembers(self):
# GH38740
pytest.importorskip("jinja2")
df = DataFrame()
msg = "DataFrame._data is deprecated"
with tm.assert_produces_warning(
Expand Down
8 changes: 0 additions & 8 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import numpy as np
import pytest

from pandas._config import using_string_dtype

from pandas.compat import HAS_PYARROW

import pandas as pd
from pandas import (
DataFrame,
Expand Down Expand Up @@ -171,12 +167,8 @@ def test_attrs(self):
result = s + 1
assert result.attrs == {"version": 1}

@pytest.mark.xfail(
using_string_dtype() and not HAS_PYARROW, reason="TODO(infer_string)"
)
def test_inspect_getmembers(self):
# GH38782
pytest.importorskip("jinja2")
ser = Series(dtype=object)
msg = "Series._data is deprecated"
with tm.assert_produces_warning(
Expand Down

0 comments on commit 601d3f3

Please sign in to comment.