简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM