What does "DataFrame(columns::AbstractMatrix)` is deprecated, use `DataFrame(columns, :auto)`" mean?

Hi,

I re-ran some older code and see changes have been made. I tried following the discussion for this depreciation here but am not understanding the difference or what I need to do to update my code.

The warning shows up only once, which is normally a good thing, but now I cannot find the line which first causes the warning. I have several files that call each other and have been slowly working backwards, restarting Julia each time to locate where the warning first appears.

`

DataFrame(columns::AbstractMatrix) is deprecated, use DataFrame(columns, :auto) instead
`

Could someone give me a MWE of what has changed? I don’t use ::AbstractMatrix specifically, as in, using :: anywhere, so I don’t understand what this represents. I also don’t understand what :auto represents.

Could someone also suggest a better way of finding where in the code the warning is coming from? I haven’t figured out yet how to use the debugger, but I assume this would help me a lot.

Thank you.

The warning tells you exactly what to do:

C:\Users\ngudat>julia --depwarn=yes
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.6.0-beta1.0 (2021-01-08)
 _/ |\__'_|_|_|\__'_|  |  release-1.6/b84990e1ac (fork: 107 commits, 78 days)
|__/                   |

julia> using DataFrames

julia> DataFrame(rand(3, 3))
β”Œ Warning: `DataFrame(columns::AbstractMatrix)` is deprecated, use `DataFrame(columns, :auto)` instead.
β”‚   caller = ip:0x0
β”” @ Core :-1
3Γ—3 DataFrame
 Row β”‚ x1        x2        x3       
     β”‚ Float64   Float64   Float64  
─────┼──────────────────────────────
   1 β”‚ 0.622345  0.212857  0.450722
   2 β”‚ 0.964056  0.592422  0.404494
   3 β”‚ 0.779371  0.621828  0.825098

julia> DataFrame(rand(3, 3), :auto)
3Γ—3 DataFrame
 Row β”‚ x1        x2        x3       
     β”‚ Float64   Float64   Float64  
─────┼──────────────────────────────
   1 β”‚ 0.4546    0.737729  0.922938
   2 β”‚ 0.656548  0.978919  0.47053
   3 β”‚ 0.372329  0.129186  0.134449

What has been deprecated is passing a matrix (more precisely, an object whose type is a subtype of AbstractMatrix) by itself to the DataFrame constructor, without specifying column names. I.e. you could have done, without warning:

julia> DataFrame(rand(3, 3), [:a, :b, :c])
3Γ—3 DataFrame
 Row β”‚ a         b          c        
     β”‚ Float64   Float64    Float64  
─────┼───────────────────────────────
   1 β”‚ 0.740213  0.76078    0.215738
   2 β”‚ 0.680955  0.0326686  0.122183
   3 β”‚ 0.652353  0.590362   0.299639

Where I’ve passed in the column names a, b, and c. In older versions of DataFrames, if you didn’t specify column names, by default the constructor would assign x1 through to xN where N is the number of columns in your matrix. The issue you linked discusses why this was not ideal, and why the change was made, but I don’t think it’s necessary to follow all of that. All you need to know is that to get the old behaviour back, you change your code from DataFrame(my_matrix) to DataFrame(my_matrix, :auto) as explained in the deprecation warning.

To address another part of your question, you say you don’t use :: anywhere, and it seems there’s a bit of a misunderstanding here about how types and dispatch work in Julia. When I define a function f with a method f(x::Float64), that means "call this method of f whenever the type of the variable x is Float64. That means, this method will get called if I do f(1.0), because typeof(1.0) == Float64. There’s no need for me as the user who calls f to annotate the call in some way (i.e. do f(1.0::Float64)) - Julia’s type inference will work out that 1.0 has the type Float64 and therefore call the correct method.

I strongly encourage you to read the relevant sections of the manual on Types and Methods, as multiple dispatch is arguably the single most important feature of Julia. Given that you’ve been around for a while and seem to be in it for the long haul with Julia developing a better understanding of this should pay off nicely in future :slight_smile:

2 Likes