# ERROR: MethodError: Cannot \`convert\` an object of type Missing to an object of type String

**URL:** <https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183>\
**Category:** General Usage\
**Created:** [December 5, 2023, 7:27pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183 "2023-12-05T19:27:33Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![steveo314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steveo314/32/205248_2.png) [@steveo314](https://discourse.julialang.org/u/steveo314)\
**Post date:** [December 5, 2023, 7:27pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/1 "2023-12-05T19:27:33Z")

</div>

I get this when trying to run

```julia
CSV.read("chuckle/random.csv", DataFrame)

```

But when running the same thing on another CSV in the same directory, it works fine.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 5, 2023, 8:22pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/2 "2023-12-05T20:22:29Z")

</div>

Can you provide the full stack trace? Also could you report the output of

```julia
using Pkg
Pkg.status()

```

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [December 5, 2023, 8:34pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/3 "2023-12-05T20:34:01Z")

</div>

Can you try increasing the value of `rows_to_check`?

So something like this?

```julia
CSV.read("chuckle/random.csv", DataFrame; rows_to_check = 100)

```

Try increasing it to 100 or 500 or 1000?

---

<div class="post-metadata">

**Author:** ![steveo314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steveo314/32/205248_2.png) [@steveo314](https://discourse.julialang.org/u/steveo314)\
**Post date:** [December 5, 2023, 9:35pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/4 "2023-12-05T21:35:35Z")

</div>

Stacktrace:

```julia
ERROR: MethodError: Cannot `convert` an object of type Missing to an object of type String

Closest candidates are:
  convert(::Type{S}, ::CategoricalArrays.CategoricalValue) where S<:Union{AbstractChar, AbstractString, Number}
   @ CategoricalArrays C:\Users\steve\.julia\packages\CategoricalArrays\0yLZN\src\value.jl:92
  convert(::Type{String}, ::WeakRefStrings.WeakRefString)
   @ WeakRefStrings C:\Users\steve\.julia\packages\WeakRefStrings\31nkb\src\WeakRefStrings.jl:81
  convert(::Type{String}, ::FilePathsBase.AbstractPath)
   @ FilePathsBase C:\Users\steve\.julia\packages\FilePathsBase\4RrDh\src\path.jl:117
  ...

Stacktrace:
 [1] get!(default::Function, h::Dict{String, UInt32}, key0::Missing)
   @ Base .\dict.jl:455
 [2] checkpooled!(#unused#::Type{String}, pertaskcolumns::Nothing, col::CSV.Column, j::Int64, ntasks::Int64, nrows::Int64, ctx::CSV.Context)
   @ CSV C:\Users\steve\.julia\packages\CSV\OnldF\src\file.jl:514
 [3] CSV.File(ctx::CSV.Context, chunking::Bool)
   @ CSV C:\Users\steve\.julia\packages\CSV\OnldF\src\file.jl:302
 [4] File
   @ C:\Users\steve\.julia\packages\CSV\OnldF\src\file.jl:227 [inlined]
 [5] #File#32
   @ C:\Users\steve\.julia\packages\CSV\OnldF\src\file.jl:223 [inlined]
 [6] CSV.File(source::String)
   @ CSV C:\Users\steve\.julia\packages\CSV\OnldF\src\file.jl:162
 [7] read(source::String, sink::Type; copycols::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ CSV C:\Users\steve\.julia\packages\CSV\OnldF\src\CSV.jl:117
 [8] read(source::String, sink::Type)
   @ CSV C:\Users\steve\.julia\packages\CSV\OnldF\src\CSV.jl:113
 [9] top-level scope
   @ c:\julia\process.jl:61

```

Pkg.status

```julia
  [336ed68f] CSV v0.10.11
  [49dc2e85] Calculus v0.5.1       
  [a93c6f00] DataFrames v1.6.1     
  [1313f7d8] DataFramesMeta v0.14.1
  [89b67f3b] ExcelFiles v1.0.0     
⌅ [c04bee98] ExcelReaders v0.11.0  
  [38e38edf] GLM v1.9.0
  [4076af6c] JuMP v1.16.0
  [626c502c] Parquet v0.8.4
  [91a5bcdd] Plots v1.39.0
  [438e738f] PyCall v1.96.2
⌅ [df47a6cb] RData v0.8.3
  [ce6b1742] RDatasets v0.7.7
  [9d95f2ec] TypedTables v1.4.3
⌅ [fdbf4ff8] XLSX v0.7.10

```

---

<div class="post-metadata">

**Author:** ![steveo314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steveo314/32/205248_2.png) [@steveo314](https://discourse.julialang.org/u/steveo314)\
**Post date:** [December 5, 2023, 9:37pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/5 "2023-12-05T21:37:32Z")

</div>

Same Error and Stacktrace as in my reply to @mkitti

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [December 5, 2023, 9:39pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/6 "2023-12-05T21:39:19Z")

</div>

How many lines are in the `chuckle/random.csv` file?

That is, what is the output of `wc -l chuck/random.csv`?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 5, 2023, 9:45pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/7 "2023-12-05T21:45:49Z")

</div>

Stacktrace points to

> <https://github.com/JuliaData/CSV.jl/blob/cb1b411f893e8e8cc120544ce629e07d25f2fb50/src/file.jl#L304>

> <https://github.com/JuliaData/CSV.jl/blob/cb1b411f893e8e8cc120544ce629e07d25f2fb50/src/file.jl#L514C1-L517C1>

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [December 5, 2023, 9:49pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/8 "2023-12-05T21:49:31Z")

</div>

I just realized that OP is on Windows.

For Windows, this should give you the number of lines in the file. Run the following in PowerShell:

```powershell
(Get-Content random.csv).Length

```

---

<div class="post-metadata">

**Author:** ![steveo314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steveo314/32/205248_2.png) [@steveo314](https://discourse.julialang.org/u/steveo314)\
**Post date:** [December 5, 2023, 9:50pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/9 "2023-12-05T21:50:09Z")

</div>

└─$ wc -l c:/data/chuckle/random.csv  
2326 c:/data/chuckle/random.csv

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [December 5, 2023, 9:51pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/10 "2023-12-05T21:51:18Z")

</div>

Can you try this?

`CSV.read("chuckle/random.csv", DataFrame; rows_to_check = 2326)`

---

<div class="post-metadata">

**Author:** ![steveo314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steveo314/32/205248_2.png) [@steveo314](https://discourse.julialang.org/u/steveo314)\
**Post date:** [December 5, 2023, 9:59pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/12 "2023-12-05T21:59:38Z")

</div>

Same thing

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [December 5, 2023, 10:00pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/13 "2023-12-05T22:00:14Z")

</div>

How about this?

`CSV.File("chuckle/random.csv"; rows_to_check = 2326)`

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [December 6, 2023, 12:01am UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/14 "2023-12-06T00:01:42Z")

</div>

Also, is it possible for you to share the CSV file?

---

<div class="post-metadata">

**Author:** ![steveo314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steveo314/32/205248_2.png) [@steveo314](https://discourse.julialang.org/u/steveo314)\
**Post date:** [December 6, 2023, 4:55am UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/15 "2023-12-06T04:55:28Z")

</div>

can’t share it due to its contents

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [December 6, 2023, 2:31pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/16 "2023-12-06T14:31:41Z")

</div>

Can you try doing the following?

```julia
for i in 1:2326
    @info "Attempting" i
    CSV.File("chuckle/random.csv"; limit = i, ntasks = 1)
    @info "Finished" i
end

```

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [December 6, 2023, 3:21pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/17 "2023-12-06T15:21:49Z")

</div>

For clarity, the referenced error seems to indicate that a request was made to convert a [Missing value](https://docs.julialang.org/en/v1/manual/missing/) into a string. It seems like the **CSV.jl** parser might be having some trouble with something in this `.csv` file that lead to a `::Missing` value somewhere unexpected.

---

<div class="post-metadata">

**Author:** ![steveo314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steveo314/32/205248_2.png) [@steveo314](https://discourse.julialang.org/u/steveo314)\
**Post date:** [December 6, 2023, 3:22pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/18 "2023-12-06T15:22:52Z")

</div>

I’ll have to keep that code handy. It helped find what the error was. It stopped on a line where one of the fields was like this:

```julia
...","text "text" text","....

```

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [December 6, 2023, 3:32pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/19 "2023-12-06T15:32:49Z")

</div>

Good to hear you found the problem.

I wonder if this is worth filing an Issue on the [CSV.jl repo](https://github.com/JuliaData/CSV.jl)? This seems like a relatively unhelpful error message, indicating the direct internal cause of the error (can’t convert missing to string) rather than the more pertinent error: malformed input at line n of the input file.

---

<div class="post-metadata">

**Author:** ![steveo314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steveo314/32/205248_2.png) [@steveo314](https://discourse.julialang.org/u/steveo314)\
**Post date:** [December 6, 2023, 4:09pm UTC](https://discourse.julialang.org/t/error-methoderror-cannot-convert-an-object-of-type-missing-to-an-object-of-type-string/107183/20 "2023-12-06T16:09:46Z")

</div>

I’ll post there also. And yes, there were no Missing values in the file at all.
