def test_empty_read(_output_dir: str): df = MyTfs(plane="X", directory=_output_dir) write_tfs(df.get_filename(), df, save_index="NAME") df_read = MyTfs(plane="X", directory=_output_dir).read() assert_frame_equal(df, df_read, check_exact=False) # float precision can be an issue assert_dict_equal(df.headers, df_read.headers, compare_keys=True)
def remove_nan_from_files(list_of_files: list, replace: bool = False): """ Remove NAN-Entries from files in list_of_files. If replace=False a new file with .dropna in it's name is created, otherwise the file is overwritten. """ for filepath in list_of_files: try: df = read_tfs(filepath) LOG.info(f"Read file {filepath:s}") except (IOError, TfsFormatError): LOG.info(f"Skipped file {filepath:s}") else: df = df.dropna(axis='index') if not replace: filepath += ".dropna" write_tfs(filepath, df)
def remove_nan_from_files(list_of_files: list, replace: bool = None) -> None: """ Remove NAN-Entries from files in list_of_files. If replace=False a new file with .dropna appended to its name is created, otherwise the file is overwritten. """ replace = False if replace is None else replace for filepath in list_of_files: try: tfs_data_frame = read_tfs(filepath) LOG.info(f"Read file {filepath:s}") except (IOError, TfsFormatError): LOG.info(f"Skipped file {filepath:s}") else: tfs_data_frame = tfs_data_frame.dropna(axis="index") if not replace: filepath += ".dropna" write_tfs(filepath, tfs_data_frame)
def write(self): self.validate_definitions() write_tfs(self._filename, self, save_index=self.index.name)
def _write_tfs(self, filename, data_frame): if self.allow_write: write_tfs(os.path.join(self.directory, filename), data_frame) self._buffer[filename] = data_frame
def test_read_fail(_output_dir: str): mydf = MyTfs(plane="X", directory=_output_dir) df = TfsDataFrame(columns=["Does", "Not", "Exist"]) write_tfs(mydf.get_filename(), df) with pytest.raises(KeyError): MyTfs(plane="X", directory=_output_dir).read()
def test_filled_read(_output_dir: str, _filled_tfs: MyTfs): df = _filled_tfs(plane="X", directory=_output_dir) write_tfs(df.get_filename(), df, save_index="NAME") df_read = MyTfs(plane="X", directory=_output_dir).read() assert_frame_equal(df, df_read) assert_dict_equal(df.headers, df_read.headers, compare_keys=True)
def test_filled_read(_output_dir, _filled_tfs): df = _filled_tfs(plane="X", directory=_output_dir) write_tfs(df.get_filename(), df, save_index="NAME") df_read = MyTfs(plane="X", directory=_output_dir).read() compare_dataframes(df, df_read)