Repository navigation
Expand file tree
/
Copy pathsaveit.ado
More file actions
133 lines (113 loc) · 5.24 KB
/
Copy pathsaveit.ado
File metadata and controls
133 lines (113 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
*! version 1.0.0.
*! Saves files in multiple formats using Python's Pandas functionality.
*! Usage: saveit auto.parquet, replace
program saveit
syntax anything [, REPlace Type(string) pd_options(string)]
if "`replace'"=="" {
cap confirm file `anything'
_assert(_rc!=0), msg("File already exists. You can specify -replace-")
}
//There's no way to use the Python API to query char keys, so in Stata store them as locals
//TODO: Throw warning if variables named _dta or anything
unab vlist : *
loc evlist _dta `vlist'
foreach ev in `evlist' {
loc `ev' : char `ev'[]
}
// Test to see if there are any additional options
if `"`pd_options'"' != "" loc optin ", "
python: pysaveit("`anything'", "`: data label'", "`type'" `optin' `pd_options')
end
python:
import os
from sfi import Characteristic, Data, ValueLabel, Macro, SFIToolkit
import pandas as pd
import numpy as np
types_stata_to_pandas = {'byte':pd.Int8Dtype(), 'int':pd.Int16Dtype(), 'long':pd.Int32Dtype(), 'float':pd.Float32Dtype(), 'double':pd.Float64Dtype()}
def get_stata_metadata(vars : [str], data_label: str = "") -> dict:
var_types = {}
var_meta = {}
var_chars = {}
for var in vars:
var_type = Data.getVarType(var)
# If str, just store length
if Data.isVarTypeStr(var):
var_type = int(var_type[3:])
var_types[var] = var_type
var_meta[var] = (Data.getVarLabel(var), Data.getVarFormat(var), ValueLabel.getVarValueLabel(var))
var_chars[var] = {name: Characteristic.getVariableChar(var, name) for name in Macro.getLocal(var).split(" ")}
dta_chars = {name: Characteristic.getDtaChar(name) for name in Macro.getLocal("_dta").split(" ")}
vls = {}
for name in ValueLabel.getNames():
vls[name] = ValueLabel.getValueLabels(name) # (getLabels(var_label_name), getValues(var_label_name))
return {'vls': vls, 'var_types':var_types, 'var_meta':var_meta, 'data_label': data_label, 'dta_chars': dta_chars, 'var_chars': var_chars}
def pysaveit(path: str, data_label:str = "", filetype: str = "", **kwargs):
# Load the data into Pandas df
dataraw = Data.getAsDict(missingval=np.nan)
# TODO: ensure same order when using dict
vars = dataraw.keys()
stata_metadata = get_stata_metadata(vars, data_label)
# ensure Stata integer types are mapped to pd nullable types, rather than float, and ensure float/double correct conversion
for var in vars:
v_type = stata_metadata['var_types'][var]
if Data.isVarTypeString(var):
dataraw[var] = pd.array(dataraw[var], dtype=pd.StringDtype())
else:
dataraw[var] = pd.array(dataraw[var], dtype=types_stata_to_pandas[v_type])
dataframe = pd.DataFrame(dataraw)
dataframe.attrs = {'stata_metadata': stata_metadata} # since 2.1 https://github.com/pandas-dev/pandas/issues/54321
# Save out to file
FILE_TYPE = {'.dta': 'stata',
'.xls': 'excel',
'.xlsx': 'excel',
'.csv': 'csv',
'.pkl': 'pickle',
'.pickle': 'pickle',
#'.sas7bdat': 'sas', #not implimented
#'.xport': 'sas', #not implimented
#'.tab': 'tab', #no specific function (closest is to_csv)
#'.tsv': 'tab', #no specific function (closest is to_csv)
#'.txt': 'fwf', #no specific function
#'.dat': 'fwf', #no specific function
'.json': 'json',
'.html': 'html',
'.feather': 'feather',
'.parquet': 'parquet',
'.h5': 'hdf',
#'.sav': 'spss' # not implimented
}
if filetype!="":
if filetype not in FILE_TYPE.values():
raise Exception("Invalid save file type specified: " + filetype)
else: # autodetect
file_extension = os.path.splitext(path)[1]
if file_extension not in FILE_TYPE.keys():
raise Exception("Unknown save file type inferred from extension (use type to specificy): " + file_extension)
filetype = FILE_TYPE[file_extension]
if 'index' not in kwargs.keys() and filetype not in ['stata', 'pickle', 'feather']:
kwargs['index'] = False
if filetype == 'stata':
dataframe.to_stata(path, **kwargs)
elif filetype == 'csv':
dataframe.to_csv(path, **kwargs)
elif filetype == 'excel':
dataframe.to_excel(path, **kwargs)
elif filetype == 'spss':
dataframe.to_spss(path, **kwargs)
elif filetype == 'html':
dataframe.to_html(path, **kwargs)
elif filetype == 'pickle':
dataframe.to_pickle(path, **kwargs)
elif filetype == 'json':
dataframe.to_json(path, **kwargs)
elif filetype == 'fwf':
dataframe.to_fwf(path, **kwargs)
elif filetype == 'feather':
dataframe.to_feather(path, **kwargs)
elif filetype == 'parquet':
dataframe.to_parquet(path, **kwargs)
elif filetype == 'hdf':
if 'key' not in kwargs.keys():
kwargs['key'] = 'df'
dataframe.to_hdf(path, **kwargs)
end