Esempio n. 1
0
    def test_save_to_existing_file(self):
        a = pd.DataFrame({
            "i": [1, 2, 3, 4],
            "a": ['a1', 'a2', 'a3', None],
            "b": [None, 'b2', None, 'b4']
        })
        a.set_index('i', inplace=True)
        b = pd.DataFrame({
            "i": [2, 3, 4],
            "b": ['b2', 'b3', None],
            "c": ['c2', None, 'c4']
        })
        b.set_index('i', inplace=True)

        expected = merge_dfs_by_index(a, b)

        a.to_csv(test_directory / 'test_files' / 'a.tsv', sep='\t', index=True)
        b.to_csv(test_directory / 'test_files' / 'b.tsv', sep='\t', index=True)
        expected.to_csv(test_directory / 'test_files' / 'merged_ab.tsv',
                        sep='\t',
                        index=True)

        # merge a -> b
        save_tsv(a, test_directory / 'test_files' / 'b.tsv')

        # read merged version of b
        merged_ab_read = pd.read_csv(test_directory / 'test_files' / 'b.tsv',
                                     sep='\t',
                                     index_col=0)

        self.assertTrue(expected.equals(merged_ab_read))
Esempio n. 2
0
    def test_create_dummy_file(self):
        df = pd.read_csv(test_directory / 'test_files' / 'participants.tsv',
                         sep='\t',
                         index_col=0)
        path_to_save = test_directory / "test_create_dummy_file.tsv"
        save_tsv(df, path_to_save)

        self.assertTrue(Path(path_to_save).exists())
        df_read = pd.read_csv(test_directory / "test_create_dummy_file.tsv",
                              sep='\t',
                              index_col=0)
        self.assertTrue(df_read.equals(df))
Esempio n. 3
0
    def test_save_index_dtype_str(self):
        a = pd.DataFrame({"i": ['1'], "a": ["a"]})
        a.set_index('i', inplace=True)

        expected = a

        # merge a -> a
        save_tsv(a, test_directory / 'test_files' / 'a.tsv')
        save_tsv(a, test_directory / 'test_files' / 'a.tsv')

        # read merged version of b
        merged_read = pd.read_csv(test_directory / 'test_files' / 'a.tsv',
                                  sep='\t',
                                  index_col=0)
        merged_read = merged_read.set_index(merged_read.index.astype(str))

        self.assertTrue(expected.equals(merged_read))
Esempio n. 4
0
 def test_create_file(self):
     df = pd.DataFrame()
     path_to_save = test_directory / "test_create_file.tsv"
     save_tsv(df, path_to_save)
     self.assertTrue(Path(path_to_save).exists())