Add personalized column for "time intervals"

Hello! Im working writing CSV of a diferents datas in columns in julia. I want to add to this csv file a new colum named “measure_time” for time intervals personalized by the user for each row. For example, my data has 15 min difference for each other, so i want to this colums be:
“Measure_Time data1 data2 data3
00:00 1 2 5
00:15 6 4 2
00:30 1 2 3
00:45” 2 3 5"

This is straightforward with the Dates standard library:

julia> using Dates

julia> (Measure_Time = [Time(0, i) for i ∈ 0:15:45], data1 = rand(1:5, 4))
(Measure_Time = Time[Time(0), Time(0, 15), Time(0, 30), Time(0, 45)], data1 = [2, 2, 5, 2])

The second line constructs a named tuple which is the simplest Tables.jl-compatible table you can write to a csv.

Thanks for your quick response! In this case, for i ∈ 0:15:45] builds me up to the data up to “00:45”, how should I do it so that the 96 intervals of the day are calculated? That is to say :
00:00
00:15
00:30
00:45
01:00
01:15
.
.
.
23:45

Simply Measure_Time = Time(0):Minute(15):Time(23, 45)

Sorry it’s not working for me. The csv is still saved without adding this new column. I am doing it this way:

Measure_names = myFction.dss.Circuit.AllBusNames()
one_iter = myFction.iter[1]
two_iter = myFction.iter[2]

for i in 1:phases
CSV.write(pathname*"\DB_MachineLearning\my_data"string(i)"one_iter_“string(one_iter)“two_iter"string(two_iter)”.csv”,
DataFrame(Measure_values[:,:,i],:auto),header = Measure_names, decima l= ‘.’)

end

to:

for i in 1:phases
CSV.write(pathname*"\DB_MachineLearning\my_data"string(i)"one_iter_“string(one_iter)“two_iter"string(two_iter)”.csv”,
DataFrame(Measure_values[:,:,i],:auto), Measure_Time = [Time(0):Minute(15):Time(23, 45)], header= Measure_names ,decimal = ‘.’)

end

Thanks for helping me

Maybe add Measure_Time to the saved DataFrame. Something like:

df = DataFrame(Measure_values[:,:,i],:auto)
df.Measure_Time = [Time(0):Minute(15):Time(23, 45)][1:nrow(df)]
CSV.write(pathname*"\DB_MachineLearning\my_data"string(i)"one_iter_“string(one_iter)“two_iter"string(two_iter)”.csv”, df,
  header= vcat(Measure_names, "Measure_Time") ,decimal = ‘.’)

Note that it is quite confusing to read your code when it isn’t formatted properly (with triple quotes around code). There is a preview pane, and an edit button to help you get your posts in best form.

Hello. Thank you for your answer and teach me how to use the respective code format. An error has occurred in the df.Measure_Time line. In this case I have 96 measurements.

df = DataFrame(Measure_values[:,:,i],:auto)
df.Measure_Time = ([Time(0):Minute(15):Time(23, 45)][1:nrow(df)])
CSV.write(pathname*"\DB_MachineLearning\my_data"string(i)"one_iter_“string(one_iter)“two_iter"string(two_iter)”.csv”, df, header= vcat(Measure_names, "Measure_Time") ,decimal = ‘.’)
BoundsError: attempt to access 1-element Vector{StepRange{Time, Minute}} at index [1:96]
Stacktrace:
 [1] throw_boundserror(A::Vector{StepRange{Time, Minute}}, I::Tuple{UnitRange{Int64}})
   @ Base .\abstractarray.jl:703
 [2] checkbounds
   @ .\abstractarray.jl:668 [inlined]
 [3] getindex
   @ .\array.jl:930 [inlined]

Sorry, my typo, try:

df.Measure_Time = (Time(0):Minute(15):Time(23, 45))[1:nrow(df)]

Im Sorry. Another error appeared

TypeError: in keyword argument decimal, expected Char, got a value of type String
Stacktrace:
 [1] write(file::String, itr::DataFrame; append::Bool, compress::Bool, writeheader::Nothing, partition::Bool, kw::Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol}, NamedTuple{(:header, :decimal), Tuple{Vector{String}, String}}})
   @ CSV C:\Users\mati_\.julia\packages\CSV\b8ebJ\src\write.jl:174
 [2] save_DSSresults(myHC::HC)

What is the statement producing this error?

C

SV.write(pathname*"\DB_MachineLearning\my_data"string(i)"one_iter_“string(one_iter)“two_iter"string(two_iter)”.csv”, df, header= vcat(Measure_names, "Measure_Time") ,decimal = ‘.’)
ERROR: TypeError: in keyword argument decimal, expected Char, got a value of type String
Stacktrace:
 [1] write(file::String, itr::DataFrame; append::Bool, compress::Bool, writeheader::Nothing, partition::Bool, kw::Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol}, NamedTuple{(:header, :decimal), Tuple{Vector{String}, String}}})

You should run this statement as is in your post, and not ,decimal = ".") as you probably actually are (I think).

You were correct! Thank you so much for all your help

1 Like