Skip to content

Commit 169b00e

Browse files
BUG: fix inspect usage when pyarrow or jinja2 is not installed (#60196)
* BUG: fix inspect usage when pyarrow or jinja2 is not installed * add whatsnew note
1 parent e449b49 commit 169b00e

File tree

5 files changed

+8
-11
lines changed

5 files changed

+8
-11
lines changed

doc/source/whatsnew/v2.3.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ Styler
173173

174174
Other
175175
^^^^^
176-
-
176+
- Fixed usage of ``inspect`` when the optional dependencies ``pyarrow`` or ``jinja2``
177+
are not installed (:issue:`60196`)
177178
-
178179

179180
.. ---------------------------------------------------------------------------

pandas/core/arrays/arrow/accessors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def _is_valid_pyarrow_dtype(self, pyarrow_dtype) -> bool:
4646

4747
def _validate(self, data) -> None:
4848
dtype = data.dtype
49-
if not isinstance(dtype, ArrowDtype):
49+
if pa_version_under10p1 or not isinstance(dtype, ArrowDtype):
5050
# Raise AttributeError so that inspect can handle non-struct Series.
5151
raise AttributeError(self._validation_msg.format(dtype=dtype))
5252

pandas/core/frame.py

+5
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,11 @@ def style(self) -> Styler:
13971397
Please see
13981398
`Table Visualization <../../user_guide/style.ipynb>`_ for more examples.
13991399
"""
1400+
# Raise AttributeError so that inspect works even if jinja2 is not installed.
1401+
has_jinja2 = import_optional_dependency("jinja2", errors="ignore")
1402+
if not has_jinja2:
1403+
raise AttributeError("The '.style' accessor requires jinja2")
1404+
14001405
from pandas.io.formats.style import Styler
14011406

14021407
return Styler(self)

pandas/tests/frame/test_api.py

-1
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,5 @@ def test_constructor_expanddim(self):
376376

377377
def test_inspect_getmembers(self):
378378
# GH38740
379-
pytest.importorskip("jinja2")
380379
df = DataFrame()
381380
inspect.getmembers(df)

pandas/tests/series/test_api.py

-8
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
import numpy as np
55
import pytest
66

7-
from pandas._config import using_string_dtype
8-
9-
from pandas.compat import HAS_PYARROW
10-
117
import pandas as pd
128
from pandas import (
139
DataFrame,
@@ -164,12 +160,8 @@ def test_attrs(self):
164160
result = s + 1
165161
assert result.attrs == {"version": 1}
166162

167-
@pytest.mark.xfail(
168-
using_string_dtype() and not HAS_PYARROW, reason="TODO(infer_string)"
169-
)
170163
def test_inspect_getmembers(self):
171164
# GH38782
172-
pytest.importorskip("jinja2")
173165
ser = Series(dtype=object)
174166
inspect.getmembers(ser)
175167

0 commit comments

Comments
 (0)