Beispiel #1
0
 def test_excelwriter_fspath(self):
     with tm.ensure_clean("foo.xlsx") as path:
         writer = ExcelWriter(path)
         assert os.fspath(writer) == str(path)
Beispiel #2
0
def path(ext):
    """
    Fixture to open file for use in each test case.
    """
    with tm.ensure_clean(ext) as file_path:
        yield file_path
Beispiel #3
0
def test_bad_encdoing_errors():
    # GH 39777
    with tm.ensure_clean() as path:
        with pytest.raises(ValueError, match="Invalid value for `encoding_errors`"):
            icom.get_handle(path, "w", errors="bad")
Beispiel #4
0
 def test_stringify_file_and_path_like(self):
     # GH 38125: do not stringify file objects that are also path-like
     fsspec = pytest.importorskip("fsspec")
     with tm.ensure_clean() as path:
         with fsspec.open(f"file://{path}", mode="wb") as fsspec_obj:
             assert fsspec_obj == icom.stringify_path(fsspec_obj)
Beispiel #5
0
 def test_unknown_engine(self):
     with tm.ensure_clean() as path:
         df = tm.makeDataFrame()
         df.to_csv(path)
         with pytest.raises(ValueError, match="Unknown engine"):
             pd.read_csv(path, engine="pyt")
Beispiel #6
0
 def check_error_on_write(self, df, engine, exc):
     # check that we are raising the exception on writing
     with tm.ensure_clean() as path:
         with pytest.raises(exc):
             to_parquet(df, path, engine, compression=None)
Beispiel #7
0
def check_round_trip(
    df,
    engine=None,
    path=None,
    write_kwargs=None,
    read_kwargs=None,
    expected=None,
    check_names=True,
    check_like=False,
    check_dtype=True,
    repeat=2,
):
    """Verify parquet serializer and deserializer produce the same results.

    Performs a pandas to disk and disk to pandas round trip,
    then compares the 2 resulting DataFrames to verify equality.

    Parameters
    ----------
    df: Dataframe
    engine: str, optional
        'pyarrow' or 'fastparquet'
    path: str, optional
    write_kwargs: dict of str:str, optional
    read_kwargs: dict of str:str, optional
    expected: DataFrame, optional
        Expected deserialization result, otherwise will be equal to `df`
    check_names: list of str, optional
        Closed set of column names to be compared
    check_like: bool, optional
        If True, ignore the order of index & columns.
    repeat: int, optional
        How many times to repeat the test
    """
    write_kwargs = write_kwargs or {"compression": None}
    read_kwargs = read_kwargs or {}

    if expected is None:
        expected = df

    if engine:
        write_kwargs["engine"] = engine
        read_kwargs["engine"] = engine

    def compare(repeat):
        for _ in range(repeat):
            df.to_parquet(path, **write_kwargs)
            with catch_warnings(record=True):
                actual = read_parquet(path, **read_kwargs)

            tm.assert_frame_equal(
                expected,
                actual,
                check_names=check_names,
                check_like=check_like,
                check_dtype=check_dtype,
            )

    if path is None:
        with tm.ensure_clean() as path:
            compare(repeat)
    else:
        compare(repeat)
Beispiel #8
0
def test_read_unsupported_compression_type():
    with tm.ensure_clean() as path:
        msg = "Unrecognized compression type: unsupported"
        with pytest.raises(ValueError, match=msg):
            pd.read_json(path, compression="unsupported")
Beispiel #9
0
def test_write_unsupported_compression_type():
    df = pd.read_json('{"a": [1, 2, 3], "b": [4, 5, 6]}')
    with tm.ensure_clean() as path:
        msg = "Unrecognized compression type: unsupported"
        with pytest.raises(ValueError, match=msg):
            df.to_json(path, compression="unsupported")