Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
c1823ef
Add reproduction test for .tar.gz archives
Skn0tt Dec 6, 2021
9a85cba
add support for .tar archives
Skn0tt Dec 6, 2021
e673061
update doc comments
Skn0tt Dec 6, 2021
a0d6386
fix: pep8 errors
Skn0tt Dec 6, 2021
6a8edef
refactor: flip _compression_to_extension around to support multiple e…
Skn0tt Dec 7, 2021
d4e40c9
refactor: detect tar files using existing extension mapping
Skn0tt Dec 7, 2021
5f22df7
feat: add support for writing tar files
Skn0tt Dec 7, 2021
c6573ef
feat: assure it respects .gz endings
Skn0tt Dec 15, 2021
f3b6ed5
Merge branch 'master' into read-tar-archives
Skn0tt Dec 15, 2021
a4ac382
feat: add "tar" entry to compressionoptions
Skn0tt Dec 15, 2021
e66826b
chore: add whatsnew entry
Skn0tt Dec 15, 2021
941be37
fix: test_compression_size_fh
Skn0tt Dec 15, 2021
e3369aa
Merge branch 'master' into read-tar-archives
Skn0tt Jan 4, 2022
0468e5f
add tarfile to shared compression docs
Skn0tt Jan 4, 2022
2531ee0
fix formatting
Skn0tt Jan 4, 2022
57eba0a
pass through "mode" via compression args
Skn0tt Jan 4, 2022
38f7d54
fix pickle test
Skn0tt Jan 4, 2022
887fd10
add class comment
Skn0tt Jan 4, 2022
fc2e7f0
Merge remote-tracking branch 'origin/main' into read-tar-archives
Skn0tt Apr 9, 2022
669d942
sort imports
Skn0tt Apr 9, 2022
7d7d3c6
add _compression_to_extension back for backwards compatibility
Skn0tt Apr 9, 2022
8b8b8ac
fix some type warnings
Skn0tt Apr 9, 2022
dd356f6
fix: formatting
Skn0tt Apr 9, 2022
514014a
fix: mypy complaints
Skn0tt Apr 9, 2022
38971c7
fix: more tests
Skn0tt Apr 9, 2022
e35d361
fix: some error with xml
Skn0tt Apr 9, 2022
c5088fc
fix: interpreted text role
Skn0tt Apr 9, 2022
f6c5173
move to v1.5 whatsnw
Skn0tt Apr 9, 2022
9a4fa07
add versionadded note
Skn0tt Apr 11, 2022
0c31aa8
don't leave blank lines
Skn0tt Apr 11, 2022
086c598
add tests for zero files / multiple files
Skn0tt Apr 13, 2022
861faf0
move _compression_to_extension to tests
Skn0tt Apr 13, 2022
9458ecb
revert added "mode" argument
Skn0tt Apr 13, 2022
d20f315
add test to ensure that `compression.mode` works
Skn0tt Apr 13, 2022
1066f1b
Merge branch 'main' into read-tar-archives
Skn0tt Apr 25, 2022
6b0e1e6
Merge branch 'main' into read-tar-archives
Skn0tt May 5, 2022
0d9ed18
compare strings, not bytes
Skn0tt May 5, 2022
37370c2
replace carriage returns
Skn0tt May 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3020,11 +3020,13 @@ def to_xml(
layout of elements and attributes from original output. This
argument requires ``lxml`` to be installed. Only XSLT 1.0
scripts and not later versions is currently supported.
compression : {{'infer', 'gzip', 'bz2', 'zip', 'xz', None}}, default 'infer'
compression : {{'infer', 'gzip', 'bz2',
'zip', 'tar', 'xz', None}}, default 'infer'
For on-the-fly decompression of on-disk data. If 'infer', then use
gzip, bz2, zip or xz if path_or_buffer is a string ending in
'.gz', '.bz2', '.zip', or 'xz', respectively, and no decompression
otherwise. If using 'zip', the ZIP file must contain only one data
gzip, bz2, zip, xz or tar if path_or_buffer is a string ending in
'.gz', '.bz2', '.zip', '.xz' or containing '.tar' respectively,
and no decompression otherwise.
If using 'zip' or 'tar', the archive must contain only one data
file to be read in. Set to None for no decompression.
{storage_options}

Expand Down
25 changes: 22 additions & 3 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import mmap
import os
from pathlib import Path
import tarfile
import tempfile
from typing import (
IO,
Expand Down Expand Up @@ -262,7 +263,7 @@ def _get_filepath_or_buffer(
----------
filepath_or_buffer : a url, filepath (str, py.path.local or pathlib.Path),
or buffer
compression : {{'gzip', 'bz2', 'zip', 'xz', None}}, optional
compression : {{'gzip', 'bz2', 'zip', 'xz', 'tar', None}}, optional
encoding : the encoding to use to decode bytes, default is 'utf-8'
mode : str, optional

Expand Down Expand Up @@ -496,9 +497,9 @@ def infer_compression(
----------
filepath_or_buffer : str or file handle
File path or object.
compression : {'infer', 'gzip', 'bz2', 'zip', 'xz', None}
compression : {'infer', 'gzip', 'bz2', 'zip', 'xz', 'tar', None}
If 'infer' and `filepath_or_buffer` is path-like, then detect
compression from the following extensions: '.gz', '.bz2', '.zip',
compression from the following extensions: '.gz', '.bz2', '.zip', '.tar',
or '.xz' (otherwise no compression).

Returns
Expand All @@ -520,6 +521,9 @@ def infer_compression(
# Cannot infer compression of a buffer, assume no compression
return None

if ".tar" in filepath_or_buffer:
return "tar"

# Infer compression from the filename/URL extension
for compression, extension in _compression_to_extension.items():
if filepath_or_buffer.lower().endswith(extension):
Expand Down Expand Up @@ -747,6 +751,21 @@ def get_handle(
f"Only one file per ZIP: {zip_names}"
)

# TAR Encoding
elif compression == "tar":
tar = tarfile.open(handle, "r:*")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If pandas supports reading from .tar*, users will probably also expect being able to write to .tar*.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in 5f22df7

handles.append(tar)
files = tar.getnames()
if len(files) == 1:
handle = tar.extractfile(files[0])
elif len(files) == 0:
raise ValueError(f"Zero files found in TAR archive {path_or_buf}")
else:
raise ValueError(
"Multiple files found in TAR archive. "
f"Only one file per TAR archive: {files}"
)

# XZ Compression
elif compression == "xz":
handle = get_lzma_file()(handle, ioargs.mode)
Expand Down
9 changes: 5 additions & 4 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,12 @@ def read_json(

``JsonReader`` is a context manager.

compression : {{'infer', 'gzip', 'bz2', 'zip', 'xz', None}}, default 'infer'
compression : {{'infer', 'gzip', 'bz2', 'zip', 'xz', 'tar', None}}, default 'infer'
For on-the-fly decompression of on-disk data. If 'infer', then use
gzip, bz2, zip or xz if path_or_buf is a string ending in
'.gz', '.bz2', '.zip', or 'xz', respectively, and no decompression
otherwise. If using 'zip', the ZIP file must contain only one data
gzip, bz2, zip, xz or tar if path_or_buf is a string ending in
'.gz', '.bz2', '.zip', '.xz' or containing '.tar' respectively,
and no decompression otherwise.
If using 'zip' or 'tar', the archive must contain only one data
file to be read in. Set to None for no decompression.

nrows : int, optional
Expand Down
6 changes: 3 additions & 3 deletions pandas/io/parsers/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@
.. versionchanged:: 1.2

``TextFileReader`` is a context manager.
compression : {{'infer', 'gzip', 'bz2', 'zip', 'xz', None}}, default 'infer'
compression : {{'infer', 'gzip', 'bz2', 'zip', 'xz', 'tar', None}}, default 'infer'
For on-the-fly decompression of on-disk data. If 'infer' and
`filepath_or_buffer` is path-like, then detect compression from the
following extensions: '.gz', '.bz2', '.zip', or '.xz' (otherwise no
decompression). If using 'zip', the ZIP file must contain only one data
following extensions: '.gz', '.bz2', '.zip', '.tar', '.xz' (otherwise no
decompression). If using 'zip' or 'tar', the archive must contain only one data
file to be read in. Set to None for no decompression.
thousands : str, optional
Thousands separator.
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ def read_pickle(
.. versionchanged:: 1.0.0
Accept URL. URL is not limited to S3 and GCS.

compression : {{'infer', 'gzip', 'bz2', 'zip', 'xz', None}}, default 'infer'
compression : {{'infer', 'gzip', 'bz2', 'zip', 'xz', 'tar', None}}, default 'infer'
If 'infer' and 'path_or_url' is path-like, then detect compression from
the following extensions: '.gz', '.bz2', '.zip', or '.xz' (otherwise no
the following extensions: '.gz', '.bz2', '.zip', '.tar', or '.xz' (otherwise no
compression) If 'infer' and 'path_or_url' is not path-like, then use
None (= no decompression).

Expand Down
13 changes: 7 additions & 6 deletions pandas/io/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ class _XMLFrameParser:
URL, file, file-like object, or a raw string containing XSLT,
`etree` does not support XSLT but retained for consistency.

compression : {'infer', 'gzip', 'bz2', 'zip', 'xz', None}, default 'infer'
compression : {'infer', 'gzip', 'bz2', 'zip', 'tar', 'xz', None}, default 'infer'
Compression type for on-the-fly decompression of on-disk data.
If 'infer', then use extension for gzip, bz2, zip or xz.
If 'infer', then use extension for gzip, bz2, zip, tar or xz.

storage_options : dict, optional
Extra options that make sense for a particular storage connection,
Expand Down Expand Up @@ -801,11 +801,12 @@ def read_xml(
transformation and not the original XML document. Only XSLT 1.0
scripts and not later versions is currently supported.

compression : {{'infer', 'gzip', 'bz2', 'zip', 'xz', None}}, default 'infer'
compression : {{'infer', 'gzip', 'bz2', 'zip', 'xz', 'tar', None}}, default 'infer'
For on-the-fly decompression of on-disk data. If 'infer', then use
gzip, bz2, zip or xz if path_or_buffer is a string ending in
'.gz', '.bz2', '.zip', or 'xz', respectively, and no decompression
otherwise. If using 'zip', the ZIP file must contain only one data
gzip, bz2, zip, xz, or tar if path_or_buffer is a string ending in
'.gz', '.bz2', '.zip', '.xz', or containing '.tar' respectively,
and no decompression otherwise.
If using 'zip' or 'tar', the archive must contain only one data
file to be read in. Set to None for no decompression.

{storage_options}
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/io/parser/test_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ def test_invalid_compression(all_parsers, invalid_compression):
parser.read_csv("test_file.zip", **compress_kwargs)


@skip_pyarrow
def test_compression_tar_archive(all_parsers, csv_dir_path):
parser = all_parsers
path = os.path.join(csv_dir_path, "tar_csv.tar.gz")
df = parser.read_csv(path)
assert list(df.columns) == ["a"]


def test_ignore_compression_extension(all_parsers):
parser = all_parsers
df = DataFrame({"a": [0, 1]})
Expand Down