Skip to content

TYP: export SASReader in pandas.api.typing #58349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ enhancement2
Other enhancements
^^^^^^^^^^^^^^^^^^
- :class:`pandas.api.typing.FrozenList` is available for typing the outputs of :attr:`MultiIndex.names`, :attr:`MultiIndex.codes` and :attr:`MultiIndex.levels` (:issue:`58237`)
- :class:`pandas.api.typing.SASReader` is available for typing the output of :func:`read_sas` (:issue:`55689`)
- :func:`DataFrame.to_excel` now raises an ``UserWarning`` when the character count in a cell exceeds Excel's limitation of 32767 characters (:issue:`56954`)
- :func:`read_stata` now returns ``datetime64`` resolutions better matching those natively stored in the stata format (:issue:`55642`)
- :meth:`Styler.set_tooltips` provides alternative method to storing tooltips by using title attribute of td elements. (:issue:`56981`)
Expand Down
2 changes: 2 additions & 0 deletions pandas/api/typing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
# TODO: Can't import Styler without importing jinja2
# from pandas.io.formats.style import Styler
from pandas.io.json._json import JsonReader
from pandas.io.sas.sasreader import SASReader
from pandas.io.stata import StataReader

__all__ = [
Expand All @@ -49,6 +50,7 @@
"RollingGroupby",
"SeriesGroupBy",
"StataReader",
"SASReader",
# See TODO above
# "Styler",
"TimedeltaIndexResamplerGroupby",
Expand Down
5 changes: 2 additions & 3 deletions pandas/io/sas/sas7bdat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from __future__ import annotations

from collections import abc
from datetime import datetime
import sys
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -45,7 +44,7 @@

from pandas.io.common import get_handle
import pandas.io.sas.sas_constants as const
from pandas.io.sas.sasreader import ReaderBase
from pandas.io.sas.sasreader import SASReader

if TYPE_CHECKING:
from pandas._typing import (
Expand Down Expand Up @@ -116,7 +115,7 @@ def __init__(


# SAS7BDAT represents a SAS data file in SAS7BDAT format.
class SAS7BDATReader(ReaderBase, abc.Iterator):
class SAS7BDATReader(SASReader):
"""
Read SAS files in SAS7BDAT format.

Expand Down
5 changes: 2 additions & 3 deletions pandas/io/sas/sas_xport.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from __future__ import annotations

from collections import abc
from datetime import datetime
import struct
from typing import TYPE_CHECKING
Expand All @@ -24,7 +23,7 @@
import pandas as pd

from pandas.io.common import get_handle
from pandas.io.sas.sasreader import ReaderBase
from pandas.io.sas.sasreader import SASReader

if TYPE_CHECKING:
from pandas._typing import (
Expand Down Expand Up @@ -252,7 +251,7 @@ def _parse_float_vec(vec):
return ieee


class XportReader(ReaderBase, abc.Iterator):
class XportReader(SASReader):
__doc__ = _xport_reader_doc

def __init__(
Expand Down
13 changes: 7 additions & 6 deletions pandas/io/sas/sasreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
ABC,
abstractmethod,
)
from collections.abc import Iterator
from typing import (
TYPE_CHECKING,
overload,
Expand All @@ -33,9 +34,9 @@
from pandas import DataFrame


class ReaderBase(ABC):
class SASReader(Iterator["DataFrame"], ABC):
"""
Protocol for XportReader and SAS7BDATReader classes.
Abstract class for XportReader and SAS7BDATReader.
"""

@abstractmethod
Expand Down Expand Up @@ -66,7 +67,7 @@ def read_sas(
chunksize: int = ...,
iterator: bool = ...,
compression: CompressionOptions = ...,
) -> ReaderBase: ...
) -> SASReader: ...


@overload
Expand All @@ -79,7 +80,7 @@ def read_sas(
chunksize: None = ...,
iterator: bool = ...,
compression: CompressionOptions = ...,
) -> DataFrame | ReaderBase: ...
) -> DataFrame | SASReader: ...


@doc(decompression_options=_shared_docs["decompression_options"] % "filepath_or_buffer")
Expand All @@ -92,7 +93,7 @@ def read_sas(
chunksize: int | None = None,
iterator: bool = False,
compression: CompressionOptions = "infer",
) -> DataFrame | ReaderBase:
) -> DataFrame | SASReader:
"""
Read SAS files stored as either XPORT or SAS7BDAT format files.

Expand Down Expand Up @@ -145,7 +146,7 @@ def read_sas(
f"unable to infer format of SAS file from filename: {fname!r}"
)

reader: ReaderBase
reader: SASReader
if format.lower() == "xport":
from pandas.io.sas.sas_xport import XportReader

Expand Down
1 change: 1 addition & 0 deletions pandas/tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ class TestApi(Base):
"RollingGroupby",
"SeriesGroupBy",
"StataReader",
"SASReader",
"TimedeltaIndexResamplerGroupby",
"TimeGrouper",
"Window",
Expand Down
Loading