Skip to content

Commit f16179a

Browse files
authored
inspect-safety for DataFrame._constructor_expanddim (#33628)
1 parent 6963b01 commit f16179a

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

pandas/core/frame.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,12 @@ def _constructor(self) -> Type["DataFrame"]:
422422

423423
@property
424424
def _constructor_expanddim(self):
425-
raise NotImplementedError("Not supported for DataFrames!")
425+
# GH#31549 raising NotImplementedError on a property causes trouble
426+
# for `inspect`
427+
def constructor(*args, **kwargs):
428+
raise NotImplementedError("Not supported for DataFrames!")
429+
430+
return constructor
426431

427432
# ----------------------------------------------------------------------
428433
# Constructors

pandas/tests/frame/test_api.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from copy import deepcopy
22
import datetime
3+
import inspect
34
import pydoc
45

56
import numpy as np
67
import pytest
78

89
from pandas.compat import PY37
9-
from pandas.util._test_decorators import async_mark
10+
from pandas.util._test_decorators import async_mark, skip_if_no
1011

1112
import pandas as pd
1213
from pandas import Categorical, DataFrame, Series, compat, date_range, timedelta_range
@@ -569,3 +570,14 @@ def test_cache_on_copy(self):
569570

570571
assert df["a"].values[0] == -1
571572
tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0], "y": [0]}))
573+
574+
@skip_if_no("jinja2")
575+
def test_constructor_expanddim_lookup(self):
576+
# GH#33628 accessing _constructor_expanddim should not
577+
# raise NotImplementedError
578+
df = DataFrame()
579+
580+
inspect.getmembers(df)
581+
582+
with pytest.raises(NotImplementedError, match="Not supported for DataFrames!"):
583+
df._constructor_expanddim(np.arange(27).reshape(3, 3, 3))

0 commit comments

Comments
 (0)