Skip to content

Commit d654778

Browse files
linus-mdcbpygit
authored andcommitted
DOC: Add example with numpy_nullable to pd.read_xml() (pandas-dev#56470)
* DOC: Add example with ``numpy_nullable`` to ``pd.read_xml()`` * Update io.rst * Update io.rst * Update * Move example * fix line length * Remove trailing whitespace * Update xml.py * fix imports * Update * Formatting
1 parent 2e508e3 commit d654778

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

pandas/io/xml.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ def read_xml(
10581058
10591059
Examples
10601060
--------
1061-
>>> import io
1061+
>>> from io import StringIO
10621062
>>> xml = '''<?xml version='1.0' encoding='utf-8'?>
10631063
... <data xmlns="http://example.com">
10641064
... <row>
@@ -1078,7 +1078,7 @@ def read_xml(
10781078
... </row>
10791079
... </data>'''
10801080
1081-
>>> df = pd.read_xml(io.StringIO(xml))
1081+
>>> df = pd.read_xml(StringIO(xml))
10821082
>>> df
10831083
shape degrees sides
10841084
0 square 360 4.0
@@ -1092,7 +1092,7 @@ def read_xml(
10921092
... <row shape="triangle" degrees="180" sides="3.0"/>
10931093
... </data>'''
10941094
1095-
>>> df = pd.read_xml(io.StringIO(xml), xpath=".//row")
1095+
>>> df = pd.read_xml(StringIO(xml), xpath=".//row")
10961096
>>> df
10971097
shape degrees sides
10981098
0 square 360 4.0
@@ -1118,14 +1118,42 @@ def read_xml(
11181118
... </doc:row>
11191119
... </doc:data>'''
11201120
1121-
>>> df = pd.read_xml(io.StringIO(xml),
1121+
>>> df = pd.read_xml(StringIO(xml),
11221122
... xpath="//doc:row",
11231123
... namespaces={{"doc": "https://example.com"}})
11241124
>>> df
11251125
shape degrees sides
11261126
0 square 360 4.0
11271127
1 circle 360 NaN
11281128
2 triangle 180 3.0
1129+
1130+
>>> xml_data = '''
1131+
... <data>
1132+
... <row>
1133+
... <index>0</index>
1134+
... <a>1</a>
1135+
... <b>2.5</b>
1136+
... <c>True</c>
1137+
... <d>a</d>
1138+
... <e>2019-12-31 00:00:00</e>
1139+
... </row>
1140+
... <row>
1141+
... <index>1</index>
1142+
... <b>4.5</b>
1143+
... <c>False</c>
1144+
... <d>b</d>
1145+
... <e>2019-12-31 00:00:00</e>
1146+
... </row>
1147+
... </data>
1148+
... '''
1149+
1150+
>>> df = pd.read_xml(StringIO(xml_data),
1151+
... dtype_backend="numpy_nullable",
1152+
... parse_dates=["e"])
1153+
>>> df
1154+
index a b c d e
1155+
0 0 1 2.5 True a 2019-12-31
1156+
1 1 <NA> 4.5 False b 2019-12-31
11291157
"""
11301158
check_dtype_backend(dtype_backend)
11311159

0 commit comments

Comments
 (0)