Skip to content

Commit 8f953a5

Browse files
committed
API: Add pandas.api.typing
1 parent d1b7244 commit 8f953a5

File tree

6 files changed

+72
-19
lines changed

6 files changed

+72
-19
lines changed

doc/source/reference/index.rst

+18-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,24 @@ API reference
99
This page gives an overview of all public pandas objects, functions and
1010
methods. All classes and functions exposed in ``pandas.*`` namespace are public.
1111

12-
Some subpackages are public which include ``pandas.errors``,
13-
``pandas.plotting``, and ``pandas.testing``. Public functions in
14-
``pandas.io`` and ``pandas.tseries`` submodules are mentioned in
15-
the documentation. ``pandas.api.types`` subpackage holds some
16-
public functions related to data types in pandas.
12+
The following subpackages are public.
13+
14+
- ``pandas.errors``: Custom exception and warnings classes that are raised by pandas.
15+
- ``pandas.plotting``: Plotting public API.
16+
- ``pandas.testing``: Functions that are useful for writing tests involving pandas objects.
17+
- ``pandas.api.extensions``: Functions and classes for extending pandas objects.
18+
- ``pandas.api.indexers``: Functions and classes for rolling window indexers.
19+
- ``pandas.api.interchange``: DataFrame interchange protocol.
20+
- ``pandas.api.types``: Datatype classes and functions.
21+
- ``pandas.api.typing``: Classes that may be necessary for type-hinting. These are
22+
classes that are encountered as intermediates results but should not be
23+
instantiated directly by users. These classes are not to be confused with
24+
classes from the `pandas-stubs <https://github.com/pandas-dev/pandas-stubs>`_
25+
package which has classes in addition to those that occur in pandas for type-hinting.
26+
27+
In addition, public functions in ``pandas.io`` and ``pandas.tseries`` submodules
28+
are mentioned in the documentation.
29+
1730

1831
.. warning::
1932

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ Other API changes
600600
- Disallow computing ``cumprod`` for :class:`Timedelta` object; previously this returned incorrect values (:issue:`50246`)
601601
- Loading a JSON file with duplicate columns using ``read_json(orient='split')`` renames columns to avoid duplicates, as :func:`read_csv` and the other readers do (:issue:`50370`)
602602
- :func:`to_datetime` with ``unit`` of either "Y" or "M" will now raise if a sequence contains a non-round ``float`` value, matching the ``Timestamp`` behavior (:issue:`50301`)
603+
- Classes that are useful for type-hinting have been added to the public API in the new submodule ``pandas.api.typing`` (:issue:`48577`)
603604
-
604605

605606
.. ---------------------------------------------------------------------------

pandas/api/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
indexers,
55
interchange,
66
types,
7+
typing,
78
)
89

910
__all__ = [
1011
"interchange",
1112
"extensions",
1213
"indexers",
1314
"types",
15+
"typing",
1416
]

pandas/api/typing/__init__.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
Public API classes that store intermediate results useful for type-hinting.
3+
"""
4+
5+
from pandas.core.groupby import (
6+
DataFrameGroupBy,
7+
SeriesGroupBy,
8+
)
9+
from pandas.core.resample import (
10+
Resampler,
11+
TimeGrouper,
12+
)
13+
from pandas.core.window import (
14+
Expanding,
15+
ExponentialMovingWindow,
16+
Rolling,
17+
Window,
18+
)
19+
20+
# TODO: Can't import Styler without impacting registered matplotlib converters
21+
# from pandas.io.formats.style import Styler
22+
from pandas.io.json._json import JsonReader
23+
from pandas.io.stata import (
24+
StataReader,
25+
StataWriter,
26+
)
27+
28+
__all__ = [
29+
"DataFrameGroupBy",
30+
"Expanding",
31+
"ExponentialMovingWindow",
32+
"JsonReader",
33+
"Resampler",
34+
"Rolling",
35+
"SeriesGroupBy",
36+
"StataWriter",
37+
"StataReader",
38+
# See TODO above
39+
# "Styler",
40+
"TimeGrouper",
41+
"Window",
42+
]

pandas/io/formats/style.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@
5252
from pandas.core.shared_docs import _shared_docs
5353

5454
from pandas.io.formats.format import save_to_buffer
55-
56-
jinja2 = import_optional_dependency("jinja2", extra="DataFrame.style requires jinja2.")
57-
5855
from pandas.io.formats.style_render import (
5956
CSSProperties,
6057
CSSStyles,
@@ -71,20 +68,15 @@
7168
if TYPE_CHECKING:
7269
from matplotlib.colors import Colormap
7370

74-
try:
75-
import matplotlib as mpl
76-
import matplotlib.pyplot as plt
77-
78-
has_mpl = True
79-
except ImportError:
80-
has_mpl = False
81-
8271

8372
@contextmanager
8473
def _mpl(func: Callable) -> Generator[tuple[Any, Any], None, None]:
85-
if has_mpl:
74+
try:
75+
import matplotlib as mpl
76+
import matplotlib.pyplot as plt
77+
8678
yield plt, mpl
87-
else:
79+
except ImportError:
8880
raise ImportError(f"{func.__name__} requires matplotlib.")
8981

9082

@@ -3420,6 +3412,9 @@ def from_custom_template(
34203412
Has the correct ``env``,``template_html``, ``template_html_table`` and
34213413
``template_html_style`` class attributes set.
34223414
"""
3415+
jinja2 = import_optional_dependency(
3416+
"jinja2", extra="DataFrame.style requires jinja2."
3417+
)
34233418
loader = jinja2.ChoiceLoader([jinja2.FileSystemLoader(searchpath), cls.loader])
34243419

34253420
# mypy doesn't like dynamically-defined classes

pandas/tests/api/test_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def test_depr(self):
235235

236236

237237
class TestApi(Base):
238-
allowed = ["types", "extensions", "indexers", "interchange"]
238+
allowed = ["types", "extensions", "indexers", "interchange", "typing"]
239239

240240
def test_api(self):
241241
self.check(api, self.allowed)

0 commit comments

Comments
 (0)