10000 BUG/PERF: groupby.transform with unobserved categories by undermyumbrella1 · Pull Request #58084 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

BUG/PERF: groupby.transform with unobserved categories #58084

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
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
Prev Previous commit
Next Next commit
Update according to pr comments
  • Loading branch information
Kei committed May 2, 2024
commit f3a3f639051de04bd3ab3df9d09253923cde923d
1 change: 0 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2044,7 +2044,6 @@ def __setstate__(self, state) -> None:
object.__setattr__(self, "_attrs", attrs)
flags = state.get("_flags", {"allows_duplicate_labels": True})
object.__setattr__(self, "_flags", Flags(self, **flags))

Copy link
Member

Choose a reason for hiding this comment

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

Can you revert this line removal. Shouldn't have any diff in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

resolved

# set in the order of internal names
# to avoid definitional recursion
# e.g. say fill_value needing _mgr to be
Expand Down
119 changes: 8 additions & 111 deletions pandas/tests/groupby/transform/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -1537,128 +1537,25 @@ def test_transform_sum_one_column_with_matching_labels_and_missing_labels():
tm.assert_frame_equal(result, expected)


# GH#58084
def test_min_one_unobserved_category_no_type_coercion():
@pytest.mark.parametrize("dtype", ["int32", "float32"])
def test_min_one_unobserved_category_no_type_coercion(dtype):
# GH#58084
df = DataFrame({"A": Categorical([1, 1, 2], categories=[1, 2, 3]), "B": [3, 4, 5]})
df["B"] = df["B"].astype("int32")
df["B"] = df["B"].astype(dtype)
gb = df.groupby("A", observed=False)
result = gb.transform("min")

expected = DataFrame({"B": [3, 3, 5]}, dtype="int32")
expected = DataFrame({"B": [3, 3, 5]}, dtype=dtype)
tm.assert_frame_equal(expected, result)


# GH#58084
def test_min_multiple_unobserved_categories_no_type_coercion():
df = DataFrame(
{
"X": Categorical(
["432945", "randomcat", -4325466, "randomcat", -4325466, -4325466],
categories=[
1,
"randomcat",
100,
333,
"cat43543",
-4325466,
54665,
-546767,
"432945",
767076,
],
),
"Y": [0, 940645, np.iinfo(np.int64).min, 9449, 100044444, 40],
}
)
df["Y"] = df["Y"].astype("int64")

gb = df.groupby("X", observed=False)
result = gb.transform("min")

expected = DataFrame(
{
"Y": [
0,
9449,
np.iinfo(np.int64).min,
9449,
np.iinfo(np.int64).min,
np.iinfo(np.int64).min,
]
},
dtype="int64",
)
tm.assert_frame_equal(expected, result)


# GH#58084
def test_min_float32_multiple_unobserved_categories_no_type_coercion():
df = DataFrame(
{
"X": Categorical(
["cat43543", -4325466, 54665, "cat43543", -4325466, 54665],
categories=[
1,
"randomcat",
100,
333,
"cat43543",
-4325466,
54665,
-546767,
"432945",
767076,
],
),
"Y": [
0.3940429,
940645.49,
np.finfo(np.float32).min,
9449.03333,
100044444.403294,
40.3020909,
],
}
)
df["Y"] = df["Y"].astype("float32")

gb = df.groupby("X", observed=False)
result = gb.transform("min")

expected = DataFrame(
{
"Y": [
0.3940429,
940645.49,
np.finfo(np.float32).min,
0.3940429,
940645.49,
np.finfo(np.float32).min,
]
},
dtype="float32",
)
tm.assert_frame_equal(expected, result)


# GH#58084
def test_min_all_empty_data_no_type_coercion():
# GH#58084
df = DataFrame(
{
"X": Categorical(
[],
categories=[
1,
"randomcat",
100,
333,
"cat43543",
-4325466,
54665,
-546767,
"432945",
767076,
],
categories=[1, "randomcat", 100],
),
"Y": [],
}
Expand All @@ -1672,8 +1569,8 @@ def test_min_all_empty_data_no_type_coercion():
tm.assert_frame_equal(expected, result)


# GH#58084
def test_min_one_dim_no_type_coercion():
# GH#58084
df = DataFrame({"Y": [9435, -5465765, 5055, 0, 954960]})
df["Y"] = df["Y"].astype("int32")
categories = Categorical([1, 2, 2, 5, 1], categories=[1, 2, 3, 4, 5])
Expand Down
0