# Reading a CSV file with an unconventional string for missing values using TextParse

**URL:** https://discourse.julialang.org/t/reading-a-csv-file-with-an-unconventional-string-for-missing-values-using-textparse/10631
**Category:** Data
**Created:** [May 1, 2018, 2:39am UTC](https://discourse.julialang.org/t/reading-a-csv-file-with-an-unconventional-string-for-missing-values-using-textparse/10631 "2018-05-01T02:39:59Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)
#### Post date: [May 1, 2018, 2:39am UTC](https://discourse.julialang.org/t/reading-a-csv-file-with-an-unconventional-string-for-missing-values-using-textparse/10631/1 "2018-05-01T02:39:59Z")

</div>

I am trying to read a CSV file with two columns (`Date, Float64`) using `TextParse`. Unfortunately missing values are coded as “-” in that file. When `TextParse` encounters one of those “-” one gets the following error message:

```julia
MethodError: Cannot `convert` an object of type Float64 to an object of type TextParse.StrRange
This may have arisen from a call to the constructor TextParse.StrRange(...),
since type constructors fall back to convert methods.
ERROR: LoadError: CSV parsing error at line 1539 char 12:
2012-05-01,-
___________ ^
column 2 is expected to be: TextParse.Field{Float64,TextParse.Numeric{Float64}}(<Float64>, true, true, true)

```

I have been unable to add “-” to the set of strings that are to be considered `NA` by `TextParse`. Which would be the best approach to solve this problem?

Thank you for your help!

---

<div class="post-metadata">

### Author: ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)
#### Post date: [May 1, 2018, 3:59am UTC](https://discourse.julialang.org/t/reading-a-csv-file-with-an-unconventional-string-for-missing-values-using-textparse/10631/2 "2018-05-01T03:59:15Z")

</div>

I am not an expert here, but I would first read the file and then do the missing substitution. this is because it would be difficult to determine where your unusual character sits, and whether it is inside a quote or not. csv is a nasty format!

/iaw

---

<div class="post-metadata">

### Author: ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)
#### Post date: [May 1, 2018, 9:07am UTC](https://discourse.julialang.org/t/reading-a-csv-file-with-an-unconventional-string-for-missing-values-using-textparse/10631/3 "2018-05-01T09:07:45Z")

</div>

Looks like the same bug as [https://github.com/JuliaData/CSV.jl/issues/198](https://github.com/JuliaData/CSV.jl/issues/198).

---

<div class="post-metadata">

### Author: ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)
#### Post date: [May 1, 2018, 1:21pm UTC](https://discourse.julialang.org/t/reading-a-csv-file-with-an-unconventional-string-for-missing-values-using-textparse/10631/4 "2018-05-01T13:21:57Z")

</div>

`JuliaDB.loadtable("myfile.csv", colparsers = [Date], nastrings=["-"])` works

```julia
#=
MWE
Input: myfile.csv
2018-01-04,1.4389
2018-01-01,-
2017-12-31,-1.4406

Output:
Table with 3 rows, 2 columns:
1 2
───────────────────
2018-01-04,1.4389
2018-01-01,#NA
2017-12-31,-1.4406
=#

```

but couldn’t find an equivalent argument to - `nastrings` - for `TextParse` (also get this CSV file from a remote URL)

---

<div class="post-metadata">

### Author: ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)
#### Post date: [May 2, 2018, 8:04pm UTC](https://discourse.julialang.org/t/reading-a-csv-file-with-an-unconventional-string-for-missing-values-using-textparse/10631/5 "2018-05-02T20:04:32Z")

</div>

Got it for `TextParse`:

`TextParse.csvread("myfile.csv", colparsers = [Date, TextParse.NAToken(TextParse.Numeric(Float64), nastrings = ["-"])])`
