Extractin semester from a datatime column

Viewed 55

I have a code in python to extract year+semester from a datetime column in a dataframe:

df['semester'] = df.date.dt.year.astype(str) + ' Semester=' + np.where(df.date.dt.quarter>2,2,1).astype(str) 

   date
2021-01-01

and the output is:

       semester
   2021 Semester=1

What's the fasted way to extract semester from a datetime column in julia?

2 Answers

Something pretty similar to your Python code:

julia> using Dates
julia> dff = DataFrame(:date => [Date(2021, 8, 1), Date(2021, 1, 1)])
2×1 DataFrame
 Row │ date       
     │ Date       
─────┼────────────
   1 │ 2021-08-01
   2 │ 2021-01-01

julia> dff[!, :semester] = @. string(year(dff.date)) *
                              " Semester=" *
                              ifelse(quarterofyear(dff.date) > 2, "2", "1")
2-element Vector{String}:
 "2021 Semester=2"
 "2021 Semester=1"


Please post runnable code for debugging, I believe you're looking for:

julia> using Dates

julia> d = today()
2021-12-13

julia> Quarter(d)
4 quarters

for getting a column, you can use brodacst, such as Quarter.(df.semester).

Related