Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undocumented or inconsistent :auto/Date behavior? #3485

Closed
droodman opened this issue Jan 30, 2025 · 1 comment
Closed

Undocumented or inconsistent :auto/Date behavior? #3485

droodman opened this issue Jan 30, 2025 · 1 comment

Comments

@droodman
Copy link

droodman commented Jan 30, 2025

I was surprised by this difference in results:

julia> using Dates, DataFrames

julia> v = [Date(2010,1,1)]
1-element Vector{Date}:
 2010-01-01

julia> DataFrame(v, :auto)
1×1 DataFrame
 Row │ x1
     │ UTInstan…
─────┼─────────────────────────────
   1 │ UTInstant{Day}(Day(733773))

julia> df[1,1] isa Date
false

julia> DataFrame("x" => v)
1×1 DataFrame
 Row │ x
     │ Date
─────┼────────────
   1 │ 2010-01-01

julia> df[1,1] isa Date
true

So :auto does more than automatically generate column names? It slightly affects how Dates and DateTimes are stored? Or is this an unintentional bug?

Using Julia 1.11.3 and DataFrames 1.7.0.

@bkamins
Copy link
Member

bkamins commented Jan 30, 2025

The issue is unreladed with :auto. First, what should have been done:

julia> DataFrame([v], :auto)
1×1 DataFrame
 Row │ x1
     │ Date
─────┼────────────
   1 │ 2010-01-01

What you do overrides this behaviour:

julia> DataFrame(v)
1×1 DataFrame
 Row │ instant
     │ UTInstan…
─────┼─────────────────────────────
   1 │ UTInstant{Day}(Day(733773))

(by adding extra auto-generation of column names).

This is a surprising behaviour (but documented). The constructor you are invoking is:

 DataFrame(table; copycols::Union{Bool, Nothing}=nothing)
  DataFrame(table, names::AbstractVector; makeunique::Bool=false, copycols::Union{Bool, Nothing}=nothing)

as v (without being wrapped) is a Tables.jl table. Here is another similar example of this constructor in use (hopefully less surprising):

julia> DataFrame((a=[1, 2], b=[3, 4])) # Tables.jl table constructor
  2×2 DataFrame
   Row │ a      b
       │ Int64  Int64
  ─────┼──────────────
     1 │     1      3
     2 │     2      4

  julia> DataFrame([(a=1, b=0), (a=2, b=0)]) # Tables.jl table constructor
  2×2 DataFrame
   Row │ a      b
       │ Int64  Int64
  ─────┼──────────────
     1 │     1      0
     2 │     2      0

@bkamins bkamins closed this as completed Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants