Skip to content

Commit 00f7b15

Browse files
committed
Fixing implementation per reviewer recommendations.
1 parent 6f15924 commit 00f7b15

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

pandas/io/xml.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,9 +1128,10 @@ def read_xml(
11281128
check_dtype_backend(dtype_backend)
11291129

11301130
if (
1131-
isinstance(path_or_buffer, str)
1132-
and not is_file_like(path_or_buffer)
1133-
and "\n" in path_or_buffer
1131+
not is_file_like(path_or_buffer)
1132+
and not file_exists(path_or_buffer)
1133+
and not is_url(path_or_buffer)
1134+
and not is_fsspec_url(path_or_buffer)
11341135
):
11351136
warnings.warn(
11361137
"Passing literal xml to 'read_xml' is deprecated and "

pandas/tests/io/xml/test_xml.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ def test_file_buffered_reader_no_xml_declaration(xml_books, parser, mode):
452452
def test_string_charset(parser):
453453
txt = "<中文標籤><row><c1>1</c1><c2>2</c2></row></中文標籤>"
454454

455-
df_str = read_xml(txt, parser=parser)
455+
df_str = read_xml(StringIO(txt), parser=parser)
456456

457457
df_expected = DataFrame({"c1": 1, "c2": 2}, index=[0])
458458

@@ -510,34 +510,48 @@ def test_empty_string_lxml(val):
510510
]
511511
)
512512
with pytest.raises(XMLSyntaxError, match=msg):
513-
read_xml(val, parser="lxml")
513+
if isinstance(val, str):
514+
read_xml(StringIO(val), parser="lxml")
515+
else:
516+
read_xml(BytesIO(val), parser="lxml")
514517

515518

516519
@pytest.mark.parametrize("val", ["", b""])
517520
def test_empty_string_etree(val):
518521
with pytest.raises(ParseError, match="no element found"):
519-
read_xml(val, parser="etree")
522+
if isinstance(val, str):
523+
read_xml(StringIO(val), parser="etree")
524+
else:
525+
read_xml(BytesIO(val), parser="etree")
520526

521527

522528
@td.skip_if_no("lxml")
523529
def test_wrong_file_path_lxml():
524-
from lxml.etree import XMLSyntaxError
525-
530+
msg = (
531+
"Passing literal xml to 'read_xml' is deprecated and "
532+
"will be removed in a future version. To read from a "
533+
"literal string, wrap it in a 'StringIO' object."
534+
)
526535
filename = os.path.join("data", "html", "books.xml")
527536

528537
with pytest.raises(
529-
XMLSyntaxError,
530-
match=("Start tag expected, '<' not found"),
538+
FutureWarning,
539+
match=msg,
531540
):
532541
read_xml(filename, parser="lxml")
533542

534543

535544
def test_wrong_file_path_etree():
545+
msg = (
546+
"Passing literal xml to 'read_xml' is deprecated and "
547+
"will be removed in a future version. To read from a "
548+
"literal string, wrap it in a 'StringIO' object."
549+
)
536550
filename = os.path.join("data", "html", "books.xml")
537551

538552
with pytest.raises(
539-
ParseError,
540-
match=("not well-formed"),
553+
FutureWarning,
554+
match=msg,
541555
):
542556
read_xml(filename, parser="etree")
543557

@@ -1223,8 +1237,8 @@ def test_style_charset():
12231237
12241238
</xsl:stylesheet>"""
12251239

1226-
df_orig = read_xml(xml)
1227-
df_style = read_xml(xml, stylesheet=xsl)
1240+
df_orig = read_xml(StringIO(xml))
1241+
df_style = read_xml(StringIO(xml), stylesheet=xsl)
12281242

12291243
tm.assert_frame_equal(df_orig, df_style)
12301244

0 commit comments

Comments
 (0)