TAGS :Viewed: 4 - Published at: a few seconds ago

[ Python Converting Quarterly to Monthly Data ]

    0       1Q08
    1       2Q08
    2       3Q08
    3       4Q08
    4       1Q09
    5       2Q09

Currently My dataframe looks like this. I would love to convert the quaterly to monthly like this:

    0       01-01-08
    1       02-01-08
    2       03-01-08
    3       04-01-08
    4       05-01-08
    5       06-01-08

It requires adding two rows per each quarter and converting a quarter into three months. Is there a built-in function for this? If not, what would be an efficient way? Thanks!

Answer 1


There are probably many ways of doing this. I find split() really useful for reformatting.

i = 0

for d in data_frame:
    quarter, year = d.split("\t")[1].split("Q")
    for j in range(3):
        month = (int(quarter) - 1) * 3 + j + 1
        print "%s\t%s-01-%s" % (i, month, year)
        i += 1

The '%s' line is an example of python's string substitution syntax.