Skip to content

NaN is converted to strings when reassigning a column with .loc #28403

Closed
@remidomingues

Description

@remidomingues

Hello team,

Here is a simple working exemple of value assignment on a dataframe:

df = pd.DataFrame({'A': [np.nan, np.nan, 'b']})
df['A'].loc[[0, 1]] = ['a', np.nan]
df.notna()
Out[126]: 
       A
0   True
1  False
2   True

However, if we use loc to assign the entire column with an array including NaNs, all values are converted to strings, breaking notna():

df = pd.DataFrame({'A': [np.nan, np.nan, 'b']})
df['A'].loc[[0, 1, 2]] = ['a', np.nan, np.nan]
df['A'].notna()                                                                                                                                                                                   
Out[133]: 
0    True
1    True
2    True
Name: A, dtype: bool

Metadata

Metadata

Assignees

Labels

Dtype ConversionsUnexpected or buggy dtype conversionsIndexingRelated to indexing on series/frames, not to indexes themselvesMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolateNeeds TestsUnit test(s) needed to prevent regressions

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions