Simple Integer DataFrame Automatic Conversion to Float (InexactError())

290 views
Skip to first unread message

Bradley Setzler

unread,
Aug 18, 2014, 10:28:58 AM8/18/14
to julia...@googlegroups.com


Good morning,

I am looking for a simple way to convert an Integer DataFrame to a Float DataFrame. Here is an example of the problem:

julia> using DataFrames
julia> A=DataFrame([1 2; 3 4])
2x2 DataFrame:
x1 x2
[1,] 1 2
[2,] 3 4

With multiplication, there is no problem automatically converting to Float:

julia> A*.5
2x2 DataFrame:
x1 x2
[1,] 0.5 1.0
[2,] 1.5 2.0

But with division, for example, the conversion fails:

julia> A/2
InexactError()

Ideally, there would be a one-line command so that we don't have to worry about this issue, say DataFloat() of the form:

julia> A=DataFloat(A)
2x2 DataFrame:
x1 x2
[1,] 1.0 2.0
[2,] 3.0 4.0

Does something like this exist?

Thanks,
Bradley

John Myles White

unread,
Aug 18, 2014, 10:31:43 AM8/18/14
to julia...@googlegroups.com
Hi Bradley,

Would you consider using DataArrays for this? DataFrames no longer support these operations, so any upgrade in your setup would turn all of this code into errors.

All of these operations work on DataArrays already.

— John

Bradley Setzler

unread,
Aug 18, 2014, 11:02:02 AM8/18/14
to julia...@googlegroups.com
Hi John,

Thanks for your reply, I'm getting the following:

julia> A=DataArray([1 2; 3 4])
2x2 DataArray{Int64,2}:
1 2
3 4
julia> A*.5
2x2 DataArray{Float64,2}:
0.5 1.0
1.5 2.0
julia> A/2.
2x2 DataArray{Float64,2}:
0.5 1.0
1.5 2.0
julia> A/2
InexactError()

So it converts to Float if divided by Float, but does not convert if divided by Integer.

Best,
Bradley

John Myles White

unread,
Aug 18, 2014, 11:04:35 AM8/18/14
to julia...@googlegroups.com
What versions are you using? This was a bug, but I think it’s been fixed on 0.3 for some time.

 — John

Bradley Setzler

unread,
Aug 18, 2014, 11:10:44 AM8/18/14
to julia...@googlegroups.com
Update: I found a 1-line command to convert everything in a DataFrame into a Float that seems to work generally:

data = data*1.0

So, for example,
julia> A=DataFrame([1 2 ; 3 4])
2x2 DataFrame:
x1 x2
[1,] 1 2
[2,] 3 4
julia> A=A*1.0

2x2 DataFrame:
x1 x2
[1,] 1.0 2.0
[2,] 3.0 4.0

Best,
Bradley

Johan Sigfrids

unread,
Aug 18, 2014, 11:32:25 AM8/18/14
to julia...@googlegroups.com
Multiplying a DataFrame by a scalar has been deprecated and will not work once you update to Julia 0.3 and the associated DataFrames version.

Bradley Setzler

unread,
Aug 18, 2014, 12:28:48 PM8/18/14
to julia...@googlegroups.com
Thanks, Johan. I have upgraded to 0.3 release candidate and see what you mean about scalar multiplication not working for DataFrame any more. I see also that DataArray successfully converts Integer to Float in the way John was describing earlier, which solves the original problem.

Problem solved, thanks guys,
Bradley

John Myles White

unread,
Aug 18, 2014, 12:30:40 PM8/18/14
to julia...@googlegroups.com
Thank you for upgrading. You've made our lives a lot easier. :)

 -- John

Johan Sigfrids

unread,
Aug 18, 2014, 12:39:45 PM8/18/14
to julia...@googlegroups.com
There are probably going to be quite a few changes in the DataFrames package when you go from Julia 0.2 to 0.3.

Bradley Setzler

unread,
Aug 26, 2014, 5:55:34 PM8/26/14
to julia...@googlegroups.com
Following up on the changes in DataFrames Johan mentioned:

The biggest difference I have found in DataFrames for Julia 0.3 is the absence of the matrix() function. How can I quickly convert a DataFrame or DataArray into an Array without matrix()? Here's an example of the issue:


Setup:
julia> X
3x3 DataFrame
|-------|------|-----|-----|
| Row # | x1 | x2 | x3 |
| 1 | -1.0 | 0.0 | 1.0 |
| 2 | 2.0 | 3.0 | 4.0 |
| 3 | 5.0 | 6.0 | 7.0 |

julia> y
3-element DataArray{Float64,1}:
1.0
0.0
1.0

Issue:
julia> linreg(X,y)
ERROR: `linreg` has no method matching linreg(::DataFrame, ::DataArray{Float64,1})


Old Solution:
julia> linreg(matrix(X),vec(matrix(y')))
4-element Array{Float64,1}:
0.222222
-0.222222
1.38778e-16
0.222222


But matrix doesn't exist anymore. So I'm looking for commands that do the following:
(1) Convert DataArray{Float64,1} into Array{Float64,1}.
(2) Convert DataFrame into Array{Float64,2}.


How do you do this in Julia 0.3?

Thanks,

John Myles White

unread,
Aug 26, 2014, 5:56:41 PM8/26/14
to julia...@googlegroups.com
For the moment, use array().

I need to finish a pull request that does these conversions properly using the convert() function.

 -- John

Bradley Setzler

unread,
Aug 26, 2014, 5:57:57 PM8/26/14
to julia...@googlegroups.com
Perfect John, thank you!

Bradley
Reply all
Reply to author
Forward
0 new messages