# Csv error reading numbers as string

**URL:** <https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341>\
**Category:** General Usage\
**Created:** [December 6, 2020, 12:16pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341 "2020-12-06T12:16:23Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [December 6, 2020, 12:16pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341/1 "2020-12-06T12:16:23Z")

</div>

Hello all,

I have always used csv.read for my csv files but now after the new julia update , I have troubles with csv.read  
it sometimes reads numbers as integers and sometime doesnot  
how do I avoid this problem ?

thanks

---

<div class="post-metadata">

**Author:** ![martincornejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martincornejo/32/13436_2.png) [@martincornejo](https://discourse.julialang.org/u/martincornejo)\
**Post date:** [December 6, 2020, 1:04pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341/2 "2020-12-06T13:04:05Z")

</div>

You can specify types for the columns

```julia
types = Dict(
    :columnA => String,
    :columnB => Float64
)

data = DataFrame(CSV.File("data.csv"; types))

```

More info in the documentation:

### Getting started

[https://csv.juliadata.org/stable/#Getting-Started](https://csv.juliadata.org/stable/#Getting-Started)

### Providing-Types

[https://csv.juliadata.org/stable/#Providing-Types](https://csv.juliadata.org/stable/#Providing-Types)

### Typemap

[https://csv.juliadata.org/stable/#Typemap](https://csv.juliadata.org/stable/#Typemap)

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [December 6, 2020, 2:17pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341/3 "2020-12-06T14:17:08Z")

</div>

![offf](https://global.discourse-cdn.com/julialang/original/3X/1/b/1b6a8322979138084933c9e8675e42d5fa3c8a21.jpeg)

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [December 6, 2020, 2:17pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341/4 "2020-12-06T14:17:28Z")

</div>

it worked one time the this the error I got

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 6, 2020, 2:20pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341/5 "2020-12-06T14:20:22Z")

</div>

Can you post an MWE of your error? The error message seems to suggest that you passed a keyword argument `type_buses` which doesn’t exist.

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [December 6, 2020, 2:25pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341/6 "2020-12-06T14:25:37Z")

</div>

```julia
type_buses= Dict(
    :bus => Int16,
    :type => String,
    :bh => Float64,
    :v => Float64,
    :delta => Float64,
    :pg => Float64,
    :qg => Float64,
    :pd => Float64,
    :qd => Float64,
    :pgmax => Float64,
    :pgmin => Float64,
    :qgmax => Float64,
    :qgmin => Float64,
)

```

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [December 6, 2020, 2:26pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341/7 "2020-12-06T14:26:20Z")

</div>

this is how I defined types\_buses from my code and I also add it in the command of CSV.file

```julia
BUSES_DATAFRAME = DataFrame(CSV.File("D:/M A S T E R S S S S S S/chapter_4/imp/14bus/B14.csv"; type_buses))

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 6, 2020, 2:38pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341/8 "2020-12-06T14:38:12Z")

</div>

`types` is a keyword argument, not a positional argument, so you need to name it. Here’s a full MWE:

```julia
julia> using CSV, DataFrames

julia> df = DataFrame(rand(2, 3), :auto);

julia> CSV.write("out.csv", df);

julia> type_buses = Dict(:x1 => Float32, :x2 => String, :x3 => Float64);

julia> CSV.read("out.csv", DataFrame; types = type_buses)
2×3 DataFrame
 Row │ x1 x2 x3       
     │ Float32 String Float64  
─────┼──────────────────────────────────────────
   1 │ 0.539608 0.050591523119873916 0.750995
   2 │ 0.396338 0.48258399391743123 0.463351

```

Note that I write `types = type_buses` to specify the kwarg. In the example above, things worked because the dictionary had the same name as the kwarg:

```julia
julia> types = type_buses;

julia> CSV.read("out.csv", DataFrame; types)
2×3 DataFrame
 Row │ x1 x2 x3       
     │ Float32 String Float64  
─────┼──────────────────────────────────────────
   1 │ 0.539608 0.050591523119873916 0.750995
   2 │ 0.396338 0.48258399391743123 0.463351

```

This fails if the name of the dict doesn’t match the kwarg name:

```julia
julia> CSV.read("out.csv", DataFrame; type_buses)
ERROR: MethodError: no method matching CSV.File(::CSV.Header{false, Parsers.Options{false, true, true, false, Missing, UInt8, Nothing}, Vector{UInt8}}; debug=false, typemap=Dict{Type, Type}(), type_buses=Dict{Symbol, DataType}(:x2 => String, :x3 => Float64, :x1 => Float32))
Closest candidates are:
  CSV.File(::CSV.Header; finalizebuffer, startingbyteposition, endingbyteposition, limit, threaded, typemap, tasks, lines_to_check, maxwarnings, debug) at /home/nils/.julia/packages/CSV/la2cd/src/file.jl:221 got unsupported keyword argument "type_buses"

```

Which is the error you’re seeing.

I think what you’re seeing here is a side effect of the new named tuple auto expansion introduced in 1.5 (?), so this might not even work on older versions.

In any case I’d say it’s always best to be explicit with kwargs and spell them out - after all, that’s what they are for.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 6, 2020, 2:41pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341/9 "2020-12-06T14:41:38Z")

</div>

And more to your original question: I haven’t seen any regressions in CSV.jl’s capability of detecting number types automatically in recent releases, and if there are any, that might well be a bug.

Can you share the file for which an older version of CSV.jl successfully detected the correct type, but the latest version fails to do so?

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [December 6, 2020, 2:55pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341/10 "2020-12-06T14:55:43Z")

</div>

I am now more confused 😟…excuse me can you make it simpler

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 6, 2020, 2:58pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341/11 "2020-12-06T14:58:11Z")

</div>

Sorry, that wasn’t the intention of course - the above is a self contained example, so you can execute it line for line and if there’s something you don’t understand feel free to check back in.

Alternatively, just change the function call you posted above:

```julia
DataFrame(CSV.File("D:/M A S T E R S S S S S S/chapter_4/imp/14bus/B14.csv"; type_buses))

```

to

```julia
DataFrame(CSV.File("D:/M A S T E R S S S S S S/chapter_4/imp/14bus/B14.csv"; types = type_buses))

```

and things should work.

I should also note that `CSV.read(file, DataFrame)` is the same as `DataFame(CSV.File(file))` just in case that added to your confusion!

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [December 6, 2020, 3:03pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341/12 "2020-12-06T15:03:11Z")

</div>

Yes it worked…Thanks a ton…I have one problem remaining…Line 15 which is the last line in the csv file is not read properly. I find all values corresponding to that line as missing in the DF

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 6, 2020, 3:06pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341/13 "2020-12-06T15:06:11Z")

</div>

This is very hard to debug without the file itself, although I would assume in this case that you are trying to force types with passing `types = type_buses`, and the last line can’t be read in the format you are specifying (which is probably the reason why `CSV.read` didn’t infer the types originally).

What happens if you just do:

```julia
CSV.read("D:/M A S T E R S S S S S S/chapter_4/imp/14bus/B14.csv", DataFrame)

```

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [December 6, 2020, 3:08pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341/14 "2020-12-06T15:08:47Z")

</div>

when I do this, I find that all numbers are read as stringssss ☹

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [December 6, 2020, 3:09pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341/15 "2020-12-06T15:09:44Z")

</div>

my file has a header and numbers …nothing else Can I share part of it here

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 6, 2020, 3:13pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341/17 "2020-12-06T15:13:21Z")

</div>

From this it seems there is no line 15? You have a header, and 14 rows of data, why are you expecting 15 rows in your DataFrame?

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [December 6, 2020, 3:16pm UTC](https://discourse.julialang.org/t/csv-error-reading-numbers-as-string/51341/18 "2020-12-06T15:16:26Z")

</div>

yes you are right…it seems like I became short sight after spending hours in front of the computer to solve this issue…Now its solved Thanks a million 🙂 🙂 😃
