Skip to content

Typ: Fix io stata type ignores #46449

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
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
21 changes: 11 additions & 10 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,13 @@ def _cast_to_stata_types(data: DataFrame) -> DataFrame:
"""
ws = ""
# original, if small, if large
conversion_data = (
conversion_data: tuple[
tuple[type, type, type],
tuple[type, type, type],
tuple[type, type, type],
tuple[type, type, type],
tuple[type, type, type],
] = (
(np.bool_, np.int8, np.int8),
(np.uint8, np.int8, np.int16),
(np.uint16, np.int16, np.int32),
Expand Down Expand Up @@ -600,8 +606,7 @@ def _cast_to_stata_types(data: DataFrame) -> DataFrame:
dtype = data[col].dtype
for c_data in conversion_data:
if dtype == c_data[0]:
# Value of type variable "_IntType" of "iinfo" cannot be "object"
if data[col].max() <= np.iinfo(c_data[1]).max: # type: ignore[type-var]
if data[col].max() <= np.iinfo(c_data[1]).max:
dtype = c_data[1]
else:
dtype = c_data[2]
Expand Down Expand Up @@ -967,17 +972,15 @@ def __init__(self) -> None:
(255, np.dtype(np.float64)),
]
)
self.DTYPE_MAP_XML = {
self.DTYPE_MAP_XML: dict[int, np.dtype] = {
32768: np.dtype(np.uint8), # Keys to GSO
65526: np.dtype(np.float64),
65527: np.dtype(np.float32),
65528: np.dtype(np.int32),
65529: np.dtype(np.int16),
65530: np.dtype(np.int8),
}
# error: Argument 1 to "list" has incompatible type "str";
# expected "Iterable[int]" [arg-type]
self.TYPE_MAP = list(range(251)) + list("bhlfd") # type: ignore[arg-type]
self.TYPE_MAP = list(tuple(range(251)) + tuple("bhlfd"))
self.TYPE_MAP_XML = {
# Not really a Q, unclear how to handle byteswap
32768: "Q",
Expand Down Expand Up @@ -1296,9 +1299,7 @@ def g(typ: int) -> str | np.dtype:
if typ <= 2045:
return str(typ)
try:
# error: Incompatible return value type (got "Type[number]", expected
# "Union[str, dtype]")
return self.DTYPE_MAP_XML[typ] # type: ignore[return-value]
return self.DTYPE_MAP_XML[typ]
except KeyError as err:
raise ValueError(f"cannot convert stata dtype [{typ}]") from err

Expand Down