diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index 014fe8207..5896156c8 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -69,6 +69,7 @@ from pandas._typing import ( MaskType, MergeHow, NaPosition, + ReadBuffer, Renamer, ReplaceMethod, Scalar, @@ -81,6 +82,7 @@ from pandas._typing import ( T as TType, TimestampConvention, WriteBuffer, + XMLParsers, np_ndarray_bool, np_ndarray_str, num, @@ -345,6 +347,46 @@ class DataFrame(NDFrame, OpsMixin): render_links: _bool = ..., encoding: _str | None = ..., ) -> _str: ... + @overload + def to_xml( + self, + path_or_buffer: FilePath | WriteBuffer[bytes] | WriteBuffer[str], + index: bool = ..., + root_name: str = ..., + row_name: str = ..., + na_rep: str | None = ..., + attr_cols: list[HashableT] | None = ..., + elem_cols: list[HashableT] | None = ..., + namespaces: dict[str | None, str] | None = ..., + prefix: str | None = ..., + encoding: str = ..., + xml_declaration: bool = ..., + pretty_print: bool = ..., + parser: XMLParsers = ..., + stylesheet: FilePath | ReadBuffer[str] | ReadBuffer[bytes] | None = ..., + compression: CompressionOptions = ..., + storage_options: StorageOptions = ..., + ) -> None: ... + @overload + def to_xml( + self, + path_or_buffer: Literal[None] = ..., + index: bool = ..., + root_name: str | None = ..., + row_name: str | None = ..., + na_rep: str | None = ..., + attr_cols: list[HashableT] | None = ..., + elem_cols: list[HashableT] | None = ..., + namespaces: dict[str | None, str] | None = ..., + prefix: str | None = ..., + encoding: str = ..., + xml_declaration: bool | None = ..., + pretty_print: bool | None = ..., + parser: str | None = ..., + stylesheet: FilePath | ReadBuffer[str] | ReadBuffer[bytes] | None = ..., + compression: CompressionOptions = ..., + storage_options: StorageOptions = ..., + ) -> str: ... def info( self, verbose=..., buf=..., max_cols=..., memory_usage=..., null_counts=... ) -> None: ... diff --git a/pandas-stubs/io/xml.pyi b/pandas-stubs/io/xml.pyi index 34518284d..73d17cde7 100644 --- a/pandas-stubs/io/xml.pyi +++ b/pandas-stubs/io/xml.pyi @@ -25,9 +25,9 @@ def read_xml( parse_dates: ParseDatesArg | None = ..., # encoding can not be None for lxml and StringIO input encoding: str | None = ..., - parser: XMLParsers | None = ..., + parser: XMLParsers = ..., stylesheet: FilePath | ReadBuffer[bytes] | ReadBuffer[str] | None = ..., iterparse: dict[str, list[str]] | None = ..., - compression: CompressionOptions | None = ..., - storage_options: StorageOptions | None = ..., + compression: CompressionOptions = ..., + storage_options: StorageOptions = ..., ) -> DataFrame: ... diff --git a/pyproject.toml b/pyproject.toml index 6ecff91a7..d754b86c0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,6 +47,7 @@ pre-commit = ">=2.19.0" black = ">=22.6.0" isort = ">=5.10.1" openpyxl = ">=3.0.10" +lxml = ">=4.7.1,<4.9.0" [build-system] requires = ["poetry-core>=1.0.0"] diff --git a/tests/test_io.py b/tests/test_io.py index e63acffd5..60ab527af 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1,8 +1,11 @@ +import io + import pandas as pd from pandas import ( DataFrame, read_clipboard, read_stata, + read_xml, ) from pandas._testing import ensure_clean import pytest @@ -17,6 +20,21 @@ DF = DataFrame({"a": [1, 2, 3], "b": [0.0, 0.0, 0.0]}) +def test_xml(): + with ensure_clean() as path: + check(assert_type(DF.to_xml(path), None), type(None)) + check(assert_type(read_xml(path), DataFrame), DataFrame) + with open(path, "rb") as f: + check(assert_type(read_xml(f), DataFrame), DataFrame) + + +def test_xml_str(): + with ensure_clean() as path: + check(assert_type(DF.to_xml(), str), str) + out: str = DF.to_xml() + check(assert_type(read_xml(io.StringIO(out)), DataFrame), DataFrame) + + def test_read_stata_df(): with ensure_clean() as path: DF.to_stata(path)