Skip to content

HUH?: DataFrame[categorical].replace #33272

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

Closed
jbrockmendel opened this issue Apr 3, 2020 · 0 comments · Fixed by #33279
Closed

HUH?: DataFrame[categorical].replace #33272

jbrockmendel opened this issue Apr 3, 2020 · 0 comments · Fixed by #33279
Labels
Categorical Categorical Data Type replace replace method
Milestone

Comments

@jbrockmendel
Copy link
Member

When we have a DataFrame with 2 Categorical columns with matching dtypes and do a .replace on that DataFrame, the results do not match what we would get if we operate column-wise. Instead the results we get look like what we'd get if we stacked the columns, replaced, then unstacked.

i.e. AFAICT we are doing gymnastics to behave as if CategoricalBlock supported 2D Categorical.

From tests.frame.methods.test_replace:

replace_dict = {'a': 1, 'b': 2}
value = 3
df = pd.DataFrame([[1, 1], [2, 2]], columns=["a", "b"], dtype="category")

final_data = [[3, 1], [2, 3]]
expected = pd.DataFrame(final_data, columns=["a", "b"], dtype="category")
expected["a"] = expected["a"].cat.set_categories([1, 2, 3])
expected["b"] = expected["b"].cat.set_categories([1, 2, 3])

result = df.replace(replace_dict, value)
tm.assert_frame_equal(result, expected)

# this part is no longer from the test
col_wise = {col: df[col].replace(replace_dict[col], value) for col in df.columns}
result2 = pd.DataFrame(col_wise)

result2["a"].dtype
CategoricalDtype(categories=[3, 2], ordered=False)
@jbrockmendel jbrockmendel added the Categorical Categorical Data Type label Apr 5, 2020
@jreback jreback added this to the 1.1 milestone Apr 5, 2020
@jbrockmendel jbrockmendel added the replace replace method label Sep 21, 2020
@jbrockmendel jbrockmendel reopened this Sep 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type replace replace method
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants