code: """ parquet compat """
  from __future__ import annotations
  
  import io
  import os
  from typing import Any
  from warnings import catch_warnings
  
  from pandas._typing import (
      FilePath,
      ReadBuffer,
      StorageOptions,
      WriteBuffer,
  )
  from pandas.compat._optional import import_optional_dependency
  from pandas.errors import AbstractMethodError
  from pandas.util._decorators import doc
  
  from pandas import (
      DataFrame,
      MultiIndex,
      get_option,
  )
  from pandas.core.shared_docs import _shared_docs
  from pandas.util.version import Version
  
  from pandas.io.common import (
      IOHandles,
      get_handle,
      is_fsspec_url,
      is_url,
      stringify_path,
  )
  
  
  def get_engine(engine: str) -> BaseImpl:
      """return our implementation"""
      if engine == "auto":
          engine = get_option("io.parquet.engine")
  
      if engine == "auto":
          # try engines in this order
          engine_classes = [PyArrowImpl, FastParquetImpl]
  
          error_msgs = ""
          for engine_class in engine_classes:
              try:
                  return engine_class()
              except ImportError as err:
                  error_msgs += "\n - " + str(err)
  
          raise ImportError(
              "Unable to find a usable engine; "
              "tried using: 'pyarrow', 'fastparquet'.\n"
              "A suitable version of "
              "pyarrow or fastparquet is required for parquet "
              "support.\n"
              "Trying to import the above resulted in these errors:"
              f"{error_msgs}"
          )
  
      if engine == "pyarrow":
          return PyArrowImpl()
      elif engine == "fastparquet":
          return FastParquetImpl()
  
      raise ValueError("engine must be one of 'pyarrow', 'fastparquet'")
  
  
  def _get_path_or_handle(
      path: FilePath | ReadBuffer[bytes] | WriteBuffer[bytes],
      fs: Any,
      storage_options: StorageOptions = None,
      mode: str = "rb",
      is_dir: bool = False,
  ) -> tuple[
      FilePath | ReadBuffer[bytes] | WriteBuffer[bytes], IOHandles[bytes] | None, Any
  ]:
      """File handling for PyArrow."""
      path_or_handle = stringify_path(path)
      if is_fsspec_url(path_or_handle) and fs is None:
          fsspec = import_optional_dependency("fsspec")
  
          fs, path_or_handle = fsspec.core.url_to_fs(
              path_or_handle, **(storage_options or {})
          )
      elif storage_options and (not is_url(path_or_handle) or mode != "rb"):
          # can't write to a remote url
          # without making use of fsspec at the moment
          raise ValueError("storage_options passed with buffer, or non-supported URL")
  
      handles = None
      if (
          not fs
          and not is_dir
          and isinstance(path_or_handle, str)
          and not os.path.isdir(path_or_handle)
      ):
          # use get_handle only when we are very certain that it is not a directory
          # fsspec resources can also point to directories
          # this branch is used for example when reading from non-fsspec URLs
          handles = get_handle(
              path_or_handle, mode, is_text=False, storage_options=storage_options
          )
          fs = None
          path_or_handle = handles.handle
      return path_or_handle, handles, fs
  
  
  class BaseImpl:
      @staticmethod
      def validate_dataframe(df: DataFrame):
  
          if not isinstance(df, DataFrame):
              raise ValueError("to_parquet only supports IO with DataFrames")
  
          # must have value column names for all index levels (strings only)
          if isinstance(df.columns, MultiIndex):
              if not all(
                  x.inferred_type in {"string", "empty"} for x in df.columns.levels
              ):
                  raise ValueError(
                      """
                      parquet must have string column names for all values in
                       each level of the MultiIndex
                      """
                  )
          else:
              if df.columns.inferred_type not in {"string", "empty"}:
                  raise ValueError("parquet must have string column names")
  
          # index level names must be strings
          valid_names = all(
              isinstance(name, str) for name in df.index.names if name is not None
          )
          if not valid_names:
              raise ValueError("Index level names must be strings")
  
      def write(self, df: DataFrame, path, compression, **kwargs):
          raise AbstractMethodError(self)
  
      def read(self, path, columns=None, **kwargs):
          raise AbstractMethodError(self)
  
  
  class PyArrowImpl(BaseImpl):
      def __init__(self):
          import_optional_dependency(
              "pyarrow", extra="pyarrow is required for parquet support."
          )
          import pyarrow.parquet
  
          # import utils to register the pyarrow extension types
          import pandas.core.arrays._arrow_utils  # noqa:F401
  
          self.api = pyarrow
  
      def write(
          self,
          df: DataFrame,
          path: FilePath | WriteBuffer[bytes],
          compression: str | None = "snappy",
          index: bool | None = None,
          storage_options: StorageOptions = None,
          partition_cols: list[str] | None = None,
          **kwargs,
      ):
          self.validate_dataframe(df)
  
          from_pandas_kwargs: dict[str, Any] = {"schema": kwargs.pop("schema", None)}
          if index is not None:
              from_pandas_kwargs["preserve_index"] = index
  
          table = self.api.Table.from_pandas(df, **from_pandas_kwargs)
  
          path_or_handle, handles, kwargs["filesystem"] = _get_path_or_handle(
              path,
              kwargs.pop("filesystem", None),
              storage_options=storage_options,
              mode="wb",
              is_dir=partition_cols is not None,
          )
          try:
              if partition_cols is not None:
                  # writes to multiple files under the given path
                  self.api.parquet.write_to_dataset(
                      table,
                      path_or_handle,
                      compression=compression,
                      partition_cols=partition_cols,
                      **kwargs,
                  )
              else:
                  # write to single output file
                  self.api.parquet.write_table(
                      table, path_or_handle, compression=compression, **kwargs
                  )
          finally:
              if handles is not None:
                  handles.close()
  
      def read(
          self,
          path,
          columns=None,
          use_nullable_dtypes=False,
          storage_options: StorageOptions = None,
          **kwargs,
      ):
          kwargs["use_pandas_metadata"] = True
  
          to_pandas_kwargs = {}
          if use_nullable_dtypes:
              import pandas as pd
  
              mapping = {
                  self.api.int8(): pd.Int8Dtype(),
                  self.api.int16(): pd.Int16Dtype(),
                  self.api.int32(): pd.Int32Dtype(),
                  self.api.int64(): pd.Int64Dtype(),
                  self.api.uint8(): pd.UInt8Dtype(),
                  self.api.uint16(): pd.UInt16Dtype(),
                  self.api.uint32(): pd.UInt32Dtype(),
                  self.api.uint64(): pd.UInt64Dtype(),
                  self.api.bool_(): pd.BooleanDtype(),
                  self.api.string(): pd.StringDtype(),
              }
              to_pandas_kwargs["types_mapper"] = mapping.get
          manager = get_option("mode.data_manager")
          if manager == "array":
              to_pandas_kwargs["split_blocks"] = True  # type: ignore[assignment]
  
          path_or_handle, handles, kwargs["filesystem"] = _get_path_or_handle(
              path,
              kwargs.pop("filesystem", None),
              storage_options=storage_options,
              mode="rb",
          )
          try:
              result = self.api.parquet.read_table(
                  path_or_handle, columns=columns, **kwargs
              ).to_pandas(**to_pandas_kwargs)
              if manager == "array":
                  result = result._as_manager("array", copy=False)
              return result
          finally:
              if handles is not None:
                  handles.close()
  
  
  class FastParquetImpl(BaseImpl):
      def __init__(self):
          # since pandas is a dependency of fastparquet
          # we need to import on first use
          fastparquet = import_optional_dependency(
              "fastparquet", extra="fastparquet is required for parquet support."
          )
          self.api = fastparquet
  
      def write(
          self,
          df: DataFrame,
          path,
          compression="snappy",
          index=None,
          partition_cols=None,
          storage_options: StorageOptions = None,
          **kwargs,
      ):
          self.validate_dataframe(df)
          # thriftpy/protocol/compact.py:339:
          # DeprecationWarning: tostring() is deprecated.
          # Use tobytes() instead.
  
          if "partition_on" in kwargs and partition_cols is not None:
              raise ValueError(
                  "Cannot use both partition_on and "
                  "partition_cols. Use partition_cols for partitioning data"
              )
          elif "partition_on" in kwargs:
              partition_cols = kwargs.pop("partition_on")
  
          if partition_cols is not None:
              kwargs["file_scheme"] = "hive"
  
          # cannot use get_handle as write() does not accept file buffers
          path = stringify_path(path)
          if is_fsspec_url(path):
              fsspec = import_optional_dependency("fsspec")
  
              # if filesystem is provided by fsspec, file must be opened in 'wb' mode.
              kwargs["open_with"] = lambda path, _: fsspec.open(
                  path, "wb", **(storage_options or {})
              ).open()
          elif storage_options:
              raise ValueError(
                  "storage_options passed with file object or non-fsspec file path"
              )
  
          with catch_warnings(record=True):
              self.api.write(
                  path,
                  df,
                  compression=compression,
                  write_index=index,
                  partition_on=partition_cols,
                  **kwargs,
              )
  
      def read(
          self, path, columns=None, storage_options: StorageOptions = None, **kwargs
      ):
          parquet_kwargs: dict[str, Any] = {}
          use_nullable_dtypes = kwargs.pop("use_nullable_dtypes", False)
          if Version(self.api.__version__) >= Version("0.7.1"):
              # We are disabling nullable dtypes for fastparquet pending discussion
              parquet_kwargs["pandas_nulls"] = False
          if use_nullable_dtypes:
              raise ValueError(
                  "The 'use_nullable_dtypes' argument is not supported for the "
                  "fastparquet engine"
              )
          path = stringify_path(path)
          handles = None
          if is_fsspec_url(path):
              fsspec = import_optional_dependency("fsspec")
  
              if Version(self.api.__version__) > Version("0.6.1"):
                  parquet_kwargs["fs"] = fsspec.open(
                      path, "rb", **(storage_options or {})
                  ).fs
              else:
                  parquet_kwargs["open_with"] = lambda path, _: fsspec.open(
                      path, "rb", **(storage_options or {})
                  ).open()
          elif isinstance(path, str) and not os.path.isdir(path):
              # use get_handle only when we are very certain that it is not a directory
              # fsspec resources can also point to directories
              # this branch is used for example when reading from non-fsspec URLs
              handles = get_handle(
                  path, "rb", is_text=False, storage_options=storage_options
              )
              path = handles.handle
  
          parquet_file = self.api.ParquetFile(path, **parquet_kwargs)
  
          result = parquet_file.to_pandas(columns=columns, **kwargs)
  
          if handles is not None:
              handles.close()
          return result
  
  
  @doc(storage_options=_shared_docs["storage_options"])
  def to_parquet(
      df: DataFrame,
      path: FilePath | WriteBuffer[bytes] | None = None,
      engine: str = "auto",
      compression: str | None = "snappy",
      index: bool | None = None,
      storage_options: StorageOptions = None,
      partition_cols: list[str] | None = None,
      **kwargs,
  ) -> bytes | None:
      """
      Write a DataFrame to the parquet format.
  
      Parameters
      ----------
      df : DataFrame
      path : str, path object, file-like object, or None, default None
          String, path object (implementing ``os.PathLike[str]``), or file-like
          object implementing a binary ``write()`` function. If None, the result is
          returned as bytes. If a string, it will be used as Root Directory path
          when writing a partitioned dataset. The engine fastparquet does not
          accept file-like objects.
  
          .. versionchanged:: 1.2.0
  
      engine : {{'auto', 'pyarrow', 'fastparquet'}}, default 'auto'
          Parquet library to use. If 'auto', then the option
          ``io.parquet.engine`` is used. The default ``io.parquet.engine``
          behavior is to try 'pyarrow', falling back to 'fastparquet' if
          'pyarrow' is unavailable.
      compression : {{'snappy', 'gzip', 'brotli', 'lz4', 'zstd', None}},
          default 'snappy'. Name of the compression to use. Use ``None``
          for no compression. The supported compression methods actually
          depend on which engine is used. For 'pyarrow', 'snappy', 'gzip',
          'brotli', 'lz4', 'zstd' are all supported. For 'fastparquet',
          only 'gzip' and 'snappy' are supported.
      index : bool, default None
          If ``True``, include the dataframe's index(es) in the file output. If
          ``False``, they will not be written to the file.
          If ``None``, similar to ``True`` the dataframe's index(es)
          will be saved. However, instead of being saved as values,
          the RangeIndex will be stored as a range in the metadata so it
          doesn't require much space and is faster. Other indexes will
          be included as columns in the file output.
      partition_cols : str or list, optional, default None
          Column names by which to partition the dataset.
          Columns are partitioned in the order they are given.
          Must be None if path is not a string.
      {storage_options}
  
          .. versionadded:: 1.2.0
  
      kwargs
          Additional keyword arguments passed to the engine
  
      Returns
      -------
      bytes if no path argument is provided else None
      """
      if isinstance(partition_cols, str):
          partition_cols = [partition_cols]
      impl = get_engine(engine)
  
      path_or_buf: FilePath | WriteBuffer[bytes] = io.BytesIO() if path is None else path
  
      impl.write(
          df,
          path_or_buf,
          compression=compression,
          index=index,
          partition_cols=partition_cols,
          storage_options=storage_options,
          **kwargs,
      )
  
      if path is None:
          assert isinstance(path_or_buf, io.BytesIO)
          return path_or_buf.getvalue()
      else:
          return None
  
  
  @doc(storage_options=_shared_docs["storage_options"])
  def read_parquet(
      path,
      engine: str = "auto",
      columns=None,
      storage_options: StorageOptions = None,
      use_nullable_dtypes: bool = False,
      **kwargs,
  ):
      """
      Load a parquet object from the file path, returning a DataFrame.
  
      Parameters
      ----------
      path : str, path object or file-like object
          String, path object (implementing ``os.PathLike[str]``), or file-like
          object implementing a binary ``read()`` function.
          The string could be a URL. Valid URL schemes include http, ftp, s3,
          gs, and file. For file URLs, a host is expected. A local file could be:
          ``file://localhost/path/to/table.parquet``.
          A file URL can also be a path to a directory that contains multiple
          partitioned parquet files. Both pyarrow and fastparquet support
          paths to directories as well as file URLs. A directory path could be:
          ``file://localhost/path/to/tables`` or ``s3://bucket/partition_dir``.
      engine : {{'auto', 'pyarrow', 'fastparquet'}}, default 'auto'
          Parquet library to use. If 'auto', then the option
          ``io.parquet.engine`` is used. The default ``io.parquet.engine``
          behavior is to try 'pyarrow', falling back to 'fastparquet' if
          'pyarrow' is unavailable.
      columns : list, default=None
          If not None, only these columns will be read from the file.
  
      {storage_options}
  
          .. versionadded:: 1.3.0
  
      use_nullable_dtypes : bool, default False
          If True, use dtypes that use ``pd.NA`` as missing value indicator
          for the resulting DataFrame. (only applicable for the ``pyarrow``
          engine)
          As new dtypes are added that support ``pd.NA`` in the future, the
          output with this option will change to use those dtypes.
          Note: this is an experimental option, and behaviour (e.g. additional
          support dtypes) may change without notice.
  
          .. versionadded:: 1.2.0
  
      **kwargs
          Any additional kwargs are passed to the engine.
  
      Returns
      -------
      DataFrame
      """
      impl = get_engine(engine)
  
      return impl.read(
          path,
          columns=columns,
          storage_options=storage_options,
          use_nullable_dtypes=use_nullable_dtypes,
          **kwargs,
      )
   """
  Tests for the pandas.io.common functionalities
  """
  import codecs
  import errno
  from functools import partial
  from io import (
      BytesIO,
      StringIO,
      UnsupportedOperation,
  )
  import mmap
  import os
  from pathlib import Path
  import tempfile
  
  import pytest
  
  from pandas.compat import is_platform_windows
  import pandas.util._test_decorators as td
  
  import pandas as pd
  import pandas._testing as tm
  
  import pandas.io.common as icom
  
  
  class CustomFSPath:
      """For testing fspath on unknown objects"""
  
      def __init__(self, path):
          self.path = path
  
      def __fspath__(self):
          return self.path
  
  
  # Functions that consume a string path and return a string or path-like object
  path_types = [str, CustomFSPath, Path]
  
  try:
      from py.path import local as LocalPath
  
      path_types.append(LocalPath)
  except ImportError:
      pass
  
  HERE = os.path.abspath(os.path.dirname(__file__))
  
  
  # https://github.com/cython/cython/issues/1720
  @pytest.mark.filterwarnings("ignore:can't resolve package:ImportWarning")
  class TestCommonIOCapabilities:
      data1 = """index,A,B,C,D
  foo,2,3,4,5
  bar,7,8,9,10
  baz,12,13,14,15
  qux,12,13,14,15
  foo2,12,13,14,15
  bar2,12,13,14,15
  """
  
      def test_expand_user(self):
          filename = "~/sometest"
          expanded_name = icom._expand_user(filename)
  
          assert expanded_name != filename
          assert os.path.isabs(expanded_name)
          assert os.path.expanduser(filename) == expanded_name
  
      def test_expand_user_normal_path(self):
          filename = "/somefolder/sometest"
          expanded_name = icom._expand_user(filename)
  
          assert expanded_name == filename
          assert os.path.expanduser(filename) == expanded_name
  
      def test_stringify_path_pathlib(self):
          rel_path = icom.stringify_path(Path("."))
          assert rel_path == "."
          redundant_path = icom.stringify_path(Path("foo//bar"))
          assert redundant_path == os.path.join("foo", "bar")
  
      @td.skip_if_no("py.path")
      def test_stringify_path_localpath(self):
          path = os.path.join("foo", "bar")
          abs_path = os.path.abspath(path)
          lpath = LocalPath(path)
          assert icom.stringify_path(lpath) == abs_path
  
      def test_stringify_path_fspath(self):
          p = CustomFSPath("foo/bar.csv")
          result = icom.stringify_path(p)
          assert result == "foo/bar.csv"
  
      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)
  
      @pytest.mark.parametrize("path_type", path_types)
      def test_infer_compression_from_path(self, compression_format, path_type):
          extension, expected = compression_format
          path = path_type("foo/bar.csv" + extension)
          compression = icom.infer_compression(path, compression="infer")
          assert compression == expected
  
      @pytest.mark.parametrize("path_type", [str, CustomFSPath, Path])
      def test_get_handle_with_path(self, path_type):
          # ignore LocalPath: it creates strange paths: /absolute/~/sometest
          with tempfile.TemporaryDirectory(dir=Path.home()) as tmp:
              filename = path_type("~/" + Path(tmp).name + "/sometest")
              with icom.get_handle(filename, "w") as handles:
                  assert Path(handles.handle.name).is_absolute()
                  assert os.path.expanduser(filename) == handles.handle.name
  
      def test_get_handle_with_buffer(self):
          input_buffer = StringIO()
          with icom.get_handle(input_buffer, "r") as handles:
              assert handles.handle == input_buffer
          assert not input_buffer.closed
          input_buffer.close()
  
      # Test that BytesIOWrapper(get_handle) returns correct amount of bytes every time
      def test_bytesiowrapper_returns_correct_bytes(self):
          # Test latin1, ucs-2, and ucs-4 chars
          data = """a,b,c
  1,2,3
  ©,®,®
  Look,a snake,🐍"""
          with icom.get_handle(StringIO(data), "rb", is_text=False) as handles:
              result = b""
              chunksize = 5
              while True:
                  chunk = handles.handle.read(chunksize)
                  # Make sure each chunk is correct amount of bytes
                  assert len(chunk) <= chunksize
                  if len(chunk) < chunksize:
                      # Can be less amount of bytes, but only at EOF
                      # which happens when read returns empty
                      assert len(handles.handle.read()) == 0
                      result += chunk
                      break
                  result += chunk
              assert result == data.encode("utf-8")
  
      # Test that pyarrow can handle a file opened with get_handle
      @td.skip_if_no("pyarrow", min_version="0.15.0")
      def test_get_handle_pyarrow_compat(self):
          from pyarrow import csv
  
          # Test latin1, ucs-2, and ucs-4 chars
          data = """a,b,c
  1,2,3
  ©,®,®
  Look,a snake,🐍"""
          expected = pd.DataFrame(
              {"a": ["1", "©", "Look"], "b": ["2", "®", "a snake"], "c": ["3", "®", "🐍"]}
          )
          s = StringIO(data)
          with icom.get_handle(s, "rb", is_text=False) as handles:
              df = csv.read_csv(handles.handle).to_pandas()
              tm.assert_frame_equal(df, expected)
              assert not s.closed
  
      def test_iterator(self):
          with pd.read_csv(StringIO(self.data1), chunksize=1) as reader:
              result = pd.concat(reader, ignore_index=True)
          expected = pd.read_csv(StringIO(self.data1))
          tm.assert_frame_equal(result, expected)
  
          # GH12153
          with pd.read_csv(StringIO(self.data1), chunksize=1) as it:
              first = next(it)
              tm.assert_frame_equal(first, expected.iloc[[0]])
              tm.assert_frame_equal(pd.concat(it), expected.iloc[1:])
  
      @pytest.mark.parametrize(
          "reader, module, error_class, fn_ext",
          [
              (pd.read_csv, "os", FileNotFoundError, "csv"),
              (pd.read_fwf, "os", FileNotFoundError, "txt"),
              (pd.read_excel, "xlrd", FileNotFoundError, "xlsx"),
              (pd.read_feather, "pyarrow", OSError, "feather"),
              (pd.read_hdf, "tables", FileNotFoundError, "h5"),
              (pd.read_stata, "os", FileNotFoundError, "dta"),
              (pd.read_sas, "os", FileNotFoundError, "sas7bdat"),
              (pd.read_json, "os", ValueError, "json"),
              (pd.read_pickle, "os", FileNotFoundError, "pickle"),
          ],
      )
      def test_read_non_existent(self, reader, module, error_class, fn_ext):
          pytest.importorskip(module)
  
          path = os.path.join(HERE, "data", "does_not_exist." + fn_ext)
          msg1 = fr"File (b')?.+does_not_exist\.{fn_ext}'? does not exist"
          msg2 = fr"\[Errno 2\] No such file or directory: '.+does_not_exist\.{fn_ext}'"
          msg3 = "Expected object or value"
          msg4 = "path_or_buf needs to be a string file path or file-like"
          msg5 = (
              fr"\[Errno 2\] File .+does_not_exist\.{fn_ext} does not exist: "
              fr"'.+does_not_exist\.{fn_ext}'"
          )
          msg6 = fr"\[Errno 2\] 没有那个文件或目录: '.+does_not_exist\.{fn_ext}'"
          msg7 = (
              fr"\[Errno 2\] File o directory non esistente: '.+does_not_exist\.{fn_ext}'"
          )
          msg8 = fr"Failed to open local file.+does_not_exist\.{fn_ext}"
  
          with pytest.raises(
              error_class,
              match=fr"({msg1}|{msg2}|{msg3}|{msg4}|{msg5}|{msg6}|{msg7}|{msg8})",
          ):
              reader(path)
  
      @pytest.mark.parametrize(
          "method, module, error_class, fn_ext",
          [
              (pd.DataFrame.to_csv, "os", OSError, "csv"),
              (pd.DataFrame.to_html, "os", OSError, "html"),
              (pd.DataFrame.to_excel, "xlrd", OSError, "xlsx"),
              (pd.DataFrame.to_feather, "pyarrow", OSError, "feather"),
              (pd.DataFrame.to_parquet, "pyarrow", OSError, "parquet"),
              (pd.DataFrame.to_stata, "os", OSError, "dta"),
              (pd.DataFrame.to_json, "os", OSError, "json"),
              (pd.DataFrame.to_pickle, "os", OSError, "pickle"),
          ],
      )
      # NOTE: Missing parent directory for pd.DataFrame.to_hdf is handled by PyTables
      def test_write_missing_parent_directory(self, method, module, error_class, fn_ext):
          pytest.importorskip(module)
  
          dummy_frame = pd.DataFrame({"a": [1, 2, 3], "b": [2, 3, 4], "c": [3, 4, 5]})
  
          path = os.path.join(HERE, "data", "missing_folder", "does_not_exist." + fn_ext)
  
          with pytest.raises(
              error_class,
              match=r"Cannot save file into a non-existent directory: .*missing_folder",
          ):
              method(dummy_frame, path)
  
      @pytest.mark.parametrize(
          "reader, module, error_class, fn_ext",
          [
              (pd.read_csv, "os", FileNotFoundError, "csv"),
              (pd.read_table, "os", FileNotFoundError, "csv"),
              (pd.read_fwf, "os", FileNotFoundError, "txt"),
              (pd.read_excel, "xlrd", FileNotFoundError, "xlsx"),
              (pd.read_feather, "pyarrow", OSError, "feather"),
              (pd.read_hdf, "tables", FileNotFoundError, "h5"),
              (pd.read_stata, "os", FileNotFoundError, "dta"),
              (pd.read_sas, "os", FileNotFoundError, "sas7bdat"),
              (pd.read_json, "os", ValueError, "json"),
              (pd.read_pickle, "os", FileNotFoundError, "pickle"),
          ],
      )
      def test_read_expands_user_home_dir(
          self, reader, module, error_class, fn_ext, monkeypatch
      ):
          pytest.importorskip(module)
  
          path = os.path.join("~", "does_not_exist." + fn_ext)
          monkeypatch.setattr(icom, "_expand_user", lambda x: os.path.join("foo", x))
  
          msg1 = fr"File (b')?.+does_not_exist\.{fn_ext}'? does not exist"
          msg2 = fr"\[Errno 2\] No such file or directory: '.+does_not_exist\.{fn_ext}'"
          msg3 = "Unexpected character found when decoding 'false'"
          msg4 = "path_or_buf needs to be a string file path or file-like"
          msg5 = (
              fr"\[Errno 2\] File .+does_not_exist\.{fn_ext} does not exist: "
              fr"'.+does_not_exist\.{fn_ext}'"
          )
          msg6 = fr"\[Errno 2\] 没有那个文件或目录: '.+does_not_exist\.{fn_ext}'"
          msg7 = (
              fr"\[Errno 2\] File o directory non esistente: '.+does_not_exist\.{fn_ext}'"
          )
          msg8 = fr"Failed to open local file.+does_not_exist\.{fn_ext}"
  
          with pytest.raises(
              error_class,
              match=fr"({msg1}|{msg2}|{msg3}|{msg4}|{msg5}|{msg6}|{msg7}|{msg8})",
          ):
              reader(path)
  
      @pytest.mark.parametrize(
          "reader, module, path",
          [
              (pd.read_csv, "os", ("io", "data", "csv", "iris.csv")),
              (pd.read_table, "os", ("io", "data", "csv", "iris.csv")),
              (
                  pd.read_fwf,
                  "os",
                  ("io", "data", "fixed_width", "fixed_width_format.txt"),
              ),
              (pd.read_excel, "xlrd", ("io", "data", "excel", "test1.xlsx")),
              (
                  pd.read_feather,
                  "pyarrow",
                  ("io", "data", "feather", "feather-0_3_1.feather"),
              ),
              (
                  pd.read_hdf,
                  "tables",
                  ("io", "data", "legacy_hdf", "datetimetz_object.h5"),
              ),
              (pd.read_stata, "os", ("io", "data", "stata", "stata10_115.dta")),
              (pd.read_sas, "os", ("io", "sas", "data", "test1.sas7bdat")),
              (pd.read_json, "os", ("io", "json", "data", "tsframe_v012.json")),
              (
                  pd.read_pickle,
                  "os",
                  ("io", "data", "pickle", "categorical.0.25.0.pickle"),
              ),
          ],
      )
      @pytest.mark.filterwarnings(
          "ignore:CategoricalBlock is deprecated:DeprecationWarning"
      )
      @pytest.mark.filterwarnings(  # pytables np.object usage
          "ignore:`np.object` is a deprecated alias:DeprecationWarning"
      )
      def test_read_fspath_all(self, reader, module, path, datapath):
          pytest.importorskip(module)
          path = datapath(*path)
  
          mypath = CustomFSPath(path)
          result = reader(mypath)
          expected = reader(path)
  
          if path.endswith(".pickle"):
              # categorical
              tm.assert_categorical_equal(result, expected)
          else:
              tm.assert_frame_equal(result, expected)
  
      @pytest.mark.filterwarnings("ignore:In future versions `DataFrame.to_latex`")
      @pytest.mark.parametrize(
          "writer_name, writer_kwargs, module",
          [
              ("to_csv", {}, "os"),
              ("to_excel", {"engine": "xlwt"}, "xlwt"),
              ("to_feather", {}, "pyarrow"),
              ("to_html", {}, "os"),
              ("to_json", {}, "os"),
              ("to_latex", {}, "os"),
              ("to_pickle", {}, "os"),
              ("to_stata", {"time_stamp": pd.to_datetime("2019-01-01 00:00")}, "os"),
          ],
      )
      def test_write_fspath_all(self, writer_name, writer_kwargs, module):
          p1 = tm.ensure_clean("string")
          p2 = tm.ensure_clean("fspath")
          df = pd.DataFrame({"A": [1, 2]})
  
          with p1 as string, p2 as fspath:
              pytest.importorskip(module)
              mypath = CustomFSPath(fspath)
              writer = getattr(df, writer_name)
  
              writer(string, **writer_kwargs)
              with open(string, "rb") as f:
                  expected = f.read()
  
              writer(mypath, **writer_kwargs)
              with open(fspath, "rb") as f:
                  result = f.read()
  
              assert result == expected
  
      @pytest.mark.filterwarnings(  # pytables np.object usage
          "ignore:`np.object` is a deprecated alias:DeprecationWarning"
      )
      def test_write_fspath_hdf5(self):
          # Same test as write_fspath_all, except HDF5 files aren't
          # necessarily byte-for-byte identical for a given dataframe, so we'll
          # have to read and compare equality
          pytest.importorskip("tables")
  
          df = pd.DataFrame({"A": [1, 2]})
          p1 = tm.ensure_clean("string")
          p2 = tm.ensure_clean("fspath")
  
          with p1 as string, p2 as fspath:
              mypath = CustomFSPath(fspath)
              df.to_hdf(mypath, key="bar")
              df.to_hdf(string, key="bar")
  
              result = pd.read_hdf(fspath, key="bar")
              expected = pd.read_hdf(string, key="bar")
  
          tm.assert_frame_equal(result, expected)
  
  
  @pytest.fixture
  def mmap_file(datapath):
      return datapath("io", "data", "csv", "test_mmap.csv")
  
  
  class TestMMapWrapper:
      def test_constructor_bad_file(self, mmap_file):
          non_file = StringIO("I am not a file")
          non_file.fileno = lambda: -1
  
          # the error raised is different on Windows
          if is_platform_windows():
              msg = "The parameter is incorrect"
              err = OSError
          else:
              msg = "[Errno 22]"
              err = mmap.error
  
          with pytest.raises(err, match=msg):
              icom._MMapWrapper(non_file)
  
          target = open(mmap_file)
          target.close()
  
          msg = "I/O operation on closed file"
          with pytest.raises(ValueError, match=msg):
              icom._MMapWrapper(target)
  
      def test_get_attr(self, mmap_file):
          with open(mmap_file) as target:
              wrapper = icom._MMapWrapper(target)
  
          attrs = dir(wrapper.mmap)
          attrs = [attr for attr in attrs if not attr.startswith("__")]
          attrs.append("__next__")
  
          for attr in attrs:
              assert hasattr(wrapper, attr)
  
          assert not hasattr(wrapper, "foo")
  
      def test_next(self, mmap_file):
          with open(mmap_file) as target:
              wrapper = icom._MMapWrapper(target)
              lines = target.readlines()
  
          for line in lines:
              next_line = next(wrapper)
              assert next_line.strip() == line.strip()
  
          with pytest.raises(StopIteration, match=r"^$"):
              next(wrapper)
  
      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")
  
      def test_binary_mode(self):
          """
          'encoding' shouldn't be passed to 'open' in binary mode.
  
          GH 35058
          """
          with tm.ensure_clean() as path:
              df = tm.makeDataFrame()
              df.to_csv(path, mode="w+b")
              tm.assert_frame_equal(df, pd.read_csv(path, index_col=0))
  
      @pytest.mark.parametrize("encoding", ["utf-16", "utf-32"])
      @pytest.mark.parametrize("compression_", ["bz2", "xz"])
      def test_warning_missing_utf_bom(self, encoding, compression_):
          """
          bz2 and xz do not write the byte order mark (BOM) for utf-16/32.
  
          https://stackoverflow.com/questions/55171439
  
          GH 35681
          """
          df = tm.makeDataFrame()
          with tm.ensure_clean() as path:
              with tm.assert_produces_warning(UnicodeWarning):
                  df.to_csv(path, compression=compression_, encoding=encoding)
  
              # reading should fail (otherwise we wouldn't need the warning)
              msg = r"UTF-\d+ stream does not start with BOM"
              with pytest.raises(UnicodeError, match=msg):
                  pd.read_csv(path, compression=compression_, encoding=encoding)
  
  
  def test_is_fsspec_url():
      assert icom.is_fsspec_url("gcs://pandas/somethingelse.com")
      assert icom.is_fsspec_url("gs://pandas/somethingelse.com")
      # the following is the only remote URL that is handled without fsspec
      assert not icom.is_fsspec_url("http://pandas/somethingelse.com")
      assert not icom.is_fsspec_url("random:pandas/somethingelse.com")
      assert not icom.is_fsspec_url("/local/path")
      assert not icom.is_fsspec_url("relative/local/path")
  
  
  @pytest.mark.parametrize("encoding", [None, "utf-8"])
  @pytest.mark.parametrize("format", ["csv", "json"])
  def test_codecs_encoding(encoding, format):
      # GH39247
      expected = tm.makeDataFrame()
      with tm.ensure_clean() as path:
          with codecs.open(path, mode="w", encoding=encoding) as handle:
              getattr(expected, f"to_{format}")(handle)
          with codecs.open(path, mode="r", encoding=encoding) as handle:
              if format == "csv":
                  df = pd.read_csv(handle, index_col=0)
              else:
                  df = pd.read_json(handle)
      tm.assert_frame_equal(expected, df)
  
  
  def test_codecs_get_writer_reader():
      # GH39247
      expected = tm.makeDataFrame()
      with tm.ensure_clean() as path:
          with open(path, "wb") as handle:
              with codecs.getwriter("utf-8")(handle) as encoded:
                  expected.to_csv(encoded)
          with open(path, "rb") as handle:
              with codecs.getreader("utf-8")(handle) as encoded:
                  df = pd.read_csv(encoded, index_col=0)
      tm.assert_frame_equal(expected, df)
  
  
  @pytest.mark.parametrize(
      "io_class,mode,msg",
      [
          (BytesIO, "t", "a bytes-like object is required, not 'str'"),
          (StringIO, "b", "string argument expected, got 'bytes'"),
      ],
  )
  def test_explicit_encoding(io_class, mode, msg):
      # GH39247; this test makes sure that if a user provides mode="*t" or "*b",
      # it is used. In the case of this test it leads to an error as intentionally the
      # wrong mode is requested
      expected = tm.makeDataFrame()
      with io_class() as buffer:
          with pytest.raises(TypeError, match=msg):
              expected.to_csv(buffer, mode=f"w{mode}")
  
  
  @pytest.mark.parametrize("encoding_errors", [None, "strict", "replace"])
  @pytest.mark.parametrize("format", ["csv", "json"])
  def test_encoding_errors(encoding_errors, format):
      # GH39450
      msg = "'utf-8' codec can't decode byte"
      bad_encoding = b"\xe4"
  
      if format == "csv":
          return
          content = bad_encoding + b"\n" + bad_encoding
          reader = pd.read_csv
      else:
          content = (
              b'{"'
              + bad_encoding * 2
              + b'": {"'
              + bad_encoding
              + b'":"'
              + bad_encoding
              + b'"}}'
          )
          reader = partial(pd.read_json, orient="index")
      with tm.ensure_clean() as path:
          file = Path(path)
          file.write_bytes(content)
  
          if encoding_errors != "replace":
              with pytest.raises(UnicodeDecodeError, match=msg):
                  reader(path, encoding_errors=encoding_errors)
          else:
              df = reader(path, encoding_errors=encoding_errors)
              decoded = bad_encoding.decode(errors=encoding_errors)
              expected = pd.DataFrame({decoded: [decoded]}, index=[decoded * 2])
              tm.assert_frame_equal(df, expected)
  
  
  def test_bad_encdoing_errors():
      # GH 39777
      with tm.ensure_clean() as path:
          with pytest.raises(LookupError, match="unknown error handler name"):
              icom.get_handle(path, "w", errors="bad")
  
  
  def test_errno_attribute():
      # GH 13872
      with pytest.raises(FileNotFoundError, match="\\[Errno 2\\]") as err:
          pd.read_csv("doesnt_exist")
          assert err.errno == errno.ENOENT
  
  
  def test_fail_mmap():
      with pytest.raises(UnsupportedOperation, match="fileno"):
          with BytesIO() as buffer:
              icom.get_handle(buffer, "rb", memory_map=True)
  
apis: pandas.UInt64Dtype pandas.io.common.is_fsspec_url pandas.util._decorators.doc pandas.UInt16Dtype pandas.UInt32Dtype pandas.Int64Dtype pandas.get_option pandas.io.common.get_handle pandas.StringDtype pandas.UInt8Dtype pandas.io.common.is_url pandas.Int32Dtype pandas.util.version.Version pandas.io.common.stringify_path pandas.Int8Dtype pandas.compat._optional.import_optional_dependency pandas.errors.AbstractMethodError pandas.BooleanDtype pandas.Int16Dtype
apis: pandas._testing.ensure_clean pandas.compat.is_platform_windows pandas._testing.assert_frame_equal pandas._testing.makeDataFrame pandas.io.common.is_fsspec_url pandas.io.common._expand_user pandas.util._test_decorators.skip_if_no pandas.io.common.get_handle pandas._testing.assert_produces_warning pandas.read_json pandas.to_datetime pandas.read_hdf pandas.read_csv pandas._testing.assert_categorical_equal pandas.concat pandas.io.common.stringify_path pandas.DataFrame pandas.io.common._MMapWrapper pandas.io.common.infer_compression