How to use the pyreadstat.write_dta function in pyreadstat

To help you get started, we’ve selected a few pyreadstat examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Roche / pyreadstat / tests / test_basic.py View on Github external
def test_dta_write_user_missing(self):


        #formatted_csv = os.path.join(self.missing_data_folder, "missing_dta_formatted.csv")
        #labeled_csv = os.path.join(self.missing_data_folder, "missing_dta_labeled.csv")
        
        df_csv = pd.DataFrame([[3,"a"],["a","b"]], columns=["Var1", "Var2"])
        df_csv2 = pd.DataFrame([[3,"a"],["labeled","b"]], columns=["Var1", "Var2"])

        missing_user_values = {'Var1': ['a']}
        variable_value_labels = {'Var1':{'a':'labeled'}}
        path = os.path.join(self.write_folder, "user_missing_write.dta")
        pyreadstat.write_dta(df_csv, path, version=12, missing_user_values=missing_user_values, variable_value_labels=variable_value_labels)
        
        df_dta, meta = pyreadstat.read_dta(path, user_missing=True)
        self.assertTrue(df_csv.equals(df_dta))
        self.assertDictEqual(meta.missing_user_values, missing_user_values)
        
        df_dta2, meta2 = pyreadstat.read_dta(path, user_missing=True, apply_value_formats=True, formats_as_category=False)
        self.assertTrue(df_csv2.equals(df_dta2))
github Roche / pyreadstat / tests / test_basic.py View on Github external
def test_dta_write_dates(self):

        #if sys.version_info[0] < 3:
        #    return

        path = os.path.join(self.write_folder, "dates_write.dta")
        pyreadstat.write_dta(self.df_sas_dates, path)
        df, meta = pyreadstat.read_dta(path)
        self.assertTrue(df.equals(self.df_sas_dates))
github Roche / pyreadstat / tests / test_basic.py View on Github external
def test_dta_write_basic(self):

        df_pandas = self.df_pandas.copy()
        df_pandas["myord"] = df_pandas["myord"].astype(np.int32)
        df_pandas["mylabl"] = df_pandas["mylabl"].astype(np.int32)

        file_label = "basic write"
        col_labels = ["mychar label","mynum label", "mydate label", "dtime label", None, "myord label", "mytime label"]
        variable_value_labels = {'mylabl': {1: 'Male', 2: 'Female'}, 'myord': {1: 'low', 2: 'medium', 3: 'high'}}
        path = os.path.join(self.write_folder, "basic_write.dta")
        pyreadstat.write_dta(df_pandas, path, file_label=file_label, column_labels=col_labels, version=12, variable_value_labels=variable_value_labels)
        df, meta = pyreadstat.read_dta(path)

        df_pandas["myord"] = df_pandas["myord"].astype(np.int64)
        df_pandas["mylabl"] = df_pandas["mylabl"].astype(np.int64)

        self.assertTrue(df.equals(df_pandas))
        self.assertEqual(meta.file_label, file_label)
        self.assertListEqual(meta.column_labels, col_labels)
        self.assertDictEqual(meta.variable_value_labels, variable_value_labels)

pyreadstat

Reads and Writes SAS, SPSS and Stata files into/from pandas data frames.

Apache-2.0
Latest version published 2 months ago

Package Health Score

84 / 100
Full package analysis

Similar packages