Skip to content

Commit 417c3d0

Browse files
authored
ENH: Synchronize XML (#207)
* ENH: Synchronize XML Synchronize to and read XML to upstream * TST: Add tests for read_xml and to_xml * MAINT: Cap lxml * ENH: Improve specificity of types Remove erroneous Nones
1 parent a45600d commit 417c3d0

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

pandas-stubs/core/frame.pyi

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ from pandas._typing import (
6969
MaskType,
7070
MergeHow,
7171
NaPosition,
72+
ReadBuffer,
7273
Renamer,
7374
ReplaceMethod,
7475
Scalar,
@@ -81,6 +82,7 @@ from pandas._typing import (
8182
T as TType,
8283
TimestampConvention,
8384
WriteBuffer,
85+
XMLParsers,
8486
np_ndarray_bool,
8587
np_ndarray_str,
8688
num,
@@ -345,6 +347,46 @@ class DataFrame(NDFrame, OpsMixin):
345347
render_links: _bool = ...,
346348
encoding: _str | None = ...,
347349
) -> _str: ...
350+
@overload
351+
def to_xml(
352+
self,
353+
path_or_buffer: FilePath | WriteBuffer[bytes] | WriteBuffer[str],
354+
index: bool = ...,
355+
root_name: str = ...,
356+
row_name: str = ...,
357+
na_rep: str | None = ...,
358+
attr_cols: list[HashableT] | None = ...,
359+
elem_cols: list[HashableT] | None = ...,
360+
namespaces: dict[str | None, str] | None = ...,
361+
prefix: str | None = ...,
362+
encoding: str = ...,
363+
xml_declaration: bool = ...,
364+
pretty_print: bool = ...,
365+
parser: XMLParsers = ...,
366+
stylesheet: FilePath | ReadBuffer[str] | ReadBuffer[bytes] | None = ...,
367+
compression: CompressionOptions = ...,
368+
storage_options: StorageOptions = ...,
369+
) -> None: ...
370+
@overload
371+
def to_xml(
372+
self,
373+
path_or_buffer: Literal[None] = ...,
374+
index: bool = ...,
375+
root_name: str | None = ...,
376+
row_name: str | None = ...,
377+
na_rep: str | None = ...,
378+
attr_cols: list[HashableT] | None = ...,
379+
elem_cols: list[HashableT] | None = ...,
380+
namespaces: dict[str | None, str] | None = ...,
381+
prefix: str | None = ...,
382+
encoding: str = ...,
383+
xml_declaration: bool | None = ...,
384+
pretty_print: bool | None = ...,
385+
parser: str | None = ...,
386+
stylesheet: FilePath | ReadBuffer[str] | ReadBuffer[bytes] | None = ...,
387+
compression: CompressionOptions = ...,
388+
storage_options: StorageOptions = ...,
389+
) -> str: ...
348390
def info(
349391
self, verbose=..., buf=..., max_cols=..., memory_usage=..., null_counts=...
350392
) -> None: ...

pandas-stubs/io/xml.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def read_xml(
2525
parse_dates: ParseDatesArg | None = ...,
2626
# encoding can not be None for lxml and StringIO input
2727
encoding: str | None = ...,
28-
parser: XMLParsers | None = ...,
28+
parser: XMLParsers = ...,
2929
stylesheet: FilePath | ReadBuffer[bytes] | ReadBuffer[str] | None = ...,
3030
iterparse: dict[str, list[str]] | None = ...,
31-
compression: CompressionOptions | None = ...,
32-
storage_options: StorageOptions | None = ...,
31+
compression: CompressionOptions = ...,
32+
storage_options: StorageOptions = ...,
3333
) -> DataFrame: ...

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pre-commit = ">=2.19.0"
4747
black = ">=22.6.0"
4848
isort = ">=5.10.1"
4949
openpyxl = ">=3.0.10"
50+
lxml = ">=4.7.1,<4.9.0"
5051

5152
[build-system]
5253
requires = ["poetry-core>=1.0.0"]

tests/test_io.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import io
2+
13
import pandas as pd
24
from pandas import (
35
DataFrame,
46
read_clipboard,
57
read_stata,
8+
read_xml,
69
)
710
from pandas._testing import ensure_clean
811
import pytest
@@ -17,6 +20,21 @@
1720
DF = DataFrame({"a": [1, 2, 3], "b": [0.0, 0.0, 0.0]})
1821

1922

23+
def test_xml():
24+
with ensure_clean() as path:
25+
check(assert_type(DF.to_xml(path), None), type(None))
26+
check(assert_type(read_xml(path), DataFrame), DataFrame)
27+
with open(path, "rb") as f:
28+
check(assert_type(read_xml(f), DataFrame), DataFrame)
29+
30+
31+
def test_xml_str():
32+
with ensure_clean() as path:
33+
check(assert_type(DF.to_xml(), str), str)
34+
out: str = DF.to_xml()
35+
check(assert_type(read_xml(io.StringIO(out)), DataFrame), DataFrame)
36+
37+
2038
def test_read_stata_df():
2139
with ensure_clean() as path:
2240
DF.to_stata(path)

0 commit comments

Comments
 (0)