Skip to content

ERR: Raise ValueError when non-default index is given for orc format #51828

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 4 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ MultiIndex

I/O
^^^
-
- :meth:`DataFrame.to_orc` now raising ``ValueError`` when non-default :class:`Index` is given (:issue:`0`)
-

Period
Expand Down
16 changes: 16 additions & 0 deletions pandas/io/orc.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
is_unsigned_integer_dtype,
)

from pandas import RangeIndex
from pandas.core.arrays import ArrowExtensionArray
from pandas.core.frame import DataFrame

Expand Down Expand Up @@ -209,6 +210,21 @@ def to_orc(
if engine_kwargs is None:
engine_kwargs = {}

# validate index
# --------------

# validate that we have only a default index
# raise on anything else as we don't serialize the index

if not df.index.equals(RangeIndex.from_range(range(len(df)))):
Copy link
Member

Choose a reason for hiding this comment

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

Might be clearer to use pandas.core.indexes.api.default_index

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated

raise ValueError(
"orc does not support serializing a non-default index for the index; "
"you can .reset_index() to make the index into column(s)"
)

if df.index.name is not None:
raise ValueError("orc does not serialize index meta-data on a default index")

# If unsupported dtypes are found raise NotImplementedError
# In Pyarrow 8.0.0 this check will no longer be needed
if pa_version_under8p0:
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/io/test_orc.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,21 @@ def test_orc_uri_path():
uri = pathlib.Path(path).as_uri()
result = read_orc(uri)
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"index",
[
pd.RangeIndex(start=2, stop=5, step=1),
pd.RangeIndex(start=0, stop=3, step=1, name="non-default"),
pd.Index([1, 2, 3]),
],
)
def test_to_orc_non_default_index(index):
df = pd.DataFrame({"a": [1, 2, 3]}, index=index)
msg = (
"orc does not support serializing a non-default index|"
"orc does not serialize index meta-data"
)
with pytest.raises(ValueError, match=msg):
df.to_orc()