# Convert string of type/eltype to Type itself

**URL:** <https://discourse.julialang.org/t/convert-string-of-type-eltype-to-type-itself/30238>\
**Category:** General Usage\
**Created:** [October 23, 2019, 4:40pm UTC](https://discourse.julialang.org/t/convert-string-of-type-eltype-to-type-itself/30238 "2019-10-23T16:40:27Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![FredC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredc/32/9857_2.png) [@FredC](https://discourse.julialang.org/u/FredC)\
**Post date:** [October 23, 2019, 4:40pm UTC](https://discourse.julialang.org/t/convert-string-of-type-eltype-to-type-itself/30238/1 "2019-10-23T16:40:27Z")

</div>

I have an Array{String,1} which I want to convert into an array of data types to use with loadnsparse in JuliaDB. There appears to be no way to convert a string such as “Union{Missing, Int64}” into the obvious type?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 23, 2019, 4:49pm UTC](https://discourse.julialang.org/t/convert-string-of-type-eltype-to-type-itself/30238/2 "2019-10-23T16:49:18Z")

</div>

Going from string to type is the same as going from code to result, it’s not conversion, it’s called running the code and you can do that with parsing and eval.

That’s almost always a terrible idea. Why do you have a string of expressions that you want to evaluate to type?

---

<div class="post-metadata">

**Author:** ![FredC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredc/32/9857_2.png) [@FredC](https://discourse.julialang.org/u/FredC)\
**Post date:** [October 23, 2019, 5:01pm UTC](https://discourse.julialang.org/t/convert-string-of-type-eltype-to-type-itself/30238/3 "2019-10-23T17:01:52Z")

</div>

I got the types of a sample of the data I want to load, but some columns were of type Missing due to the sparseness of the data, when the full data will be Union of Missing and Int64. I want to use the correct types in the colparsers argument to JuliaDB.loadndsparse. So I describe()ed the sample dataframe, exported to CSV (which is also broken and requires using show()) filled in the blanks, and reimported as strings.

And here I am…

---

<div class="post-metadata">

**Author:** ![FredC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredc/32/9857_2.png) [@FredC](https://discourse.julialang.org/u/FredC)\
**Post date:** [October 23, 2019, 5:51pm UTC](https://discourse.julialang.org/t/convert-string-of-type-eltype-to-type-itself/30238/4 "2019-10-23T17:51:08Z")

</div>

So I made a dict from string to type and mapped the dataframe column… 🤢

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 23, 2019, 6:37pm UTC](https://discourse.julialang.org/t/convert-string-of-type-eltype-to-type-itself/30238/5 "2019-10-23T18:37:26Z")

</div>

It’s still not clear why you need to go from types to strings and back to types at all. Why can’t you just call `eltypes()` on your DataFrame to get a vector of element types (as _types_, not strings)?

---

<div class="post-metadata">

**Author:** ![FredC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredc/32/9857_2.png) [@FredC](https://discourse.julialang.org/u/FredC)\
**Post date:** [October 23, 2019, 10:02pm UTC](https://discourse.julialang.org/t/convert-string-of-type-eltype-to-type-itself/30238/6 "2019-10-23T22:02:52Z")

</div>

Because the dataframe has missing entries. Does Julia really not have an easy way to load an external data schema for an input file?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 23, 2019, 10:13pm UTC](https://discourse.julialang.org/t/convert-string-of-type-eltype-to-type-itself/30238/7 "2019-10-23T22:13:19Z")

</div>

I’m still missing something here. As I understand it, you’re saying that saving the results of `describe()` to a CSV file is giving a different column element type than `eltypes()` would? Is that right? And can you give an example of the kind of situation you’re seeing? It’s hard to be helpful in the abstract.

---

<div class="post-metadata">

**Author:** ![FredC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredc/32/9857_2.png) [@FredC](https://discourse.julialang.org/u/FredC)\
**Post date:** [October 23, 2019, 10:30pm UTC](https://discourse.julialang.org/t/convert-string-of-type-eltype-to-type-itself/30238/8 "2019-10-23T22:30:35Z")

</div>

Describe is running on a sample of the full dataset. Some of the columns are sparse and therefore empty, or the source is using numbers for chars and other such stupidities which require manual adjustment.

Like I mentioned, I managed a solution so this topic is more of a “we should cover this case if we’re serious about beating Python” bin. I have a [New topic](https://discourse.julialang.org/t/juliadb-loadndsparse-many-errors/30249) in usage regarding my current travails with JuliaDB which is my immediate concern.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 23, 2019, 11:03pm UTC](https://discourse.julialang.org/t/convert-string-of-type-eltype-to-type-itself/30238/9 "2019-10-23T23:03:20Z")

</div>

What you mean by

> [@FredC](#):
>
> manual adjustment.

?

Is it done with a script or did you just open a file to edit it?

If it’s a script then you should be able to manipulate the data directly without saving it to a file and not having to worry about retrieving the type from the file.

If it’s manual editing, well, could you just edit (a copy of) the original data? Are you using the save to csv step as a way to convert the original data to a more editable format? Are you saving the edited data as the new original data for all processing or are you just looking for a interactive way to fix the data as they come in before you do more processing on them?

Also, taking a step back, if the editing is not scriptable, since you are saying that you are “loading” the data, I assume the type information you need were all in that original data. What format is that?

---

<div class="post-metadata">

**Author:** ![FredC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredc/32/9857_2.png) [@FredC](https://discourse.julialang.org/u/FredC)\
**Post date:** [October 23, 2019, 11:16pm UTC](https://discourse.julialang.org/t/convert-string-of-type-eltype-to-type-itself/30238/10 "2019-10-23T23:16:21Z")

</div>

Type information is external. Think of it like a file containing the column names, except it’s types.

I’m sorry the stuff about sampling the data and generating a dataframe of the Describe() output and editing that was confusing. That’s just something I did as a first pass because the data is too big to load into memory via normal CSV methods.

The issue is that the data has a separate list of types that I would like to load as such, much like column names can be separately stored and loaded.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 24, 2019, 12:12am UTC](https://discourse.julialang.org/t/convert-string-of-type-eltype-to-type-itself/30238/11 "2019-10-24T00:12:00Z")

</div>

The question I was asking from the start is that why you have a list of “external” types to begin with. You shouldn’t need that.

---

<div class="post-metadata">

**Author:** ![FredC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredc/32/9857_2.png) [@FredC](https://discourse.julialang.org/u/FredC)\
**Post date:** [October 24, 2019, 12:36am UTC](https://discourse.julialang.org/t/convert-string-of-type-eltype-to-type-itself/30238/12 "2019-10-24T00:36:23Z")

</div>

So what do you do when the data is too large and sparse to have its types detected? I have 2000+ columns to load here.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 24, 2019, 12:41am UTC](https://discourse.julialang.org/t/convert-string-of-type-eltype-to-type-itself/30238/13 "2019-10-24T00:41:51Z")

</div>

If you want to manually specify the type, that’s exactly what you should do. And you manutally specify the types as code.

---

<div class="post-metadata">

**Author:** ![FredC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredc/32/9857_2.png) [@FredC](https://discourse.julialang.org/u/FredC)\
**Post date:** [October 24, 2019, 11:22pm UTC](https://discourse.julialang.org/t/convert-string-of-type-eltype-to-type-itself/30238/14 "2019-10-24T23:22:12Z")

</div>

```julia
Meta.eval(Meta.parse(“Float64”))

```

This gives me a result that is a data type. The Meta package appears to be built in and doesn’t need to be added.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 25, 2019, 12:22am UTC](https://discourse.julialang.org/t/convert-string-of-type-eltype-to-type-itself/30238/15 "2019-10-25T00:22:47Z")

</div>

Yes, just don’t treat that string as data. You have to trust that string as much as you trust your code.

Also, don’t use Meta.eval. use eval.
