Skip to content

BUG: fixed fillna('') on a Int64 column causes TypeError: <U3 cannot b… #44870

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,12 @@ def __init__(
if data is None:
data = {}
if dtype is not None:
if isinstance(dtype, str):
if dtype == "Int64":
dtype = "int64"
elif dtype == "Float64":
dtype = "float64"
Copy link
Member

Choose a reason for hiding this comment

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

this is going to change behavior in an unwanted way. The issue is with 'fillna' with an Int64 dtype, not the existence of Int64 dtypes

Copy link
Author

Choose a reason for hiding this comment

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

Agreed - as mentioned above in the description this contribution is for a final class project. Just trying to get a feel for contributing to open source software.

Copy link
Member

Choose a reason for hiding this comment

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

Are you planning to follow-up on this? That's an important part of the contribution process!


dtype = self._validate_dtype(dtype)

if isinstance(data, DataFrame):
Expand Down
84 changes: 84 additions & 0 deletions pandas/tests/frame/methods/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,87 @@ def test_fillna_nonconsolidated_frame():
df_nonconsol = df.pivot("i1", "i2")
result = df_nonconsol.fillna(0)
assert result.isna().sum().sum() == 0


def test_fillna_with_int_and_string_nonempty():
df = DataFrame(
{
"A": [1, 2, np.nan],
"B": [4, np.nan, 8],
},
dtype="Int64",
)
df.fillna("nan")

expected = df = DataFrame(
Copy link
Member

Choose a reason for hiding this comment

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

probably don't want to set both df and expected in this line?

Copy link
Member

Choose a reason for hiding this comment

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

same in each case below

Copy link
Author

Choose a reason for hiding this comment

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

Interesting, I don't remember setting both df and expected like that - might have been a product of running pre-commit or something else? I can try to change this asap.

Copy link
Member

Choose a reason for hiding this comment

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

can't imagine the pre-commit tooling doing this. most likely just a typo. happens to us all.

{
"A": [1, 2, " "],
"B": [4, " ", 8],
},
dtype="Int64",
)

tm.assert_frame_equal(df, expected)


def test_fillna_with_int_and_string_empty():
df = DataFrame(
{
"A": [1, 2, np.nan],
"B": [4, np.nan, 8],
},
dtype="Int64",
)
df.fillna(" ")

expected = df = DataFrame(
{
"A": [1, 2, " "],
"B": [4, " ", 8],
},
dtype="Int64",
)

tm.assert_frame_equal(df, expected)


def test_fillna_with_float_and_string_nonempty():
df = DataFrame(
{
"A": [1, 2, np.nan],
"B": [4, np.nan, 8],
},
dtype="Float64",
)
df.fillna("nan")

expected = df = DataFrame(
{
"A": [1, 2, " "],
"B": [4, " ", 8],
},
dtype="Float64",
)

tm.assert_frame_equal(df, expected)


def test_fillna_with_float_and_string_empty():
df = DataFrame(
{
"A": [1, 2, np.nan],
"B": [4, np.nan, 8],
},
dtype="Float64",
)
df.fillna(" ")

expected = df = DataFrame(
{
"A": [1, 2, " "],
"B": [4, " ", 8],
},
dtype="Float64",
)

tm.assert_frame_equal(df, expected)