Skip to content

TST: ro_replace changing dtype of series #50664

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 9 commits into from
Jan 12, 2023
Merged

Conversation

pooja-subramaniam
Copy link
Contributor

@phofl phofl added the Sprints Sprint Pull Requests label Jan 10, 2023
@noatamir
Copy link
Member

noatamir commented Jan 11, 2023

👋 @pooja-subramaniam
The pre-commit errors will be fixed if you:

  1. Replace your single quotes with double quotes
  2. Add a new line at the end of the file (after your test)

If you want, you can have a look at the details link to see what the errors look like, so you can identify them next time 😉.

In the future, I recommend making a feature branch in your fork, rather than making a PR from your fork's main branch. You can find guidance for this in our documentation (here).

@pooja-subramaniam
Copy link
Contributor Author

thanks for the points @noatamir!

@pooja-subramaniam
Copy link
Contributor Author

@noatamir I don't understand the reason for the canceled check here. Do you know why that happened?

Copy link
Member

@phofl phofl left a comment

Choose a reason for hiding this comment

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

You can ignore cancelled pipelines if there is only one of them

This happens once in a while

def test_replace_change_dtype_series(self):
# GH#25797
df = pd.DataFrame.from_dict({"Test": ["0.5", True, "0.6"]})
print(df["Test"].dtype)
Copy link
Member

Choose a reason for hiding this comment

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

Some pointers: Please remove the print statements.

Generally, you can remove your first assert statements per block, but we want to compare the whole df with the second assert statement, e.g. define your expected object and then call assert_frame_equal

expected = ...
tm.assert_frame_equal(df, expected)

if expected is the same as your initial df, you can define expected through

expected = df.copy()

in the beginning

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Made the changes.

Correct me if I am wrong, but here the assert needs to check if the series dtype remains 'object' and not if the dataframes are equal. I've made changes accordingly.

Copy link
Member

Choose a reason for hiding this comment

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

tm.assert_frame_equal raises if your dtypes are different. So this is covered as well. In general, we want to check that the DataFrame is as expected (values, columns, index, dtype, ...). That is why we are using assert_frame_equal

The functions could modify the values unexpectedly and the test would not fail, if you only check the dtypes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah okay! Thanks for explaining :)

# GH#25797
df = pd.DataFrame.from_dict({"Test": ["0.5", True, "0.6"]})
df["Test"] = df["Test"].replace([True], [np.nan])
expected = pd.DataFrame.from_dict({"Test": ["0.5", np.nan, "0.6"]})
Copy link
Member

Choose a reason for hiding this comment

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

One last comment: Could you define expected only once after line 681 and reuse it for all assert_frame_equal statements? It's not necessary to define it multiple times when it's the same object :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right! How did I miss that one! Thanks again ;)

@phofl phofl changed the title test added for issue #25797 TST: ro_replace changing dtype of series Jan 12, 2023
@phofl phofl added this to the 2.0 milestone Jan 12, 2023
@phofl phofl added the replace replace method label Jan 12, 2023
@phofl phofl merged commit dc7f851 into pandas-dev:main Jan 12, 2023
@phofl
Copy link
Member

phofl commented Jan 12, 2023

thx @pooja-subramaniam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
replace replace method Sprints Sprint Pull Requests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Different behavior of to_replace method between pandas version 0.23.4 and 0.24.2 by changing dtype of series
3 participants