79156718

Date: 2024-11-04 19:21:49
Score: 1
Natty:
Report link

I had luck by reading the entire file in as a string, then manually specifying datatypes later. In my situation, I had a column which had IDs that could contain strings like "08" which would be different from an ID of "8".

The first thing I tried was df = pd.read_csv(dtype={"ID": str}) but for some reason, this was still converting "08" to "8" (at least it was still a string, but it must have been interpreted as an integer first, which removed the leading 0).

The thing that worked for me was this: df = pd.read_csv(dtype=str) And then I could go through and manually assign other columns their datatypes as needed like @lbolla mentioned.

For some reason, applying the data type across the entire document skipped the type inference step I suppose. Annoying this isn't the default behavior when specifying a specific column data type :(

Reasons:
  • Blacklisted phrase (1): :(
  • Whitelisted phrase (-1): worked for me
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @lbolla
  • Low reputation (1):
Posted by: Jimmy LeBaron