@@ -1948,52 +1948,39 @@ def to_stata(
1948
1948
"""
1949
1949
if version not in (114 , 117 , 118 , 119 , None ):
1950
1950
raise ValueError ("Only formats 114, 117, 118 and 119 are supported." )
1951
- if version == 114 and convert_strl is not None :
1952
- raise ValueError ("strl is not supported in format 114" )
1953
-
1954
- # TODO: There must be a better way?
1955
1951
if version == 114 :
1956
- from pandas .io .stata import StataWriter
1957
-
1958
- StataWriter (
1959
- path ,
1960
- self ,
1961
- convert_dates = convert_dates ,
1962
- byteorder = byteorder ,
1963
- time_stamp = time_stamp ,
1964
- data_label = data_label ,
1965
- write_index = write_index ,
1966
- variable_labels = variable_labels ,
1967
- ).write_file ()
1952
+ if convert_strl is not None :
1953
+ raise ValueError ("strl is not supported in format 114" )
1954
+ from pandas .io .stata import StataWriter as statawriter
1968
1955
elif version == 117 :
1969
- from pandas . io . stata import StataWriter117
1970
-
1971
- StataWriter117 (
1972
- path ,
1973
- self ,
1974
- convert_dates = convert_dates ,
1975
- byteorder = byteorder ,
1976
- time_stamp = time_stamp ,
1977
- data_label = data_label ,
1978
- write_index = write_index ,
1979
- variable_labels = variable_labels ,
1980
- convert_strl = convert_strl ,
1981
- ). write_file ()
1982
- else :
1983
- from pandas . io . stata import StataWriterUTF8
1984
-
1985
- StataWriterUTF8 (
1986
- path ,
1987
- self ,
1988
- convert_dates = convert_dates ,
1989
- byteorder = byteorder ,
1990
- time_stamp = time_stamp ,
1991
- data_label = data_label ,
1992
- write_index = write_index ,
1993
- variable_labels = variable_labels ,
1994
- convert_strl = convert_strl ,
1995
- version = version ,
1996
- ) .write_file ()
1956
+ # mypy: Name 'statawriter' already defined (possibly by an import)
1957
+ from pandas . io . stata import StataWriter117 as statawriter # type: ignore
1958
+ else : # versions 118 and 119
1959
+ # mypy: Name 'statawriter' already defined (possibly by an import)
1960
+ from pandas . io . stata import StataWriterUTF8 as statawriter # type:ignore
1961
+
1962
+ kwargs = {}
1963
+ if version is None or version >= 117 :
1964
+ # strl conversion is only supported >= 117
1965
+ kwargs [ "convert_strl" ] = convert_strl
1966
+ if version is None or version >= 118 :
1967
+ # Specifying the version is only supported for UTF8 (118 or 119)
1968
+ # mypy: Incompatible types in assignment
1969
+ kwargs [ "version" ] = version # type: ignore
1970
+
1971
+ # mypy: Too many arguments for "StataWriter"
1972
+ writer = statawriter ( # type: ignore
1973
+ path ,
1974
+ self ,
1975
+ convert_dates = convert_dates ,
1976
+ byteorder = byteorder ,
1977
+ time_stamp = time_stamp ,
1978
+ data_label = data_label ,
1979
+ write_index = write_index ,
1980
+ variable_labels = variable_labels ,
1981
+ ** kwargs ,
1982
+ )
1983
+ writer .write_file ()
1997
1984
1998
1985
@deprecate_kwarg (old_arg_name = "fname" , new_arg_name = "path" )
1999
1986
def to_feather (self , path ) -> None :
0 commit comments