Skip to content

Commit cfa9af8

Browse files
committed
BUG fixed fillna('') on a Int64 column causes TypeError: <U3 cannot be converted to an IntegerDtype(pandas-dev#44289)
1 parent a3702e2 commit cfa9af8

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

pandas/core/frame.py

+6
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,12 @@ def __init__(
598598
if data is None:
599599
data = {}
600600
if dtype is not None:
601+
if isinstance(dtype, str):
602+
if dtype == "Int64":
603+
dtype = "int64"
604+
elif dtype == "Float64":
605+
dtype = "float64"
606+
601607
dtype = self._validate_dtype(dtype)
602608

603609
if isinstance(data, DataFrame):

pandas/tests/frame/methods/test_fillna.py

+84
Original file line numberDiff line numberDiff line change
@@ -647,3 +647,87 @@ def test_fillna_nonconsolidated_frame():
647647
df_nonconsol = df.pivot("i1", "i2")
648648
result = df_nonconsol.fillna(0)
649649
assert result.isna().sum().sum() == 0
650+
651+
652+
def test_fillna_with_int_and_string_nonempty():
653+
df = DataFrame(
654+
{
655+
"A": [1, 2, np.nan],
656+
"B": [4, np.nan, 8],
657+
},
658+
dtype="Int64",
659+
)
660+
df.fillna("nan")
661+
662+
expected = df = DataFrame(
663+
{
664+
"A": [1, 2, " "],
665+
"B": [4, " ", 8],
666+
},
667+
dtype="Int64",
668+
)
669+
670+
tm.assert_frame_equal(df, expected)
671+
672+
673+
def test_fillna_with_int_and_string_empty():
674+
df = DataFrame(
675+
{
676+
"A": [1, 2, np.nan],
677+
"B": [4, np.nan, 8],
678+
},
679+
dtype="Int64",
680+
)
681+
df.fillna(" ")
682+
683+
expected = df = DataFrame(
684+
{
685+
"A": [1, 2, " "],
686+
"B": [4, " ", 8],
687+
},
688+
dtype="Int64",
689+
)
690+
691+
tm.assert_frame_equal(df, expected)
692+
693+
694+
def test_fillna_with_float_and_string_nonempty():
695+
df = DataFrame(
696+
{
697+
"A": [1, 2, np.nan],
698+
"B": [4, np.nan, 8],
699+
},
700+
dtype="Float64",
701+
)
702+
df.fillna("nan")
703+
704+
expected = df = DataFrame(
705+
{
706+
"A": [1, 2, " "],
707+
"B": [4, " ", 8],
708+
},
709+
dtype="Float64",
710+
)
711+
712+
tm.assert_frame_equal(df, expected)
713+
714+
715+
def test_fillna_with_float_and_string_empty():
716+
df = DataFrame(
717+
{
718+
"A": [1, 2, np.nan],
719+
"B": [4, np.nan, 8],
720+
},
721+
dtype="Float64",
722+
)
723+
df.fillna(" ")
724+
725+
expected = df = DataFrame(
726+
{
727+
"A": [1, 2, " "],
728+
"B": [4, " ", 8],
729+
},
730+
dtype="Float64",
731+
)
732+
733+
tm.assert_frame_equal(df, expected)

0 commit comments

Comments
 (0)