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