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

GH-41502: [Python] Fix reading column index with decimal values #41503

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions python/pyarrow/pandas_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# module bug (ARROW-11983)
import concurrent.futures.thread # noqa
from copy import deepcopy
import decimal
from itertools import zip_longest
import json
import operator
Expand Down Expand Up @@ -1027,6 +1028,7 @@ def _is_generated_index_name(name):
'string': np.str_,
'integer': np.int64,
'floating': np.float64,
'decimal': np.object_,
'empty': np.object_,
}

Expand Down Expand Up @@ -1105,6 +1107,9 @@ def _reconstruct_columns_from_metadata(columns, column_indexes):
tz = pa.lib.string_to_tzinfo(
column_indexes[0]['metadata']['timezone'])
level = pd.to_datetime(level, utc=True).tz_convert(tz)
# GH-41503: if the column index was decimal, restore to decimal
elif pandas_dtype == "decimal":
level = _pandas_api.pd.Index([decimal.Decimal(i) for i in level])
elif level.dtype != dtype:
level = level.astype(dtype)
# ARROW-9096: if original DataFrame was upcast we keep that
Expand Down
11 changes: 11 additions & 0 deletions python/pyarrow/tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ def test_column_index_names_with_tz(self):
)
_check_pandas_roundtrip(df, preserve_index=True)

def test_column_index_names_with_decimal(self):
Copy link
Member

Choose a reason for hiding this comment

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

Could you add info about the GitHub issue this test is added for? Similarly as what you added in reconstruct_columns_from_metadata or in the test just above.

Copy link
Author

Choose a reason for hiding this comment

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

@AlenkaF sure, done.

# GH-41503: Test valid roundtrip with decimal value in column index
df = pd.DataFrame(
[[decimal.Decimal(5), decimal.Decimal(6)]],
columns=pd.MultiIndex.from_product(
[[decimal.Decimal(1)], [decimal.Decimal(2), decimal.Decimal(3)]]
),
index=[decimal.Decimal(4)],
)
_check_pandas_roundtrip(df, preserve_index=True)

def test_range_index_shortcut(self):
# ARROW-1639
index_name = 'foo'
Expand Down