# \#convert

**URL:** https://discourse.julialang.org/tag/convert/408.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [Switching off implicit conversion](https://discourse.julialang.org/t/switching-off-implicit-conversion/134568)

<div class="topic-metadata">

**Author:** [@sadish-d](https://discourse.julialang.org/u/sadish-d)\
**Replies:** 25\
**Last updated:** [December 17, 2025, 2:04pm UTC](https://discourse.julialang.org/t/switching-off-implicit-conversion/134568 "2025-12-17T14:04:29Z")

</div>

Let’s work with an example. Say I have two different units of length. abstract type Length end struct Meters \<: Length value::Float64 end struct CentiMeters \<: Length value::Float64 end I want to use the existing…

---

## [Should Rationals be explicitly converted?](https://discourse.julialang.org/t/should-rationals-be-explicitly-converted/131350)

<div class="topic-metadata">

**Author:** [@NonDairyNeutrino](https://discourse.julialang.org/u/NonDairyNeutrino)\
**Replies:** 11\
**Last updated:** [August 5, 2025, 5:56pm UTC](https://discourse.julialang.org/t/should-rationals-be-explicitly-converted/131350 "2025-08-05T17:56:46Z")

</div>

The question is basically, which is “better” in terms of intermediate type-stability and/or performance: function half\_promote(x :: T) :: T where T \<: AbstractFloat half = 1//2 return half \* x end or function …

---

## [Conversion of inner "flag" type parameters from supertype "flag" type parameters for composite struct construcor](https://discourse.julialang.org/t/conversion-of-inner-flag-type-parameters-from-supertype-flag-type-parameters-for-composite-struct-construcor/131306)

<div class="topic-metadata">

**Author:** [@kunzaatko](https://discourse.julialang.org/u/kunzaatko)\
**Replies:** 3\
**Last updated:** [August 2, 2025, 1:08pm UTC](https://discourse.julialang.org/t/conversion-of-inner-flag-type-parameters-from-supertype-flag-type-parameters-for-composite-struct-construcor/131306 "2025-08-02T13:08:17Z")

</div>

I have a type structure that looks something like this: abstract type AbstractA{T} end struct A1{T} \<: AbstractA{T} end # no field of type \`T\` A1() = A1{Any}() struct A2{T} \<: AbstractA{T} end A2() = A2{Any}() and I wo…

---

## [Order of Base.convert calls with \`@kwdef\` constructors](https://discourse.julialang.org/t/order-of-base-convert-calls-with-kwdef-constructors/130460)

<div class="topic-metadata">

**Author:** [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Replies:** 4\
**Last updated:** [July 4, 2025, 5:22am UTC](https://discourse.julialang.org/t/order-of-base-convert-calls-with-kwdef-constructors/130460 "2025-07-04T05:22:38Z")

</div>

Base.convert( ::Type{Int64}, x::AbstractString ) = parse(Int64,x) @kwdef struct X a::Int64 b::Int64=a+1 end X(a="1") ERROR: MethodError: no method matching +(::String, ::Int64) Can I make the conversion pars…

---

## [Matrix to dataframe conversion with structure of existing dataframe](https://discourse.julialang.org/t/matrix-to-dataframe-conversion-with-structure-of-existing-dataframe/129278)

<div class="topic-metadata">

**Author:** [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Replies:** 4\
**Last updated:** [May 28, 2025, 8:51am UTC](https://discourse.julialang.org/t/matrix-to-dataframe-conversion-with-structure-of-existing-dataframe/129278 "2025-05-28T08:51:46Z")

</div>

Is there any built-in function to convert a matrix to a DataFrame with the structure of a second DataFrame ? i.e. : df = DataFrame(a = \[1,2,3\], b= \["a","b","c"\]) # Option 1, types not promoted dfnames = names(df) m = …

---

## [Limitations of \`Base.convert\` for the construction of parametric types](https://discourse.julialang.org/t/limitations-of-base-convert-for-the-construction-of-parametric-types/127977)

<div class="topic-metadata">

**Author:** [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Replies:** 4\
**Last updated:** [April 11, 2025, 9:35pm UTC](https://discourse.julialang.org/t/limitations-of-base-convert-for-the-construction-of-parametric-types/127977 "2025-04-11T21:35:25Z")

</div>

I have a couple of user-defined types in which one contains the other, as in the following minimal example: struct MyType x::Float64 end struct Container c::MyType end In this example, MyType is some kind of F…

---

## [Convert vector of binaries to decimal and/or hex](https://discourse.julialang.org/t/convert-vector-of-binaries-to-decimal-and-or-hex/124974)

<div class="topic-metadata">

**Author:** [@stefaniecg](https://discourse.julialang.org/u/stefaniecg)\
**Replies:** 4\
**Last updated:** [January 20, 2025, 4:15pm UTC](https://discourse.julialang.org/t/convert-vector-of-binaries-to-decimal-and-or-hex/124974 "2025-01-20T16:15:43Z")

</div>

I want to convert a binary string to decimal. This converst one item of the array to an int value: bit\_vector = digits(0x0284, base=2, pad=16) convert(Int,bit\_vector\[1\]) but i want something like this, but it does not…

---

## [How can I convert a CompoundPeriod type object to Period type in Julia?](https://discourse.julialang.org/t/how-can-i-convert-a-compoundperiod-type-object-to-period-type-in-julia/60930)

<div class="topic-metadata">

**Author:** [@shubh\_b](https://discourse.julialang.org/u/shubh_b)\
**Replies:** 19\
**Last updated:** [November 20, 2024, 1:08pm UTC](https://discourse.julialang.org/t/how-can-i-convert-a-compoundperiod-type-object-to-period-type-in-julia/60930 "2024-11-20T13:08:45Z")

</div>

Using Dates module in Julia I am able to transform a Period type object (Hour, Minute, Second, Day etc.) to another Period type as below: using Dates julia\> convert(Day, Hour(96)) 4 days julia\> convert(Second, Hour(20…

---

## [Custom implicit type conversion](https://discourse.julialang.org/t/custom-implicit-type-conversion/117315)

<div class="topic-metadata">

**Author:** [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Replies:** 14\
**Last updated:** [July 27, 2024, 1:37am UTC](https://discourse.julialang.org/t/custom-implicit-type-conversion/117315 "2024-07-27T01:37:18Z")

</div>

I would like to specify my own implicit type conversion and according to Conversion and Promotion · The Julia Language I should be able to do something like: julia\> struct AA end julia\> Base.convert(::Type{Float64}, x:…

---

## [Promoting a broadcasted array](https://discourse.julialang.org/t/promoting-a-broadcasted-array/117498)

<div class="topic-metadata">

**Author:** [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Replies:** 5\
**Last updated:** [July 26, 2024, 11:36am UTC](https://discourse.julialang.org/t/promoting-a-broadcasted-array/117498 "2024-07-26T11:36:03Z")

</div>

Consider: julia\> ifelse.(\[true, false\], \[1,2\], \[3.0,4.5\]) 2-element Vector{Real}: 1 4.5 This isn’t ideal; in this case clearly a concrete eltype would be better. I know I can write it as float.(ifelse.(...)) to solve…

---

## [Date to float](https://discourse.julialang.org/t/date-to-float/20094)

<div class="topic-metadata">

**Author:** [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Replies:** 8\
**Last updated:** [July 22, 2024, 4:23pm UTC](https://discourse.julialang.org/t/date-to-float/20094 "2024-07-22T16:23:34Z")

</div>

Is there an easy way to convert date/datetime to a float representing year? I.e. f(2000-01-01) = 2000, f(2015-07-01) ~ 2015.5 and so on. The most obvious method is 2000 + (x - DateTime(2000, 1, 1)) / (DateTime(2001, 1, 1…

---

## [Outputting string columns using Avro.jl](https://discourse.julialang.org/t/outputting-string-columns-using-avro-jl/80112)

<div class="topic-metadata">

**Author:** [@kobusherbst](https://discourse.julialang.org/u/kobusherbst)\
**Replies:** 5\
**Last updated:** [April 15, 2024, 5:13pm UTC](https://discourse.julialang.org/t/outputting-string-columns-using-avro-jl/80112 "2024-04-15T17:13:51Z")

</div>

When I try to convert an Arrow file to Avro using this code: function t\_arrowtoavro(path, file) a = Arrow.Table(joinpath(path, "$(file).arrow")) |\> DataFrame println(Tables.schema(a)) Avro.writetable(joinpat…

---

## [Convert a tuple of matrix Float64 data to Float32](https://discourse.julialang.org/t/convert-a-tuple-of-matrix-float64-data-to-float32/112005)

<div class="topic-metadata">

**Author:** [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Replies:** 1\
**Last updated:** [March 23, 2024, 4:34am UTC](https://discourse.julialang.org/t/convert-a-tuple-of-matrix-float64-data-to-float32/112005 "2024-03-23T04:34:10Z")

</div>

I have a tuple of matrices data of type Float64 . But when i want to convert it to Float32 it gives error How to convert a tuple of matrix Float64 data to Float32 ?

---

## [\`Type(val)\` vs \`convert(Type, val)\` (Converting more than 365 days to months and years)](https://discourse.julialang.org/t/type-val-vs-convert-type-val-converting-more-than-365-days-to-months-and-years/105684)

<div class="topic-metadata">

**Author:** [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Replies:** 14\
**Last updated:** [November 3, 2023, 1:32pm UTC](https://discourse.julialang.org/t/type-val-vs-convert-type-val-converting-more-than-365-days-to-months-and-years/105684 "2023-11-03T13:32:37Z")

</div>

I was wondering how many years were between two events. So I define the timepoints, and find their difference: julia\> using Dates julia\> d0 = Date(1980, 6, 17) 1980-06-17 julia\> d1 = Date(2015, 1, 4) 2015-01-04 julia\>…

---

## [Oscar and DynamicaPolynomials: Convert polynomial type for basis of invariant rings](https://discourse.julialang.org/t/oscar-and-dynamicapolynomials-convert-polynomial-type-for-basis-of-invariant-rings/102422)

<div class="topic-metadata">

**Author:** [@ilovemath](https://discourse.julialang.org/u/ilovemath)\
**Replies:** 6\
**Last updated:** [September 14, 2023, 2:57am UTC](https://discourse.julialang.org/t/oscar-and-dynamicapolynomials-convert-polynomial-type-for-basis-of-invariant-rings/102422 "2023-09-14T02:57:48Z")

</div>

I would like to know if there is a way to convert a type nf\_elem to a polynomial. My goal with this code is to find all minimal monomials of degree d that obey a rotational symmetry, so I can directly construct a polyno…

---

## [Conversion of nothing to void pointer](https://discourse.julialang.org/t/conversion-of-nothing-to-void-pointer/102766)

<div class="topic-metadata">

**Author:** [@projekter](https://discourse.julialang.org/u/projekter)\
**Replies:** 11\
**Last updated:** [August 14, 2023, 1:10pm UTC](https://discourse.julialang.org/t/conversion-of-nothing-to-void-pointer/102766 "2023-08-14T13:10:37Z")

</div>

It is quite common in C interfaces to have functions that accept pointers to something, but they can also be given NULL instead, which means that for example the part that is related with this parameter is not of interes…

---

## [Trying to translate Matlab code to Julia - need help #50640](https://discourse.julialang.org/t/trying-to-translate-matlab-code-to-julia-need-help-50640/102111)

<div class="topic-metadata">

**Author:** [@manuelcaeiro](https://discourse.julialang.org/u/manuelcaeiro)\
**Replies:** 14\
**Last updated:** [July 27, 2023, 6:26pm UTC](https://discourse.julialang.org/t/trying-to-translate-matlab-code-to-julia-need-help-50640/102111 "2023-07-27T18:26:38Z")

</div>

I’m trying to translate the following code in Matlab: e = 1.05; %Eccentricity a = -147562.72; %km b = abs(a)\*sqrt(e.^2 -1); %Equation for Semi-Minor Axis, b x1 = linspace( a, 2\*a, 1E3); %…

---

## [Convert matrix of 3D arrays to 3D array](https://discourse.julialang.org/t/convert-matrix-of-3d-arrays-to-3d-array/101888)

<div class="topic-metadata">

**Author:** [@mastrof](https://discourse.julialang.org/u/mastrof)\
**Replies:** 12\
**Last updated:** [July 23, 2023, 9:05am UTC](https://discourse.julialang.org/t/convert-matrix-of-3d-arrays-to-3d-array/101888 "2023-07-23T09:05:04Z")

</div>

I have N^2 3D arrays, all with size (nx, ny, nz). These arrays are arranged in a square matrix (N x N). I would like to expand the inner arrays concatenating them along the first two dimensions but not along the third, s…

---

## [Solving A\*x=b in Nemo](https://discourse.julialang.org/t/solving-a-x-b-in-nemo/101051)

<div class="topic-metadata">

**Author:** [@blociss](https://discourse.julialang.org/u/blociss)\
**Replies:** 7\
**Last updated:** [July 3, 2023, 7:30am UTC](https://discourse.julialang.org/t/solving-a-x-b-in-nemo/101051 "2023-07-03T07:30:06Z")

</div>

Let A = \[1 0 0; 0 1 1; 0 3 0; 0 0 1\] a matrix of type 4×3 Matrix{QQFieldElem}: and Q=\[2 ,-3 , 1, 1\] of type 4-element Vector{QQFieldElem}: My goal is to compute A \\setminus Q but I got error MethodError: no m…

---

## [Converting python code into julia code](https://discourse.julialang.org/t/converting-python-code-into-julia-code/98991)

<div class="topic-metadata">

**Author:** [@RDuarte](https://discourse.julialang.org/u/RDuarte)\
**Replies:** 9\
**Last updated:** [May 17, 2023, 2:29pm UTC](https://discourse.julialang.org/t/converting-python-code-into-julia-code/98991 "2023-05-17T14:29:54Z")

</div>

Hello everyone, I’m new in Julia word and I would like to translate a code/function I have in python to julia language but I’m didn’t have success till this moment. The function I have is: def buffer(X, n, p=0, opt=No…

---

## [DataFrames: convert column data type](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522)

<div class="topic-metadata">

**Author:** [@Usor](https://discourse.julialang.org/u/Usor)\
**Replies:** 65\
**Last updated:** [May 11, 2023, 1:37pm UTC](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522 "2023-05-11T13:37:19Z")

</div>

Was looking around, but didn’t find an answer. Specifically, I have a DataFrame and one column has data type Int64; it has the unique values 0 and 1 (meaning obviously false and true). What is the easiest quickest way t…

---

## [Converting array to a vector](https://discourse.julialang.org/t/converting-array-to-a-vector/34881)

<div class="topic-metadata">

**Author:** [@bandaya](https://discourse.julialang.org/u/bandaya)\
**Replies:** 15\
**Last updated:** [April 14, 2023, 7:29am UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881 "2023-04-14T07:29:58Z")

</div>

I have an array T which is 76×1 Array{Any,2}. How can I convert it into 76-element Array{Float64,1} or a 76 element row vector? This array contains data only in rows.

---

## [How to convert the String15 datatype in DataFrames.jl to Float64](https://discourse.julialang.org/t/how-to-convert-the-string15-datatype-in-dataframes-jl-to-float64/95907)

<div class="topic-metadata">

**Author:** [@hungpham3112](https://discourse.julialang.org/u/hungpham3112)\
**Replies:** 9\
**Last updated:** [March 12, 2023, 5:52pm UTC](https://discourse.julialang.org/t/how-to-convert-the-string15-datatype-in-dataframes-jl-to-float64/95907 "2023-03-12T17:52:14Z")

</div>

I have a DataFrame with 2 columns in type String15 and String31, respectively. Now I want to convert element type from 2 columns to Float64 but I can’t found any document or example in DataFrames.jl’s document. My f…

---

## [Converting \`\`\`ComplexF64\`\`\` array to \`\`\`Float64\`\`\` array in-place?](https://discourse.julialang.org/t/converting-complexf64-array-to-float64-array-in-place/94007)

<div class="topic-metadata">

**Author:** [@bremez](https://discourse.julialang.org/u/bremez)\
**Replies:** 5\
**Last updated:** [February 6, 2023, 4:17pm UTC](https://discourse.julialang.org/t/converting-complexf64-array-to-float64-array-in-place/94007 "2023-02-06T16:17:04Z")

</div>

I have a calculation that manipulates and produces a ComplexF64 array (whose dimension varies by usage in different places in my codebase). I know the final result must be an array of real numbers, or suppose I only care…

---

## [Converting entire custom structure from cpu to gpu and viceversa](https://discourse.julialang.org/t/converting-entire-custom-structure-from-cpu-to-gpu-and-viceversa/93203)

<div class="topic-metadata">

**Author:** [@carlogalli](https://discourse.julialang.org/u/carlogalli)\
**Replies:** 2\
**Last updated:** [January 19, 2023, 2:40pm UTC](https://discourse.julialang.org/t/converting-entire-custom-structure-from-cpu-to-gpu-and-viceversa/93203 "2023-01-19T14:40:56Z")

</div>

Hi! I am looking for a good way to convert a custom structure of Arrays to CuArrays and viceversa. I suspect the answer is related to them and to this topic, but I am not well versed in the usage of convert and adapt so…

---

## [Overwriting matrix elements of a different type](https://discourse.julialang.org/t/overwriting-matrix-elements-of-a-different-type/89572)

<div class="topic-metadata">

**Author:** [@chadagreene](https://discourse.julialang.org/u/chadagreene)\
**Replies:** 12\
**Last updated:** [November 2, 2022, 4:58pm UTC](https://discourse.julialang.org/t/overwriting-matrix-elements-of-a-different-type/89572 "2022-11-02T16:58:34Z")

</div>

Suppose I have this matrix: M = \[1 2 3; 4 5 6\]; The type of M is Int64, but now I want to update one of the values to a non-integer: M\[1\] = pi Of course, the above just produces this error: ERROR: MethodError: no me…

---

## [How to convert from bool to float](https://discourse.julialang.org/t/how-to-convert-from-bool-to-float/89216)

<div class="topic-metadata">

**Author:** [@Xavier\_Gonzalez](https://discourse.julialang.org/u/Xavier_Gonzalez)\
**Replies:** 2\
**Last updated:** [October 24, 2022, 10:40pm UTC](https://discourse.julialang.org/t/how-to-convert-from-bool-to-float/89216 "2022-10-24T22:40:58Z")

</div>

I would like to take a matrix that is currently made of bools and turn it into floats. If I is this matrix of bools, I’ve been trying commands like convert(Int8, I) and getting errors like ERROR: MethodError: Cannot \`c…

---

## [Cannot convert despite convert method being defined](https://discourse.julialang.org/t/cannot-convert-despite-convert-method-being-defined/88813)

<div class="topic-metadata">

**Author:** [@HashBrown](https://discourse.julialang.org/u/HashBrown)\
**Replies:** 1\
**Last updated:** [October 16, 2022, 8:17pm UTC](https://discourse.julialang.org/t/cannot-convert-despite-convert-method-being-defined/88813 "2022-10-16T20:17:27Z")

</div>

I have the following sample code: struct Bar b:: Int64 end convert(::Type{Bar}, x::Int64) = Bar(x) struct Foo b::Bar end I execute it in the REPL. I then write f = Foo(1) I expect it to work because I have…

---

## [Best way to convert/collect/promote into a concrete struct?](https://discourse.julialang.org/t/best-way-to-convert-collect-promote-into-a-concrete-struct/87953)

<div class="topic-metadata">

**Author:** [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Replies:** 5\
**Last updated:** [September 30, 2022, 7:24pm UTC](https://discourse.julialang.org/t/best-way-to-convert-collect-promote-into-a-concrete-struct/87953 "2022-09-30T19:24:18Z")

</div>

I have the struct below that I am trying to make more flexible: struct PolyFit x::Vector{Float64} y::Vector{Float64} knots::Vector{Int64} segmentlengths::Vector{Float64} polys::Vector{Polynomial{Floa…

---

## [Error: Cannot 'Convert' an object of type Array{Float64,3} to an object of type float64](https://discourse.julialang.org/t/error-cannot-convert-an-object-of-type-array-float64-3-to-an-object-of-type-float64/87928)

<div class="topic-metadata">

**Author:** [@mcket747](https://discourse.julialang.org/u/mcket747)\
**Replies:** 3\
**Last updated:** [September 29, 2022, 2:59pm UTC](https://discourse.julialang.org/t/error-cannot-convert-an-object-of-type-array-float64-3-to-an-object-of-type-float64/87928 "2022-09-29T14:59:18Z")

</div>

Hello, I am very new to Julia and am running into this error. Operations with similar code have not returned this error so Im not sure what I’m doing wrong. I run the following code and get the can’t convert error. Any …

[Next page](https://discourse.julialang.org/tag/convert/408.md?match_all_tags=true&page=1&tags%5B%5D=convert)
