Skip to content

Commit

Permalink
BUG: fix inspect usage when pyarrow or jinja2 is not installed (#60196)
Browse files Browse the repository at this point in the history
* BUG: fix inspect usage when pyarrow or jinja2 is not installed

* add whatsnew note
  • Loading branch information
jorisvandenbossche authored Nov 5, 2024
1 parent e449b49 commit 169b00e
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 @@ -173,7 +173,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) -> None:
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 @@ -1397,6 +1397,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 @@ -376,6 +376,5 @@ def test_constructor_expanddim(self):

def test_inspect_getmembers(self):
# GH38740
pytest.importorskip("jinja2")
df = DataFrame()
inspect.getmembers(df)
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 @@ -164,12 +160,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)
inspect.getmembers(ser)

Expand Down

0 comments on commit 169b00e

Please sign in to comment.