Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_sas7bdat(self):
df, meta = pyreadstat.read_sas7bdat(os.path.join(self.basic_data_folder, "sample.sas7bdat"))
self.assertTrue(df.equals(self.df_pandas))
self.assertTrue(meta.number_columns == len(self.df_pandas.columns))
self.assertTrue(meta.number_rows == len(self.df_pandas))
df2["date"] = pd.to_datetime(df2["date"])
df2["dtime"] = pd.to_datetime(df2["dtime"])
df2["time"] = pd.to_datetime(df2["time"])
df2["time"] = df2["time"].apply(lambda x: x.time())
df3 = df2.copy()
df3["date"] = df3["date"].apply(lambda x: x.date())
df3["time"] = df3["time"].apply(lambda x: x.time())
import pyreadstat
sas_file = os.path.join(basic_data_folder, "dates.sas7bdat")
df_sas,meta = pyreadstat.read_sas7bdat(sas_file)
df_sas.equals(df3)
df_sas,meta = pyreadstat.read_sas7bdat(sas_file, dates_as_pandas_datetime=True)
df_sas.equals(df2)
for col in df2.columns:
if not df2[col].equals(df_sas[col]):
for indx, (pa, sas) in enumerate(zip(df2[col], df_sas[col])):
if pa != sas:
print(indx, pa, sas)
unformatted_csv = os.path.join(self.missing_data_folder, "missing_unformatted.csv")
formatted_csv = os.path.join(self.missing_data_folder, "missing_sas_formatted.csv")
labeled_csv = os.path.join(self.missing_data_folder, "missing_sas_labeled.csv")
df_sas, meta = pyreadstat.read_sas7bdat(sas_file)
df_csv = pd.read_csv(unformatted_csv)
self.assertTrue(df_sas.equals(df_csv))
df_sas, meta = pyreadstat.read_sas7bdat(sas_file, user_missing=True)
df_csv = pd.read_csv(formatted_csv)
self.assertTrue(df_sas.equals(df_csv))
missing_user_values = {'var1':['A'],'var2': ['B'], 'var3':['C'], 'var4':['X'], 'var5':['Y'],
'var6':['Z'], 'var7':['_']}
self.assertDictEqual(meta.missing_user_values, missing_user_values)
df_sas, meta = pyreadstat.read_sas7bdat(sas_file,
catalog_file=cat_file, user_missing=True,
formats_as_category=False)
df_csv = pd.read_csv(labeled_csv)
self.assertTrue(df_sas.equals(df_csv))
def test_sas7bdat_metaonly(self):
df, meta = pyreadstat.read_sas7bdat(os.path.join(self.basic_data_folder, "sample.sas7bdat"))
df2, meta2 = pyreadstat.read_sas7bdat(os.path.join(self.basic_data_folder, "sample.sas7bdat"), metadataonly=True)
self.assertTrue(df2.empty)
self.assertTrue(meta.number_columns == meta2.number_columns)
self.assertTrue(meta.number_rows == meta2.number_rows)
self.assertTrue(meta.column_names == meta2.column_names)
self.assertTrue(meta.column_labels == meta2.column_labels)
def test_sas_user_missing(self):
sas_file = os.path.join(self.missing_data_folder, "missing_test.sas7bdat")
cat_file = os.path.join(self.missing_data_folder, "missing_formats.sas7bcat")
unformatted_csv = os.path.join(self.missing_data_folder, "missing_unformatted.csv")
formatted_csv = os.path.join(self.missing_data_folder, "missing_sas_formatted.csv")
labeled_csv = os.path.join(self.missing_data_folder, "missing_sas_labeled.csv")
df_sas, meta = pyreadstat.read_sas7bdat(sas_file)
df_csv = pd.read_csv(unformatted_csv)
self.assertTrue(df_sas.equals(df_csv))
df_sas, meta = pyreadstat.read_sas7bdat(sas_file, user_missing=True)
df_csv = pd.read_csv(formatted_csv)
self.assertTrue(df_sas.equals(df_csv))
missing_user_values = {'var1':['A'],'var2': ['B'], 'var3':['C'], 'var4':['X'], 'var5':['Y'],
'var6':['Z'], 'var7':['_']}
self.assertDictEqual(meta.missing_user_values, missing_user_values)
df_sas, meta = pyreadstat.read_sas7bdat(sas_file,
catalog_file=cat_file, user_missing=True,
formats_as_category=False)
df_csv = pd.read_csv(labeled_csv)
self.assertTrue(df_sas.equals(df_csv))
def test_sas7bdat_usecols(self):
df, meta = pyreadstat.read_sas7bdat(os.path.join(self.basic_data_folder, "sample.sas7bdat"), usecols=self.usecols)
self.assertTrue(df.equals(self.df_usecols))
self.assertTrue(meta.number_columns == len(self.usecols))
self.assertTrue(meta.column_names == self.usecols)
def test_sas_user_missing(self):
sas_file = os.path.join(self.missing_data_folder, "missing_test.sas7bdat")
cat_file = os.path.join(self.missing_data_folder, "missing_formats.sas7bcat")
unformatted_csv = os.path.join(self.missing_data_folder, "missing_unformatted.csv")
formatted_csv = os.path.join(self.missing_data_folder, "missing_sas_formatted.csv")
labeled_csv = os.path.join(self.missing_data_folder, "missing_sas_labeled.csv")
df_sas, meta = pyreadstat.read_sas7bdat(sas_file)
df_csv = pd.read_csv(unformatted_csv)
self.assertTrue(df_sas.equals(df_csv))
df_sas, meta = pyreadstat.read_sas7bdat(sas_file, user_missing=True)
df_csv = pd.read_csv(formatted_csv)
self.assertTrue(df_sas.equals(df_csv))
missing_user_values = {'var1':['A'],'var2': ['B'], 'var3':['C'], 'var4':['X'], 'var5':['Y'],
'var6':['Z'], 'var7':['_']}
self.assertDictEqual(meta.missing_user_values, missing_user_values)
df_sas, meta = pyreadstat.read_sas7bdat(sas_file,
catalog_file=cat_file, user_missing=True,
formats_as_category=False)
df_csv = pd.read_csv(labeled_csv)
self.assertTrue(df_sas.equals(df_csv))
def test_sas7bdat_nodates(self):
df, meta = pyreadstat.read_sas7bdat(os.path.join(self.basic_data_folder, "sample.sas7bdat"), disable_datetime_conversion=True)
self.assertTrue(df.equals(self.df_nodates_sastata))
def test_sas_dates(self):
sas_file = os.path.join(self.basic_data_folder, "dates.sas7bdat")
df_sas, meta = pyreadstat.read_sas7bdat(sas_file)
self.assertTrue(df_sas.equals(self.df_sas_dates))