0

I want to create a dataframe in Scala with all dates from a start date to an end date.

I can get an Array of dates using this expression

    val dates = sqlContext.sql("SELECT sequence(to_date('2018-01-01'), to_date('2018-02-01'), interval 1 day) as dates")

    +--------------------+
    |               dates|
    +--------------------+
    |[2018-01-01, 2018...|
    +--------------------+

This gives me a dataframe with one column and one row. I rather want one row for each date and not an array with all the dates as I have now

How can I achieve this?

    +--------------------+
    |               dates|
    +--------------------+
    |[2018-01-01|
    |[2018-01-02|
    |[2018-01-03|
    |[2018-01-04|
    etc.....
    +--------------------+

2 Answers 2

1

You can use explode to unwind the array:

SELECT explode(sequence(to_date('2018-01-01'), to_date('2018-02-01'), interval 1 day)) as dates
Sign up to request clarification or add additional context in comments.

Comments

0

You can create a list of dates and then convert it to a dataframe like this.

import org.joda.time.LocalDate

val start = new LocalDate(2019, 9, 24)
val end   = new LocalDate(2019, 9, 28)
val numberOfDays = Days.daysBetween(start, end).getDays
val days = (for (f<- 0 to numberOfDays) 
yield  start.plusDays(f).toDate.toString).toList
import session.implicits._
val df = days.toDF("dates")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.