def __new__( cls, path: FilePathOrBuffer | ExcelWriter, engine=None, date_format=None, datetime_format=None, mode: str = "w", storage_options: StorageOptions = None, if_sheet_exists: str | None = None, engine_kwargs: dict | None = None, **kwargs, ): if kwargs: if engine_kwargs is not None: raise ValueError("Cannot use both engine_kwargs and **kwargs") warnings.warn( "Use of **kwargs is deprecated, use engine_kwargs instead.", FutureWarning, stacklevel=2, ) # only switch class if generic(ExcelWriter) if cls is ExcelWriter: if engine is None or (isinstance(engine, str) and engine == "auto"): if isinstance(path, str): ext = os.path.splitext(path)[-1][1:] else: ext = "xlsx" try: engine = config.get_option(f"io.excel.{ext}.writer", silent=True) if engine == "auto": engine = get_default_engine(ext, mode="writer") except KeyError as err: raise ValueError(f"No engine for filetype: '{ext}'") from err if engine == "xlwt": xls_config_engine = config.get_option( "io.excel.xls.writer", silent=True ) # Don't warn a 2nd time if user has changed the default engine for xls if xls_config_engine != "xlwt": warnings.warn( "As the xlwt package is no longer maintained, the xlwt " "engine will be removed in a future version of pandas. " "This is the only engine in pandas that supports writing " "in the xls format. Install openpyxl and write to an xlsx " "file instead. You can set the option io.excel.xls.writer " "to 'xlwt' to silence this warning. While this option is " "deprecated and will also raise a warning, it can " "be globally set and the warning suppressed.", FutureWarning, stacklevel=4, ) cls = get_writer(engine) return object.__new__(cls)
def register_converter_cb(key): from pandas.plotting import register_matplotlib_converters from pandas.plotting import deregister_matplotlib_converters if cf.get_option(key): register_matplotlib_converters() else: deregister_matplotlib_converters()
def register_converter_cb(key) -> None: from pandas.plotting import ( deregister_matplotlib_converters, register_matplotlib_converters, ) if cf.get_option(key): register_matplotlib_converters() else: deregister_matplotlib_converters()
def _deprecate_negative_int_max_colwidth(key): value = cf.get_option(key) if value is not None and value < 0: warnings.warn( "Passing a negative integer is deprecated in version 1.0 and " "will not be supported in future version. Instead, use None " "to not limit the column width.", FutureWarning, stacklevel=4, )
def test_repr_binary_type(): import string letters = string.ascii_letters try: raw = bytes(letters, encoding=cf.get_option('display.encoding')) except TypeError: raw = bytes(letters) b = str(raw.decode('utf-8')) res = printing.pprint_thing(b, quote_strings=True) assert res == repr(b) res = printing.pprint_thing(b, quote_strings=False) assert res == b
def test_repr_binary_type(): import string letters = string.ascii_letters try: raw = bytes(letters, encoding=cf.get_option('display.encoding')) except TypeError: raw = bytes(letters) b = str(compat.bytes_to_str(raw)) res = printing.pprint_thing(b, quote_strings=True) assert res == repr(b) res = printing.pprint_thing(b, quote_strings=False) assert res == b
def test_repr_binary_type(): import string letters = string.ascii_letters btype = compat.binary_type try: raw = btype(letters, encoding=cf.get_option('display.encoding')) except TypeError: raw = btype(letters) b = compat.text_type(compat.bytes_to_str(raw)) res = printing.pprint_thing(b, quote_strings=True) assert res == repr(b) res = printing.pprint_thing(b, quote_strings=False) assert res == b
def register_plotting_backend_cb(key): backend_str = cf.get_option(key) if backend_str == 'matplotlib': try: import pandas.plotting._matplotlib # noqa except ImportError: raise ImportError('matplotlib is required for plotting when the ' 'default backend "matplotlib" is selected.') else: return try: importlib.import_module(backend_str) except ImportError: raise ValueError('"{}" does not seem to be an installed module. ' 'A pandas plotting backend must be a module that ' 'can be imported'.format(backend_str))
def __new__(cls, path, engine=None, **kwargs): # only switch class if generic(ExcelWriter) if cls is ExcelWriter: if engine is None or (isinstance(engine, str) and engine == "auto"): if isinstance(path, str): ext = os.path.splitext(path)[-1][1:] else: ext = "xlsx" try: engine = config.get_option(f"io.excel.{ext}.writer") if engine == "auto": engine = get_default_writer(ext) except KeyError as err: raise ValueError(f"No engine for filetype: '{ext}'") from err cls = get_writer(engine) return object.__new__(cls)
def __new__(cls, path, engine=None, **kwargs): # only switch class if generic(ExcelWriter) if issubclass(cls, ExcelWriter): if engine is None or (isinstance(engine, string_types) and engine == 'auto'): if isinstance(path, string_types): ext = os.path.splitext(path)[-1][1:] else: ext = 'xlsx' try: engine = config.get_option( 'io.excel.{ext}.writer'.format(ext=ext)) if engine == 'auto': engine = _get_default_writer(ext) except KeyError: raise ValueError( "No engine for filetype: '{ext}'".format(ext=ext)) cls = get_writer(engine) return object.__new__(cls)
def __new__(cls, path, engine=None, **kwargs): # only switch class if generic(ExcelWriter) if issubclass(cls, ExcelWriter): if engine is None or (isinstance(engine, str) and engine == 'auto'): if isinstance(path, str): ext = os.path.splitext(path)[-1][1:] else: ext = 'xlsx' try: engine = config.get_option('io.excel.{ext}.writer' .format(ext=ext)) if engine == 'auto': engine = _get_default_writer(ext) except KeyError: raise ValueError("No engine for filetype: '{ext}'" .format(ext=ext)) cls = get_writer(engine) return object.__new__(cls)
def __init__(self, path_or_buffer, engine=None, storage_options: StorageOptions = None): if engine is not None and engine not in self._engines: raise ValueError(f"Unknown engine: {engine}") # Could be a str, ExcelFile, Book, etc. self.io = path_or_buffer # Always a string self._io = stringify_path(path_or_buffer) # Determine xlrd version if installed if (import_optional_dependency( "xlrd", raise_on_missing=False, on_version="ignore") is None): xlrd_version = None else: import xlrd xlrd_version = LooseVersion(xlrd.__version__) if xlrd_version is not None and isinstance(path_or_buffer, xlrd.Book): ext = "xls" else: ext = inspect_excel_format(content=path_or_buffer, storage_options=storage_options) if engine is None: # ext will always be valid, otherwise inspect_excel_format would raise engine = config.get_option(f"io.excel.{ext}.reader", silent=True) if engine == "auto": engine = get_default_engine(ext, mode="reader") if engine == "xlrd" and ext != "xls" and xlrd_version is not None: if xlrd_version >= "2": raise ValueError( f"Your version of xlrd is {xlrd_version}. In xlrd >= 2.0, " f"only the xls format is supported. Install openpyxl instead." ) else: caller = inspect.stack()[1] if (caller.filename.endswith( os.path.join("pandas", "io", "excel", "_base.py")) and caller.function == "read_excel"): stacklevel = 4 else: stacklevel = 2 warnings.warn( f"Your version of xlrd is {xlrd_version}. In xlrd >= 2.0, " f"only the xls format is supported. As a result, the " f"openpyxl engine will be used if it is installed and the " f"engine argument is not specified. Install " f"openpyxl instead.", FutureWarning, stacklevel=stacklevel, ) self.engine = engine self.storage_options = storage_options self._reader = self._engines[engine](self._io, storage_options=storage_options)
def use_numexpr_cb(key): from pandas.core.computation import expressions expressions.set_use_numexpr(cf.get_option(key))
def use_bottleneck_cb(key): from pandas.core import nanops nanops.set_use_bottleneck(cf.get_option(key))
def table_schema_cb(key): from pandas.io.formats.printing import _enable_data_resource_formatter _enable_data_resource_formatter(cf.get_option(key))
def __init__(self, path_or_buffer, engine=None, storage_options: StorageOptions = None): if engine is not None and engine not in self._engines: raise ValueError(f"Unknown engine: {engine}") # Could be a str, ExcelFile, Book, etc. self.io = path_or_buffer # Always a string self._io = stringify_path(path_or_buffer) # Determine xlrd version if installed if import_optional_dependency("xlrd", errors="ignore") is None: xlrd_version = None else: import xlrd xlrd_version = Version(get_version(xlrd)) ext = None if engine is None: # Only determine ext if it is needed if xlrd_version is not None and isinstance(path_or_buffer, xlrd.Book): ext = "xls" else: ext = inspect_excel_format(content_or_path=path_or_buffer, storage_options=storage_options) if ext is None: raise ValueError( "Excel file format cannot be determined, you must specify " "an engine manually.") engine = config.get_option(f"io.excel.{ext}.reader", silent=True) if engine == "auto": engine = get_default_engine(ext, mode="reader") if engine == "xlrd" and xlrd_version is not None: if ext is None: # Need ext to determine ext in order to raise/warn if isinstance(path_or_buffer, xlrd.Book): ext = "xls" else: ext = inspect_excel_format(path_or_buffer, storage_options=storage_options) # Pass through if ext is None, otherwise check if ext valid for xlrd if ext and ext != "xls" and xlrd_version >= Version("2"): raise ValueError( f"Your version of xlrd is {xlrd_version}. In xlrd >= 2.0, " f"only the xls format is supported. Install openpyxl instead." ) elif ext and ext != "xls": stacklevel = find_stack_level() warnings.warn( f"Your version of xlrd is {xlrd_version}. In xlrd >= 2.0, " f"only the xls format is supported. Install " f"openpyxl instead.", FutureWarning, stacklevel=stacklevel, ) self.engine = engine self.storage_options = storage_options self._reader = self._engines[engine](self._io, storage_options=storage_options)
def text_adjustment_init(self): self.ansi_regx = re.compile(r"\x1B[@-_][0-?]*[ -/]*[@-~]") self.encoding = get_option("display.encoding")
def use_numba_cb(key): from pandas.core.util import numba_ numba_.set_use_numba(cf.get_option(key))
def text_adjustment_init(self): """Adjust text monkey patch for Pandas""" self.ansi_regx = re.compile(r"\x1B[@-_][0-?]*[ -/]*[@-~]") self.encoding = get_option("display.encoding")