Converting a Time() to an standardized Int64 or Float64

Hello Everyone:

I instantiated a variable using

Sprints = Time(00,01,00):Dates.Minute(1):Time(01,00,00)

and created a data frame with:

Allstars = DataFrame(Id = 1:10, BestTimes = rand(Sprints, 10))

How could I convert items in :BestTimes to
standardized Int64 or Float64()?

Would you approach this doing something like:

DF = :BestTimes.value(Dates.minutes(5))
then
convert.(DF, Int64)

??

How would I manage zeros?

Thank you,

Hi, the following creates a column with integer seconds:

using Dates, DataFrames

Sprints = Time(00,01,00):Dates.Minute(1):Time(01,00,00)
Allstars = DataFrame(Id = 1:10, BestTimes = rand(Sprints, 10))

Allstars[:,:IntBestTimes] = round.(Int64, Dates.value.(Allstars[:,:BestTimes])/1e9)

julia> Allstars
10Γ—3 DataFrame
 Row β”‚ Id     BestTimes  IntBestTimes 
     β”‚ Int64  Time       Int64        
─────┼────────────────────────────────
   1 β”‚     1  00:16:00            960
   2 β”‚     2  00:59:00           3540
   3 β”‚     3  00:18:00           1080
   4 β”‚     4  00:10:00            600
   5 β”‚     5  00:54:00           3240
   6 β”‚     6  00:12:00            720
   7 β”‚     7  00:08:00            480
   8 β”‚     8  00:29:00           1740
   9 β”‚     9  00:33:00           1980
  10 β”‚    10  00:07:00            420

or to get seconds as Float64:

Allstars[:,:FloatBestTimes] = Dates.value.(Allstars[:,:BestTimes])/1e9

@rafael.guerra Thank you – worked.

If I wanted to convert the Int or Float columns
to minutes would I:

Allstars[:,:FloatBestTimes] = Dates.minute.(Allstars[:,:BestTimes])

Two follow-up question:

  1. From what baseline are you deriving 1e9?
  2. Why does 'round.Float64, Dates.value…)
    return a no method matching? Is there some
    logic or performance issue with this?

Thank you,

Hi, for the first question, from the doc: Time wraps a Nanosecond and represents a specific moment in a 24-hour day. So the baseline are nanoseconds.

1 Like

For the second question, rounding a Float64 to a Float64 (does not seem very useful) and it is not defined in Julia:

julia> round(Float64, 1.2)
ERROR: MethodError: no method matching round(::Type{Float64}, ::Float64)

The language experts will need to explain this.

@rafael.guerra

I meant:

round.(Int64, Dates.value.(Allstars[:,:BestTimes])/1e9)

To:

round.(Float64, Dates.value.(Allstars[:,:BestTimes])/1e9)

The :BestTimes attribute is a Float64 implicitly?

We have integers:

Dates.value.(Allstars[:,:BestTimes])

that get promoted to floats after division by 1e9. So your line of code is trying to round Float64 to Float64.

@rafael.guerra Thanks for clarifying.