Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: pd.read_xml does not support file like object when iterparse is used #50641

Closed
3 tasks done
bama-chi opened this issue Jan 9, 2023 · 5 comments · Fixed by #50759
Closed
3 tasks done

BUG: pd.read_xml does not support file like object when iterparse is used #50641

bama-chi opened this issue Jan 9, 2023 · 5 comments · Fixed by #50759
Labels
Bug IO XML read_xml, to_xml Needs Info Clarification about behavior needed to assess issue

Comments

@bama-chi
Copy link

bama-chi commented Jan 9, 2023

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

with open(file_, "r") as f:
    df = pd.read_xml(
        f,
        parser="etree",
        iterparse={root: list(elements)}
    )

Issue Description

the method read_xml with iterparse as parms is used to read large xml file, but it's restricted to read only files on local disk.
this design is not justified since that the module iterparse from xml.etree.Elements does allow this features (reading from a file like object)

if (
    not isinstance(self.path_or_buffer, str) # -> condition that raise the Error
    or is_url(self.path_or_buffer)
    or is_fsspec_url(self.path_or_buffer)
    or self.path_or_buffer.startswith(("<?xml", "<"))
    or infer_compression(self.path_or_buffer, "infer") is not None
):
    raise ParserError(
        "iterparse is designed for large XML files that are fully extracted on "
        "local disk and not as compressed files or online sources."
    )

maybe something like this is better:

if isinstance(self.path_or_buffer,str) or hasattr(self.path_or_buffer, "read"):
 #do something
else:
  raise ParserError(...)

Expected Behavior

this must return a dataframe reither than raise Exception

Installed Versions

INSTALLED VERSIONS

commit : 87cfe4e
python : 3.10.4.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-136-generic
Version : #153-Ubuntu SMP Thu Nov 24 15:56:58 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.0
numpy : 1.23.4
pytz : 2022.5
dateutil : 2.8.2
setuptools : 58.1.0
pip : 22.0.4
Cython : None
pytest : 7.1.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy :
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@bama-chi bama-chi added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 9, 2023
@phofl
Copy link
Member

phofl commented Jan 11, 2023

Hi, thanks for your report. Please provide a reproducible example that is copy-paste able

@phofl phofl added Needs Info Clarification about behavior needed to assess issue IO XML read_xml, to_xml and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 11, 2023
@ParfaitG
Copy link
Contributor

This is a straightforward fix and not a use case originally considered for very large files requiring iterparse. For lxml parser, the read mode must be rb as its iterparse method expects only binary input.

@bama-chi
Copy link
Author

@phofl dunno what can I provide more than the example above, you only have to replace the file_ variable with your xml file
let me know if I can help with something

@ParfaitG thank you for ur PR.

@phofl
Copy link
Member

phofl commented Jan 16, 2023

That's exactly what copy-and-pasteable means. For example a simple read_csv reproducer:

data = """a,b
1,2
"""
pd.read_csv(StringIO(data))

Same goes for xml for future bug reports

@bama-chi
Copy link
Author

okey thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO XML read_xml, to_xml Needs Info Clarification about behavior needed to assess issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants