Skip to content

Commit 29bbd6a

Browse files
authored
CLN: Remove to_xml from format.py to run directly in frame.py (#40240)
1 parent 940926f commit 29bbd6a

File tree

3 files changed

+31
-144
lines changed

3 files changed

+31
-144
lines changed

pandas/core/frame.py

+25-5
Original file line numberDiff line numberDiff line change
@@ -2820,12 +2820,31 @@ def to_xml(
28202820
</doc:data>
28212821
"""
28222822

2823-
formatter = fmt.DataFrameFormatter(
2824-
self,
2825-
index=index,
2823+
from pandas.io.formats.xml import (
2824+
EtreeXMLFormatter,
2825+
LxmlXMLFormatter,
28262826
)
28272827

2828-
return fmt.DataFrameRenderer(formatter).to_xml(
2828+
lxml = import_optional_dependency("lxml.etree", errors="ignore")
2829+
2830+
TreeBuilder: Union[Type[EtreeXMLFormatter], Type[LxmlXMLFormatter]]
2831+
2832+
if parser == "lxml":
2833+
if lxml is not None:
2834+
TreeBuilder = LxmlXMLFormatter
2835+
else:
2836+
raise ImportError(
2837+
"lxml not found, please install or use the etree parser."
2838+
)
2839+
2840+
elif parser == "etree":
2841+
TreeBuilder = EtreeXMLFormatter
2842+
2843+
else:
2844+
raise ValueError("Values for parser can only be lxml or etree.")
2845+
2846+
xml_formatter = TreeBuilder(
2847+
self,
28292848
path_or_buffer=path_or_buffer,
28302849
index=index,
28312850
root_name=root_name,
@@ -2838,12 +2857,13 @@ def to_xml(
28382857
encoding=encoding,
28392858
xml_declaration=xml_declaration,
28402859
pretty_print=pretty_print,
2841-
parser=parser,
28422860
stylesheet=stylesheet,
28432861
compression=compression,
28442862
storage_options=storage_options,
28452863
)
28462864

2865+
return xml_formatter.write_output()
2866+
28472867
# ----------------------------------------------------------------------
28482868
@Substitution(
28492869
klass="DataFrame",

pandas/io/formats/format.py

-133
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@
6161
IndexLabel,
6262
StorageOptions,
6363
)
64-
from pandas.compat._optional import import_optional_dependency
65-
from pandas.util._decorators import doc
6664

6765
from pandas.core.dtypes.common import (
6866
is_categorical_dtype,
@@ -98,7 +96,6 @@
9896
from pandas.core.indexes.datetimes import DatetimeIndex
9997
from pandas.core.indexes.timedeltas import TimedeltaIndex
10098
from pandas.core.reshape.concat import concat
101-
from pandas.core.shared_docs import _shared_docs
10299

103100
from pandas.io.common import stringify_path
104101
from pandas.io.formats.printing import (
@@ -944,7 +941,6 @@ class DataFrameRenderer:
944941
945942
Called in pandas.core.frame.DataFrame:
946943
- to_html
947-
- to_xml
948944
- to_string
949945
950946
Parameters
@@ -1037,135 +1033,6 @@ def to_html(
10371033
string = html_formatter.to_string()
10381034
return save_to_buffer(string, buf=buf, encoding=encoding)
10391035

1040-
@doc(storage_options=_shared_docs["storage_options"])
1041-
def to_xml(
1042-
self,
1043-
path_or_buffer: Optional[FilePathOrBuffer] = None,
1044-
index: Optional[bool] = True,
1045-
root_name: Optional[str] = "data",
1046-
row_name: Optional[str] = "row",
1047-
na_rep: Optional[str] = None,
1048-
attr_cols: Optional[Union[str, List[str]]] = None,
1049-
elem_cols: Optional[Union[str, List[str]]] = None,
1050-
namespaces: Optional[Dict[Optional[str], str]] = None,
1051-
prefix: Optional[str] = None,
1052-
encoding: str = "utf-8",
1053-
xml_declaration: Optional[bool] = True,
1054-
pretty_print: Optional[bool] = True,
1055-
parser: Optional[str] = "lxml",
1056-
stylesheet: Optional[FilePathOrBuffer] = None,
1057-
compression: CompressionOptions = "infer",
1058-
storage_options: StorageOptions = None,
1059-
) -> Optional[str]:
1060-
"""
1061-
Render a DataFrame to an XML document.
1062-
1063-
.. versionadded:: 1.3.0
1064-
1065-
Parameters
1066-
----------
1067-
path_or_buffer : str, path object or file-like object, optional
1068-
File to write output to. If None, the output is returned as a
1069-
string.
1070-
index : bool, default True
1071-
Whether to include index in XML document.
1072-
root_name : str, default 'data'
1073-
The name of root element in XML document.
1074-
row_name : str, default 'row'
1075-
The name of row element in XML document.
1076-
na_rep : str, optional
1077-
Missing data representation.
1078-
attr_cols : list-like, optional
1079-
List of columns to write as attributes in row element.
1080-
Hierarchical columns will be flattened with underscore
1081-
delimiting the different levels.
1082-
elem_cols : list-like, optional
1083-
List of columns to write as children in row element. By default,
1084-
all columns output as children of row element. Hierarchical
1085-
columns will be flattened with underscore delimiting the
1086-
different levels.
1087-
namespaces : dict, optional
1088-
All namespaces to be defined in root element. Keys of dict
1089-
should be prefix names and values of dict corresponding URIs.
1090-
Default namespaces should be given empty string key. For
1091-
example, ::
1092-
1093-
namespaces = {{'': 'https://example.com'}}
1094-
1095-
prefix : str, optional
1096-
Namespace prefix to be used for every element and/or attribute
1097-
in document. This should be one of the keys in ``namespaces``
1098-
dict.
1099-
encoding : str, default 'utf-8'
1100-
Encoding of the resulting document.
1101-
xml_declaration : str, optional
1102-
Whether to include the XML declaration at start of document.
1103-
pretty_print : bool, default True
1104-
Whether output should be pretty printed with indentation and
1105-
line breaks.
1106-
parser : {{'lxml','etree'}}, default "lxml"
1107-
Parser module to use for building of tree. Only 'lxml' and
1108-
'etree' are supported. With 'lxml', the ability to use XSLT
1109-
stylesheet is supported.
1110-
stylesheet : str, path object or file-like object, optional
1111-
A URL, file-like object, or a raw string containing an XSLT
1112-
script used to transform the raw XML output. Script should use
1113-
layout of elements and attributes from original output. This
1114-
argument requires ``lxml`` to be installed. Only XSLT 1.0
1115-
scripts and not later versions is currently supported.
1116-
compression : {{'infer', 'gzip', 'bz2', 'zip', 'xz', None}}, default 'infer'
1117-
For on-the-fly decompression of on-disk data. If 'infer', then use
1118-
gzip, bz2, zip or xz if path_or_buffer is a string ending in
1119-
'.gz', '.bz2', '.zip', or 'xz', respectively, and no decompression
1120-
otherwise. If using 'zip', the ZIP file must contain only one data
1121-
file to be read in. Set to None for no decompression.
1122-
{storage_options}
1123-
"""
1124-
1125-
from pandas.io.formats.xml import (
1126-
EtreeXMLFormatter,
1127-
LxmlXMLFormatter,
1128-
)
1129-
1130-
lxml = import_optional_dependency("lxml.etree", errors="ignore")
1131-
1132-
TreeBuilder: Union[Type[EtreeXMLFormatter], Type[LxmlXMLFormatter]]
1133-
1134-
if parser == "lxml":
1135-
if lxml is not None:
1136-
TreeBuilder = LxmlXMLFormatter
1137-
else:
1138-
raise ImportError(
1139-
"lxml not found, please install or use the etree parser."
1140-
)
1141-
1142-
elif parser == "etree":
1143-
TreeBuilder = EtreeXMLFormatter
1144-
1145-
else:
1146-
raise ValueError("Values for parser can only be lxml or etree.")
1147-
1148-
xml_formatter = TreeBuilder(
1149-
self.fmt,
1150-
path_or_buffer=path_or_buffer,
1151-
index=index,
1152-
root_name=root_name,
1153-
row_name=row_name,
1154-
na_rep=na_rep,
1155-
attr_cols=attr_cols,
1156-
elem_cols=elem_cols,
1157-
namespaces=namespaces,
1158-
prefix=prefix,
1159-
encoding=encoding,
1160-
xml_declaration=xml_declaration,
1161-
pretty_print=pretty_print,
1162-
stylesheet=stylesheet,
1163-
compression=compression,
1164-
storage_options=storage_options,
1165-
)
1166-
1167-
return xml_formatter.write_output()
1168-
11691036
def to_string(
11701037
self,
11711038
buf: Optional[FilePathOrBuffer[str]] = None,

pandas/io/formats/xml.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121

2222
from pandas.core.dtypes.common import is_list_like
2323

24+
from pandas.core.frame import DataFrame
25+
2426
from pandas.io.common import get_handle
25-
from pandas.io.formats.format import DataFrameFormatter
2627
from pandas.io.xml import (
2728
get_data_from_filepath,
2829
preprocess_data,
@@ -93,7 +94,7 @@ class BaseXMLFormatter:
9394

9495
def __init__(
9596
self,
96-
formatter: DataFrameFormatter,
97+
frame: DataFrame,
9798
path_or_buffer: Optional[FilePathOrBuffer] = None,
9899
index: Optional[bool] = True,
99100
root_name: Optional[str] = "data",
@@ -110,7 +111,7 @@ def __init__(
110111
compression: CompressionOptions = "infer",
111112
storage_options: StorageOptions = None,
112113
) -> None:
113-
self.fmt = formatter
114+
self.frame = frame
114115
self.path_or_buffer = path_or_buffer
115116
self.index = index
116117
self.root_name = root_name
@@ -127,8 +128,7 @@ def __init__(
127128
self.compression = compression
128129
self.storage_options = storage_options
129130

130-
self.frame = self.fmt.frame
131-
self.orig_cols = self.fmt.frame.columns.tolist()
131+
self.orig_cols = self.frame.columns.tolist()
132132
self.frame_dicts = self.process_dataframe()
133133

134134
def build_tree(self) -> bytes:
@@ -183,7 +183,7 @@ def process_dataframe(self) -> Dict[Union[int, str], Dict[str, Any]]:
183183
including optionally replacing missing values and including indexes.
184184
"""
185185

186-
df = self.fmt.frame
186+
df = self.frame
187187

188188
if self.index:
189189
df = df.reset_index()

0 commit comments

Comments
 (0)