-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Add ORC reader #29447
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
Merged
Merged
ENH: Add ORC reader #29447
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
810cd3c
add orc reader
kkraus14 6240f94
black
kkraus14 21ada9f
flake8 and more black
kkraus14 39518da
update docs
jreback f4e4eb5
use min version of pyarrow
jreback 1fe30e9
update doc-links & add typing
jreback 5582a09
simplify
jreback 0d027e9
skip tests on windows
jreback a4284d1
actually skip on windows
jreback 25cf714
simplify imports
jreback e8efceb
clean impl
jreback bf4f013
Revert "clean impl"
jreback ad1bade
Revert "simplify imports"
jreback ca016ef
remove option for multiple backends & simplify tests
jreback b846bff
small doc update
jreback ebaec28
fix doc error & make simpler
jreback 39b578d
actually skip on windows
jreback 8a203a6
skip on dep missing
jreback 884d61e
typo
jorisvandenbossche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,13 @@ Parquet | |
|
||
read_parquet | ||
|
||
ORC | ||
~~~ | ||
.. autosummary:: | ||
:toctree: api/ | ||
|
||
read_orc | ||
|
||
SAS | ||
~~~ | ||
.. autosummary:: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,6 +168,7 @@ | |
# misc | ||
read_clipboard, | ||
read_parquet, | ||
read_orc, | ||
read_feather, | ||
read_gbq, | ||
read_html, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,57 @@ | ||||||
""" orc compat """ | ||||||
|
||||||
import distutils | ||||||
from typing import TYPE_CHECKING, List, Optional | ||||||
|
||||||
from pandas._typing import FilePathOrBuffer | ||||||
|
||||||
from pandas.io.common import get_filepath_or_buffer | ||||||
|
||||||
if TYPE_CHECKING: | ||||||
from pandas import DataFrame | ||||||
|
||||||
|
||||||
def read_orc( | ||||||
path: FilePathOrBuffer, columns: Optional[List[str]] = None, **kwargs, | ||||||
) -> "DataFrame": | ||||||
""" | ||||||
Load an ORC object from the file path, returning a DataFrame. | ||||||
|
||||||
.. versionadded:: 1.0.0 | ||||||
|
||||||
Parameters | ||||||
---------- | ||||||
path : str, path object or file-like object | ||||||
Any valid string path is acceptable. The string could be a URL. Valid | ||||||
URL schemes include http, ftp, s3, and file. For file URLs, a host is | ||||||
expected. A local file could be: | ||||||
``file://localhost/path/to/table.orc``. | ||||||
|
||||||
If you want to pass in a path object, pandas accepts any | ||||||
``os.PathLike``. | ||||||
|
||||||
By file-like object, we refer to objects with a ``read()`` method, | ||||||
such as a file handler (e.g. via builtin ``open`` function) | ||||||
or ``StringIO``. | ||||||
columns : list, default=None | ||||||
If not None, only these columns will be read from the file. | ||||||
**kwargs | ||||||
Any additional kwargs are passed to pyarrow. | ||||||
|
||||||
Returns | ||||||
------- | ||||||
DataFrame | ||||||
""" | ||||||
|
||||||
# we require a newer version of pyarrow thaN we support for parquet | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
import pyarrow | ||||||
|
||||||
if distutils.version.LooseVersion(pyarrow.__version__) < "0.13.0": | ||||||
raise ImportError("pyarrow must be >= 0.13.0 for read_orc") | ||||||
|
||||||
import pyarrow.orc | ||||||
|
||||||
path, _, _, _ = get_filepath_or_buffer(path) | ||||||
orc_file = pyarrow.orc.ORCFile(path) | ||||||
result = orc_file.read(columns=columns, **kwargs).to_pandas() | ||||||
return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,6 +167,7 @@ class TestPDApi(Base): | |
"read_table", | ||
"read_feather", | ||
"read_parquet", | ||
"read_orc", | ||
"read_spss", | ||
] | ||
|
||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
""" test orc compat """ | ||
import datetime | ||
import os | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
import pandas as pd | ||
from pandas import read_orc | ||
import pandas.util.testing as tm | ||
|
||
pytest.importorskip("pyarrow", minversion="0.13.0") | ||
pytest.importorskip("pyarrow.orc") | ||
|
||
pytestmark = pytest.mark.filterwarnings( | ||
"ignore:RangeIndex.* is deprecated:DeprecationWarning" | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def dirpath(datapath): | ||
return datapath("io", "data", "orc") | ||
|
||
|
||
def test_orc_reader_empty(dirpath): | ||
columns = [ | ||
"boolean1", | ||
"byte1", | ||
"short1", | ||
"int1", | ||
"long1", | ||
"float1", | ||
"double1", | ||
"bytes1", | ||
"string1", | ||
] | ||
dtypes = [ | ||
"bool", | ||
"int8", | ||
"int16", | ||
"int32", | ||
"int64", | ||
"float32", | ||
"float64", | ||
"object", | ||
"object", | ||
] | ||
expected = pd.DataFrame(index=pd.RangeIndex(0)) | ||
for colname, dtype in zip(columns, dtypes): | ||
expected[colname] = pd.Series(dtype=dtype) | ||
|
||
inputfile = os.path.join(dirpath, "TestOrcFile.emptyFile.orc") | ||
got = read_orc(inputfile, columns=columns) | ||
|
||
tm.assert_equal(expected, got) | ||
|
||
|
||
def test_orc_reader_basic(dirpath): | ||
data = { | ||
"boolean1": np.array([False, True], dtype="bool"), | ||
"byte1": np.array([1, 100], dtype="int8"), | ||
"short1": np.array([1024, 2048], dtype="int16"), | ||
"int1": np.array([65536, 65536], dtype="int32"), | ||
"long1": np.array([9223372036854775807, 9223372036854775807], dtype="int64"), | ||
"float1": np.array([1.0, 2.0], dtype="float32"), | ||
"double1": np.array([-15.0, -5.0], dtype="float64"), | ||
"bytes1": np.array([b"\x00\x01\x02\x03\x04", b""], dtype="object"), | ||
"string1": np.array(["hi", "bye"], dtype="object"), | ||
} | ||
expected = pd.DataFrame.from_dict(data) | ||
|
||
inputfile = os.path.join(dirpath, "TestOrcFile.test1.orc") | ||
got = read_orc(inputfile, columns=data.keys()) | ||
|
||
tm.assert_equal(expected, got) | ||
|
||
|
||
def test_orc_reader_decimal(dirpath): | ||
from decimal import Decimal | ||
|
||
# Only testing the first 10 rows of data | ||
data = { | ||
"_col0": np.array( | ||
[ | ||
Decimal("-1000.50000"), | ||
Decimal("-999.60000"), | ||
Decimal("-998.70000"), | ||
Decimal("-997.80000"), | ||
Decimal("-996.90000"), | ||
Decimal("-995.10000"), | ||
Decimal("-994.11000"), | ||
Decimal("-993.12000"), | ||
Decimal("-992.13000"), | ||
Decimal("-991.14000"), | ||
], | ||
dtype="object", | ||
) | ||
} | ||
expected = pd.DataFrame.from_dict(data) | ||
|
||
inputfile = os.path.join(dirpath, "TestOrcFile.decimal.orc") | ||
got = read_orc(inputfile).iloc[:10] | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
tm.assert_equal(expected, got) | ||
|
||
|
||
def test_orc_reader_date_low(dirpath): | ||
data = { | ||
"time": np.array( | ||
[ | ||
"1900-05-05 12:34:56.100000", | ||
"1900-05-05 12:34:56.100100", | ||
"1900-05-05 12:34:56.100200", | ||
"1900-05-05 12:34:56.100300", | ||
"1900-05-05 12:34:56.100400", | ||
"1900-05-05 12:34:56.100500", | ||
"1900-05-05 12:34:56.100600", | ||
"1900-05-05 12:34:56.100700", | ||
"1900-05-05 12:34:56.100800", | ||
"1900-05-05 12:34:56.100900", | ||
], | ||
dtype="datetime64[ns]", | ||
), | ||
"date": np.array( | ||
[ | ||
datetime.date(1900, 12, 25), | ||
datetime.date(1900, 12, 25), | ||
datetime.date(1900, 12, 25), | ||
datetime.date(1900, 12, 25), | ||
datetime.date(1900, 12, 25), | ||
datetime.date(1900, 12, 25), | ||
datetime.date(1900, 12, 25), | ||
datetime.date(1900, 12, 25), | ||
datetime.date(1900, 12, 25), | ||
datetime.date(1900, 12, 25), | ||
], | ||
dtype="object", | ||
), | ||
} | ||
expected = pd.DataFrame.from_dict(data) | ||
|
||
inputfile = os.path.join(dirpath, "TestOrcFile.testDate1900.orc") | ||
got = read_orc(inputfile).iloc[:10] | ||
|
||
tm.assert_equal(expected, got) | ||
|
||
|
||
def test_orc_reader_date_high(dirpath): | ||
data = { | ||
"time": np.array( | ||
[ | ||
"2038-05-05 12:34:56.100000", | ||
"2038-05-05 12:34:56.100100", | ||
"2038-05-05 12:34:56.100200", | ||
"2038-05-05 12:34:56.100300", | ||
"2038-05-05 12:34:56.100400", | ||
"2038-05-05 12:34:56.100500", | ||
"2038-05-05 12:34:56.100600", | ||
"2038-05-05 12:34:56.100700", | ||
"2038-05-05 12:34:56.100800", | ||
"2038-05-05 12:34:56.100900", | ||
], | ||
dtype="datetime64[ns]", | ||
), | ||
"date": np.array( | ||
[ | ||
datetime.date(2038, 12, 25), | ||
datetime.date(2038, 12, 25), | ||
datetime.date(2038, 12, 25), | ||
datetime.date(2038, 12, 25), | ||
datetime.date(2038, 12, 25), | ||
datetime.date(2038, 12, 25), | ||
datetime.date(2038, 12, 25), | ||
datetime.date(2038, 12, 25), | ||
datetime.date(2038, 12, 25), | ||
datetime.date(2038, 12, 25), | ||
], | ||
dtype="object", | ||
), | ||
} | ||
expected = pd.DataFrame.from_dict(data) | ||
|
||
inputfile = os.path.join(dirpath, "TestOrcFile.testDate2038.orc") | ||
got = read_orc(inputfile).iloc[:10] | ||
|
||
tm.assert_equal(expected, got) | ||
|
||
|
||
def test_orc_reader_snappy_compressed(dirpath): | ||
data = { | ||
"int1": np.array( | ||
[ | ||
-1160101563, | ||
1181413113, | ||
2065821249, | ||
-267157795, | ||
172111193, | ||
1752363137, | ||
1406072123, | ||
1911809390, | ||
-1308542224, | ||
-467100286, | ||
], | ||
dtype="int32", | ||
), | ||
"string1": np.array( | ||
[ | ||
"f50dcb8", | ||
"382fdaaa", | ||
"90758c6", | ||
"9e8caf3f", | ||
"ee97332b", | ||
"d634da1", | ||
"2bea4396", | ||
"d67d89e8", | ||
"ad71007e", | ||
"e8c82066", | ||
], | ||
dtype="object", | ||
), | ||
} | ||
expected = pd.DataFrame.from_dict(data) | ||
|
||
inputfile = os.path.join(dirpath, "TestOrcFile.testSnappy.orc") | ||
got = read_orc(inputfile).iloc[:10] | ||
|
||
tm.assert_equal(expected, got) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.