如何修正熊猫每季度收入的日期比较

时间:2019-09-06 07:57:25

标签: python pandas

我正在编写一个工具,该工具将获取一个csv,其中包括该时间段的日期和收入,并且必须将其拆分为各个季度,并相应地拆分收入并将其添加到新列中。

这次,我正在使用熊猫和DataFrame。我尝试使用无法控制的纯csv列表。

def runCalculation(someindex, df):
    index = someindex
    startdate = df['Start Date'][index]
    enddate = df['End Date'][index]
    deal_days = (enddate - startdate).days
    rest_days = deal_days
    while rest_days > 0:
        currentQuarterEndDate = getCurrentQuarterEndDate(startdate)
        startquarter = startdate.quarter
        startyear = startdate.year
        if currentQuarterEndDate <= enddate:
            # verrechne aktuelles Quartal
            prized_days = (currentQuarterEndDate - startdate).days
            print(f'Prized: {prized_days}')
            rest_days = rest_days - prized_days
            revenue_percent = 100 / deal_days * prized_days
            print(f'Percent: {revenue_percent}%')
            revenue = round(getIndexedRevenueFloat(index, df) / 100 * revenue_percent, 2)
            # Round up or down to 0.05 CHF
            revenue = roundCHF(revenue)
            print(revenue)
            appendQuarterRevenue(startdate, revenue)
        else:
            # verrechne restliche Tage
            rest_days = (enddate - startdate).days
            prized_days = rest_days
            print(f'Prized: {prized_days}')
            rest_days = 0
            revenue_percent = 100 / deal_days * prized_days
            print(f'Percent: {revenue_percent}%')
            revenue = round(getIndexedRevenueFloat(index, df) / 100 * revenue_percent, 2)
            # Round up or down to 0.05 CHF
            revenue = roundCHF(revenue)
            print(revenue)
            appendQuarterRevenue(startdate, revenue)

        print(f'Rest: {rest_days}')
        startdate = startdate + pd.tseries.offsets.DateOffset(prized_days)

    if index == len(df) - 1:
        print(f'Last entry reached! Rows proccessed: {index+1}')
    else:
        index += 1
        runCalculation(index, df)
def appendQuarterRevenue(startdate, revenue):
    if revenue < 0:
        revenue = 0.0
    for key in q1:
        if (startdate + pd.tseries.offsets.DateOffset(1)).year == int(key[3:]) and (startdate + pd.tseries.offsets.DateOffset(1)).quarter == int(key[1:-5]):
            q1[key].append(f'{revenue:,}')
        else:
            q1[key].append('0.0')
    for key in q2:
        if (startdate + pd.tseries.offsets.DateOffset(1)).year == int(key[3:]) and (startdate + pd.tseries.offsets.DateOffset(1)).quarter == int(key[1:-5]):
            q2[key].append(f'{revenue:,}')
        else:
            q2[key].append('0.0')
    for key in q3:
        if (startdate + pd.tseries.offsets.DateOffset(1)).year == int(key[3:]) and (startdate + pd.tseries.offsets.DateOffset(1)).quarter == int(key[1:-5]):
            q3[key].append(f'{revenue:,}')
        else:
            q3[key].append('0.0')
    for key in q4:
        if (startdate + pd.tseries.offsets.DateOffset(1)).year == int(key[3:]) and (startdate + pd.tseries.offsets.DateOffset(1)).quarter == int(key[1:-5]):
            q4[key].append(f'{revenue:,}')
        else:
            q4[key].append('0.0')

我希望收入的收入基于该季度的日期,但仅会在第一季度产生。之后,一路只有0.0。

0 个答案:

没有答案