From b40c726bdf65c1a4949af42a362a3d47e4e11e36 Mon Sep 17 00:00:00 2001 From: Richard Bruskiewich Date: Sat, 1 Feb 2020 11:50:09 -0800 Subject: [PATCH 1/5] replace NotImplementedError with AttributeError Fixes Python inspection of members - bug reported in https://github.com/pandas-dev/pandas/issues/31474 --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 0dea8235e9d3f..6b832e903b626 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -411,7 +411,7 @@ def _constructor(self) -> Type["DataFrame"]: @property def _constructor_expanddim(self): - raise NotImplementedError("Not supported for DataFrames!") + raise AttributeError("Property constructor_expanddim is not supported for DataFrames!") # ---------------------------------------------------------------------- # Constructors From 44ec4a330519f787f2d7132a510a332022dbf986 Mon Sep 17 00:00:00 2001 From: Richard Bruskiewich Date: Sat, 1 Feb 2020 11:54:58 -0800 Subject: [PATCH 2/5] fix line length --- pandas/core/frame.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 6b832e903b626..bc15c8d75e5c5 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -411,7 +411,9 @@ def _constructor(self) -> Type["DataFrame"]: @property def _constructor_expanddim(self): - raise AttributeError("Property constructor_expanddim is not supported for DataFrames!") + raise AttributeError( + "Property 'constructor_expanddim' is not supported for DataFrames!" + ) # ---------------------------------------------------------------------- # Constructors From b4984e021fbd7a4f09c274dbd6c1fb07c1493e6b Mon Sep 17 00:00:00 2001 From: Richard Bruskiewich Date: Sat, 1 Feb 2020 17:28:31 -0800 Subject: [PATCH 3/5] DataFrame exception Local DataFrame Exception inherits from multiple Exception targets to satisfy several use case failure modes --- pandas/core/frame.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index bc15c8d75e5c5..4a18b5fa883e0 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -132,6 +132,10 @@ from pandas.io.formats.printing import pprint_thing import pandas.plotting +# Customized "cover all bases" local Exception +class DataFrameError(NotImplementedError, TypeError, AttributeError): + pass + if TYPE_CHECKING: from pandas.core.groupby.generic import DataFrameGroupBy from pandas.io.formats.style import Styler @@ -411,7 +415,7 @@ def _constructor(self) -> Type["DataFrame"]: @property def _constructor_expanddim(self): - raise AttributeError( + raise DataFrameError( "Property 'constructor_expanddim' is not supported for DataFrames!" ) From 33fa7123a2235bbd71efd6778328bff17dc78706 Mon Sep 17 00:00:00 2001 From: Richard Bruskiewich Date: Sat, 1 Feb 2020 17:31:06 -0800 Subject: [PATCH 4/5] PEP-8 blank line added --- pandas/core/frame.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 4a18b5fa883e0..1bcd1c5fcf4eb 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -132,10 +132,12 @@ from pandas.io.formats.printing import pprint_thing import pandas.plotting + # Customized "cover all bases" local Exception class DataFrameError(NotImplementedError, TypeError, AttributeError): pass + if TYPE_CHECKING: from pandas.core.groupby.generic import DataFrameGroupBy from pandas.io.formats.style import Styler From ab28184d33f2cb0722e8685bd8806f51d81a562c Mon Sep 17 00:00:00 2001 From: Richard Bruskiewich Date: Mon, 20 Apr 2020 18:39:50 -0700 Subject: [PATCH 5/5] Adding test for inspect.getmembers(pd) --- pandas/tests/frame/test_subclass.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/tests/frame/test_subclass.py b/pandas/tests/frame/test_subclass.py index a2e7dc527c4b8..9eda5f4504a75 100644 --- a/pandas/tests/frame/test_subclass.py +++ b/pandas/tests/frame/test_subclass.py @@ -5,8 +5,14 @@ from pandas import DataFrame, Index, MultiIndex, Series import pandas._testing as tm +import inspect + class TestDataFrameSubclassing: + + def test_get_members(): + inspect.getmembers(pd.DataFrame()) + def test_frame_subclassing_and_slicing(self): # Subclass frame and ensure it returns the right class on slicing it # In reference to PR 9632