MCPcopy
hub / github.com/pandas-dev/pandas / variable_labels

Method variable_labels

pandas/io/stata.py:2004–2043  ·  view source on GitHub ↗

Return a dict associating each variable name with corresponding label. This method retrieves variable labels from a Stata file. Variable labels are mappings between variable names and their corresponding descriptive labels in a Stata dataset. Returns

(self)

Source from the content-addressed store, hash-verified

2002 return self._time_stamp
2003
2004 def variable_labels(self) -> dict[str, str]:
2005 """
2006 Return a dict associating each variable name with corresponding label.
2007
2008 This method retrieves variable labels from a Stata file. Variable labels are
2009 mappings between variable names and their corresponding descriptive labels
2010 in a Stata dataset.
2011
2012 Returns
2013 -------
2014 dict
2015 A python dictionary.
2016
2017 See Also
2018 --------
2019 read_stata : Read Stata file into DataFrame.
2020 DataFrame.to_stata : Export DataFrame object to Stata dta format.
2021
2022 Examples
2023 --------
2024 >>> df = pd.DataFrame([[1, 2], [3, 4]], columns=["col_1", "col_2"])
2025 >>> time_stamp = pd.Timestamp(2000, 2, 29, 14, 21)
2026 >>> path = "/My_path/filename.dta"
2027 >>> variable_labels = {"col_1": "This is an example"}
2028 >>> df.to_stata(
2029 ... path,
2030 ... time_stamp=time_stamp, # doctest: +SKIP
2031 ... variable_labels=variable_labels,
2032 ... version=None,
2033 ... ) # doctest: +SKIP
2034 >>> with pd.io.stata.StataReader(path) as reader: # doctest: +SKIP
2035 ... print(reader.variable_labels()) # doctest: +SKIP
2036 {'index': '', 'col_1': 'This is an example', 'col_2': ''}
2037 >>> pd.read_stata(path) # doctest: +SKIP
2038 index col_1 col_2
2039 0 0 1 2
2040 1 1 3 4
2041 """
2042 self._ensure_open()
2043 return dict(zip(self._varlist, self._variable_labels, strict=True))
2044
2045 def value_labels(self) -> dict[str, dict[int, str]]:
2046 """

Callers 4

test_read_dta118_119Method · 0.80
test_variable_labelsMethod · 0.80
test_utf8_writerMethod · 0.80

Calls 1

_ensure_openMethod · 0.95

Tested by 4

test_read_dta118_119Method · 0.64
test_variable_labelsMethod · 0.64
test_utf8_writerMethod · 0.64