So I know it's possible to read in either Stata categorical labels or values using the convert_categoricals parameter.
I was looking for a way to write/export a pandas dataframe to Stata and include the value labels. However all I could find was either
data_label : str, optional for the dataset label
or
variable_labels : dict for column names label,
but nothing for the values themselves.
Here is an answer to your question. It is probably not what you were expecting because I am not using
pd.to_Stata, but the Python integration developed on Stata 16.The code below must be executed within Stata (from version 16 onwards). Briefly, I am generating a Pandas Data.Frame (
df) that I will export to Stata. The trick is to apply the labels on the values using theValueLabel.setLabelValue()functionality that comes from thesfilibrary.In case you want to understand better the reasoning behind the code above, here are the references that I used to learn it.