0

I have an incoming data set where the time periods for data is in quarters i.e 4 quarters in a year. It is represented as as string ['2016:01:00', '2016:02:00','2016:03:00', 2016:04:00'] representing data for each quarter. How can I convert this to datetime format? My expected output for example will be 2016:01:00 for the first quarter of 2016. Thanks in advance

1 Answer 1

1

Fetch the year and the quarter from your string and convert the quarter number into a calendar month.

from datetime import datetime


def convert_to_date(value):
    year, quarter, _ = value.split(":")
    return datetime(int(year), 3 * int(quarter), 1)


values = ["2016:01:00", "2016:02:00", "2016:03:00", "2016:04:00"]

list(map(convert_to_date,  values))
Sign up to request clarification or add additional context in comments.

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.