Skip to content

Commit b54aaf7

Browse files
bashtagejreback
authored andcommitted
CLN/MAINT: Clean and annotate stata reader and writers (#31072)
1 parent 8b4f973 commit b54aaf7

File tree

4 files changed

+426
-373
lines changed

4 files changed

+426
-373
lines changed

pandas/core/frame.py

+20-15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import collections
1313
from collections import abc
14+
import datetime
1415
from io import StringIO
1516
import itertools
1617
import sys
@@ -19,6 +20,7 @@
1920
IO,
2021
TYPE_CHECKING,
2122
Any,
23+
Dict,
2224
FrozenSet,
2325
Hashable,
2426
Iterable,
@@ -39,7 +41,7 @@
3941
from pandas._config import get_option
4042

4143
from pandas._libs import algos as libalgos, lib, properties
42-
from pandas._typing import Axes, Axis, Dtype, FilePathOrBuffer, Level, Renamer
44+
from pandas._typing import Axes, Axis, Dtype, FilePathOrBuffer, Label, Level, Renamer
4345
from pandas.compat import PY37
4446
from pandas.compat._optional import import_optional_dependency
4547
from pandas.compat.numpy import function as nv
@@ -1851,16 +1853,16 @@ def _from_arrays(cls, arrays, columns, index, dtype=None) -> "DataFrame":
18511853
@deprecate_kwarg(old_arg_name="fname", new_arg_name="path")
18521854
def to_stata(
18531855
self,
1854-
path,
1855-
convert_dates=None,
1856-
write_index=True,
1857-
byteorder=None,
1858-
time_stamp=None,
1859-
data_label=None,
1860-
variable_labels=None,
1861-
version=114,
1862-
convert_strl=None,
1863-
):
1856+
path: FilePathOrBuffer,
1857+
convert_dates: Optional[Dict[Label, str]] = None,
1858+
write_index: bool = True,
1859+
byteorder: Optional[str] = None,
1860+
time_stamp: Optional[datetime.datetime] = None,
1861+
data_label: Optional[str] = None,
1862+
variable_labels: Optional[Dict[Label, str]] = None,
1863+
version: Optional[int] = 114,
1864+
convert_strl: Optional[Sequence[Label]] = None,
1865+
) -> None:
18641866
"""
18651867
Export DataFrame object to Stata dta format.
18661868
@@ -1954,19 +1956,22 @@ def to_stata(
19541956
raise ValueError("strl is not supported in format 114")
19551957
from pandas.io.stata import StataWriter as statawriter
19561958
elif version == 117:
1957-
from pandas.io.stata import StataWriter117 as statawriter
1959+
# mypy: Name 'statawriter' already defined (possibly by an import)
1960+
from pandas.io.stata import StataWriter117 as statawriter # type: ignore
19581961
else: # versions 118 and 119
1959-
from pandas.io.stata import StataWriterUTF8 as statawriter
1962+
# mypy: Name 'statawriter' already defined (possibly by an import)
1963+
from pandas.io.stata import StataWriterUTF8 as statawriter # type:ignore
19601964

1961-
kwargs = {}
1965+
kwargs: Dict[str, Any] = {}
19621966
if version is None or version >= 117:
19631967
# strl conversion is only supported >= 117
19641968
kwargs["convert_strl"] = convert_strl
19651969
if version is None or version >= 118:
19661970
# Specifying the version is only supported for UTF8 (118 or 119)
19671971
kwargs["version"] = version
19681972

1969-
writer = statawriter(
1973+
# mypy: Too many arguments for "StataWriter"
1974+
writer = statawriter( # type: ignore
19701975
path,
19711976
self,
19721977
convert_dates=convert_dates,

pandas/io/common.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,9 @@ def get_filepath_or_buffer(
160160
161161
Returns
162162
-------
163-
tuple of ({a filepath_ or buffer or S3File instance},
164-
encoding, str,
165-
compression, str,
166-
should_close, bool)
163+
Tuple[FilePathOrBuffer, str, str, bool]
164+
Tuple containing the filepath or buffer, the encoding, the compression
165+
and should_close.
167166
"""
168167
filepath_or_buffer = stringify_path(filepath_or_buffer)
169168

0 commit comments

Comments
 (0)