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

FIX-#5650: Restore the right dtype for applying Series.cat #5651

Merged
merged 2 commits into from
Feb 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
23 changes: 22 additions & 1 deletion modin/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ def _saving_make_api_url(token, _make_api_url=modin.utils._make_api_url):

import modin # noqa: E402
import modin.config # noqa: E402
from modin.config import IsExperimental, TestRayClient # noqa: E402
from modin.config import ( # noqa: E402
NPartitions,
MinPartitionSize,
IsExperimental,
TestRayClient,
)
import uuid # noqa: E402

from modin.core.storage_formats import ( # noqa: E402
Expand Down Expand Up @@ -535,6 +540,22 @@ def get_generated_doc_urls():
return lambda: _generated_doc_urls


@pytest.fixture
def set_num_partitions(request):
old_num_partitions = NPartitions.get()
NPartitions.put(request.param)
yield
NPartitions.put(old_num_partitions)


@pytest.fixture
def set_min_partition_size(request):
old_min_partition_size = MinPartitionSize.get()
MinPartitionSize.put(request.param)
yield
MinPartitionSize.put(old_min_partition_size)


ray_client_server = None


Expand Down
8 changes: 7 additions & 1 deletion modin/core/storage_formats/pandas/query_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3385,7 +3385,13 @@ def sort_columns_by_row_values(self, rows, ascending=True, **kwargs):
# Cat operations
def cat_codes(self):
def func(df) -> np.ndarray:
ser = df.iloc[:, 0]
# `df` is supposed to be consisted of multiple partitions,
# which should be concatenated before applying a function.
# `pd.concat` doesn't preserve categorical dtype
# if the dfs have categorical columns
# so we intentionaly restore the right dtype.
# TODO: revert the change when https://github.com/pandas-dev/pandas/issues/51362 is fixed.
Copy link
Collaborator

Choose a reason for hiding this comment

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

we've already raised a similar issue in modin, we should probably link it here as well: #2513

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This PR doesn't fix #2513, but only series.cat.* methods. So I think we can close #2513 when pandas has a fix on their side unless we get a P0 request to fix #2513 by that time.

ser = df.astype("category", copy=False).iloc[:, 0]
YarShev marked this conversation as resolved.
Show resolved Hide resolved
return ser.cat.codes

res = self._modin_frame.apply_full_axis(axis=0, func=func)
Expand Down
15 changes: 15 additions & 0 deletions modin/pandas/test/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4333,6 +4333,21 @@ def test_cat_codes(data):
df_equals(modin_result, pandas_result)


@pytest.mark.parametrize(
"set_min_partition_size",
[1, 2],
ids=["four_partitions", "two_partitions"],
indirect=True,
)
def test_cat_codes_issue5650(set_min_partition_size):
data = {"name": ["abc", "def", "ghi", "jkl"]}
pandas_df = pandas.DataFrame(data)
pandas_df = pandas_df.astype("category")
modin_df = pd.DataFrame(data)
modin_df = modin_df.astype("category")
eval_general(modin_df, pandas_df, lambda df: df["name"].cat.codes)


@pytest.mark.parametrize(
"data", test_data_categorical_values, ids=test_data_categorical_keys
)
Expand Down
8 changes: 0 additions & 8 deletions modin/test/storage_formats/pandas/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,6 @@ def validate_partitions_cache(df):
assert df._partitions[i, j].width() == column_widths[j]


@pytest.fixture
def set_num_partitions(request):
old_num_partitions = NPartitions.get()
NPartitions.put(request.param)
yield
NPartitions.put(old_num_partitions)


def test_aligning_blocks():
# Test problem when modin frames have the same number of rows, but different
# blocks (partition.list_of_blocks). See #2322 for details
Expand Down