Skip to content

pandas.read_csv tries to convert "NaN" even when specifying dtype argument #19644

@jamesqo

Description

@jamesqo

Code Sample, a copy-pastable example if possible

from pandas.compat import StringIO import pandas as pd t1 = """float 1 """ t2 = """float NaN """ for t in t1, t2: df = pd.read_csv(StringIO(t), dtype={'float': 'str'}) print(type(df['float'][0]))

Problem description

Even when explicitly specifying dtype above, read_csv still converts values in the float column to a float when the string is "NaN". This behavior appears to be limited to "NaN" as it doesn't happen for regular numbers. Still, I unexpectedly ran across a "NaN" string in my application so it's blocking me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDtype ConversionsUnexpected or buggy dtype conversionsDuplicate ReportDuplicate issue or pull requestIO CSVread_csv, to_csvMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions