Skip to content

Commit 22cc611

Browse files
aimlnerdjorisvandenbossche
authored andcommitted
ENH: set __module__ for objects in pandas pd.DataFrame API (pandas-dev#55171)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent 06f305d commit 22cc611

File tree

14 files changed

+43
-16
lines changed

14 files changed

+43
-16
lines changed

doc/source/development/contributing_docstring.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ Finally, docstrings can also be appended to with the ``doc`` decorator.
940940

941941
In this example, we'll create a parent docstring normally (this is like
942942
``pandas.core.generic.NDFrame``). Then we'll have two children (like
943-
``pandas.core.series.Series`` and ``pandas.core.frame.DataFrame``). We'll
943+
``pandas.core.series.Series`` and ``pandas.DataFrame``). We'll
944944
substitute the class names in this docstring.
945945

946946
.. code-block:: python

doc/source/user_guide/enhancingperf.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ by evaluate arithmetic and boolean expression all at once for large :class:`~pan
453453
:func:`~pandas.eval` is many orders of magnitude slower for
454454
smaller expressions or objects than plain Python. A good rule of thumb is
455455
to only use :func:`~pandas.eval` when you have a
456-
:class:`.DataFrame` with more than 10,000 rows.
456+
:class:`~pandas.core.frame.DataFrame` with more than 10,000 rows.
457457

458458
Supported syntax
459459
~~~~~~~~~~~~~~~~

doc/source/user_guide/io.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -6400,7 +6400,7 @@ ignored.
64006400
In [2]: df = pd.DataFrame({'A': np.random.randn(sz), 'B': [1] * sz})
64016401
64026402
In [3]: df.info()
6403-
<class 'pandas.core.frame.DataFrame'>
6403+
<class 'pandas.DataFrame'>
64046404
RangeIndex: 1000000 entries, 0 to 999999
64056405
Data columns (total 2 columns):
64066406
A 1000000 non-null float64

doc/source/whatsnew/v0.24.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ then all the columns are dummy-encoded, and a :class:`SparseDataFrame` was retur
840840
In [2]: df = pd.DataFrame({"A": [1, 2], "B": ['a', 'b'], "C": ['a', 'a']})
841841
842842
In [3]: type(pd.get_dummies(df, sparse=True))
843-
Out[3]: pandas.core.frame.DataFrame
843+
Out[3]: pandas.DataFrame
844844
845845
In [4]: type(pd.get_dummies(df[['B', 'C']], sparse=True))
846846
Out[4]: pandas.core.sparse.frame.SparseDataFrame

doc/source/whatsnew/v1.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ Extended verbose info output for :class:`~pandas.DataFrame`
414414
... "text_col": ["a", "b", "c"],
415415
... "float_col": [0.0, 0.1, 0.2]})
416416
In [2]: df.info(verbose=True)
417-
<class 'pandas.core.frame.DataFrame'>
417+
<class 'pandas.DataFrame'>
418418
RangeIndex: 3 entries, 0 to 2
419419
Data columns (total 3 columns):
420420
int_col 3 non-null int64

pandas/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def ignore_doctest_warning(item: pytest.Item, path: str, message: str) -> None:
125125
item : pytest.Item
126126
pytest test item.
127127
path : str
128-
Module path to Python object, e.g. "pandas.core.frame.DataFrame.append". A
128+
Module path to Python object, e.g. "pandas.DataFrame.append". A
129129
warning will be filtered when item.name ends with in given path. So it is
130130
sufficient to specify e.g. "DataFrame.append".
131131
message : str

pandas/core/frame.py

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
Appender,
6666
Substitution,
6767
doc,
68+
set_module,
6869
)
6970
from pandas.util._exceptions import (
7071
find_stack_level,
@@ -498,6 +499,7 @@
498499
# DataFrame class
499500

500501

502+
@set_module("pandas")
501503
class DataFrame(NDFrame, OpsMixin):
502504
"""
503505
Two-dimensional, size-mutable, potentially heterogeneous tabular data.

pandas/core/indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def iloc(self) -> _iLocIndexer:
227227
a b c d
228228
0 1 2 3 4
229229
>>> type(df.iloc[[0]])
230-
<class 'pandas.core.frame.DataFrame'>
230+
<class 'pandas.DataFrame'>
231231
232232
>>> df.iloc[[0, 1]]
233233
a b c d

pandas/io/formats/format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ class DataFrameRenderer:
855855
- to_csv
856856
- to_latex
857857
858-
Called in pandas.core.frame.DataFrame:
858+
Called in pandas.DataFrame:
859859
- to_html
860860
- to_string
861861

pandas/io/formats/info.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
Prints information of all columns:
7373
7474
>>> df.info(verbose=True)
75-
<class 'pandas.core.frame.DataFrame'>
75+
<class 'pandas.DataFrame'>
7676
RangeIndex: 5 entries, 0 to 4
7777
Data columns (total 3 columns):
7878
# Column Non-Null Count Dtype
@@ -87,7 +87,7 @@
8787
information:
8888
8989
>>> df.info(verbose=False)
90-
<class 'pandas.core.frame.DataFrame'>
90+
<class 'pandas.DataFrame'>
9191
RangeIndex: 5 entries, 0 to 4
9292
Columns: 3 entries, int_col to float_col
9393
dtypes: float64(1), int64(1), object(1)
@@ -115,7 +115,7 @@
115115
... 'column_3': np.random.choice(['a', 'b', 'c'], 10 ** 6)
116116
... })
117117
>>> df.info()
118-
<class 'pandas.core.frame.DataFrame'>
118+
<class 'pandas.DataFrame'>
119119
RangeIndex: 1000000 entries, 0 to 999999
120120
Data columns (total 3 columns):
121121
# Column Non-Null Count Dtype
@@ -127,7 +127,7 @@
127127
memory usage: 22.9+ MB
128128
129129
>>> df.info(memory_usage='deep')
130-
<class 'pandas.core.frame.DataFrame'>
130+
<class 'pandas.DataFrame'>
131131
RangeIndex: 1000000 entries, 0 to 999999
132132
Data columns (total 3 columns):
133133
# Column Non-Null Count Dtype

pandas/tests/api/test_api.py

+4
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,7 @@ def test_pandas_array_alias():
401401
res = pd.arrays.PandasArray
402402

403403
assert res is pd.arrays.NumpyExtensionArray
404+
405+
406+
def test_set_module():
407+
assert pd.DataFrame.__module__ == "pandas"

pandas/tests/frame/methods/test_info.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_info_empty():
4040
result = buf.getvalue()
4141
expected = textwrap.dedent(
4242
"""\
43-
<class 'pandas.core.frame.DataFrame'>
43+
<class 'pandas.DataFrame'>
4444
RangeIndex: 0 entries
4545
Empty DataFrame\n"""
4646
)
@@ -208,7 +208,7 @@ def test_info_memory():
208208
bytes = float(df.memory_usage().sum())
209209
expected = textwrap.dedent(
210210
f"""\
211-
<class 'pandas.core.frame.DataFrame'>
211+
<class 'pandas.DataFrame'>
212212
RangeIndex: 2 entries, 0 to 1
213213
Data columns (total 1 columns):
214214
# Column Non-Null Count Dtype
@@ -501,7 +501,7 @@ def test_info_int_columns():
501501
result = buf.getvalue()
502502
expected = textwrap.dedent(
503503
"""\
504-
<class 'pandas.core.frame.DataFrame'>
504+
<class 'pandas.DataFrame'>
505505
Index: 2 entries, A to B
506506
Data columns (total 2 columns):
507507
# Column Non-Null Count Dtype

pandas/tests/groupby/test_grouping.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def test_groupby_with_datetime_key(self):
509509
assert len(gb.groups.keys()) == 4
510510

511511
def test_grouping_error_on_multidim_input(self, df):
512-
msg = "Grouper for '<class 'pandas.core.frame.DataFrame'>' not 1-dimensional"
512+
msg = "Grouper for '<class 'pandas.DataFrame'>' not 1-dimensional"
513513
with pytest.raises(ValueError, match=msg):
514514
Grouping(df.index, df[["A", "A"]])
515515

pandas/util/_decorators.py

+21
Original file line numberDiff line numberDiff line change
@@ -503,3 +503,24 @@ def indent(text: str | None, indents: int = 1) -> str:
503503
"future_version_msg",
504504
"Substitution",
505505
]
506+
507+
508+
def set_module(module):
509+
"""Private decorator for overriding __module__ on a function or class.
510+
511+
Example usage::
512+
513+
@set_module("pandas")
514+
def example():
515+
pass
516+
517+
518+
assert example.__module__ == "pandas"
519+
"""
520+
521+
def decorator(func):
522+
if module is not None:
523+
func.__module__ = module
524+
return func
525+
526+
return decorator

0 commit comments

Comments
 (0)