CSV.jl open file error, contain data in scientific notation format

dataframes
csv

#1

When I open the CSV file containing integer file in the scientific notation format, such as 2e2 for the integer 200. When I use CSV.jl to open this file, I got the following error.

CSV.ParsingException(“error parsing a Int64 value on column 2, row 7190; encountered ‘e’”)

CSV file data table as f as
x, y
12, 567e3
18, 2356

The Julia code is

using CSV
dtype = Type[Union{Missing,Int64} for i=1:2];
df_ais = CSV.read( file_path; delim=",", datarow=2, header=["x", "y"], types=dtype )

#2

That’s because the type of 2e2 is a float not an integer:

julia> typeof(2e2)
Float64

#3

Thank you so much! It works.


#4

While the Julia type of 2e2 and 567e3 is Float64, they can be cleanly converted to Int64. In the interest of “being liberal in what you accept”, maybe CSV.jl should attempt to do a conversion? After all, the CSV file might come from an application that does not adhere to Julia semantics.


#5

Yea, I’ve come across situations where I wanted a round Float64 to magically become an Int, but this behavior would be outside the scope of CSV and downright unexpected: in my (albeit limited) experience, number notations with the exponential e (e.g. 2e2) are always treated as floats.
Part of the problem is that if we treat such numbers as Ints where we can, then their type won’t be guaranteed, it would depend on their value. But that issue doesn’t have to do with CSV, it’s a separate discussion.
BTW, I think there is a package that automatically converts floats to integers when it can, but I forgot what it’s called…