格式转换不适用于根据月份计算会计年度

时间:2018-09-04 17:47:48

标签: python pandas

我试图根据我的月份计算会计年度。转换无效。目前,我的时间戳是object类型。我已将其转换为int以获取无法使用的必要值。

import pandas as pd
upload_raw['Month_']= upload_raw['CREAT_TS'].str[:10]
upload_raw['Year_']= upload_raw['Month_'].str[:4].astype(int)

upload_raw['Month_']=pd.DatetimeIndex(upload_raw['Month_']).month.astype(int)
def year_conv():
    if upload_raw['Month_'] > 6: 
        upload_raw['Year_']+1
    else:
        upload_raw['Year_1']

我仍然得到与年份相同的值。

这是我转换日期的格式。

CREAT_TS
2018-06-22-06.48.49.601000 

完整代码:

import pandas as pd
import numpy as np
import datetime
from datetime import date
from datetime import datetime
from dateutil.relativedelta import relativedelta
import pyodbc
import calendar

#loading Agency Notices Upload Raw Data
upload_raw = pd.read_excel(r'C:\Users\Desktop\Upload Raw Data.xlsx',
                     sheet_name = 'Upload',
                     header = 0
                     )
display(upload_raw)
upload_raw.dtypes
datatype =upload_raw.dtypes
display(datatype)

# creating Month and Year column
upload_raw['Month_']= upload_raw['CREAT_TS'].str[:10]
upload_raw['Year_']=upload_raw['Month_'].str[2:4].astype(int)
upload_raw['Month_']=pd.DatetimeIndex(upload_raw['Month_']).month.astype(int)

def year_conv():
    if upload_raw['Month_'] > 6: 
        upload_raw['Year_']+1
    else:
        upload_raw['Year_1']



upload_raw['Month_']=upload_raw['Month_'].apply(lambda x: calendar.month_abbr[x])


# loading Branch Mapping Details
mapping = pd.read_excel(r'C:\Users\Desktop\Mapping.xlsx',
                     sheet_name = 'Mapping',
                     header = 0
                     )

upload_lookup= pd.merge(left = upload_raw, right= mapping,on='BRANCH')
display(upload_lookup)

这是上传文件中的示例数据

BRANCH  CUE CREAT_TS                     RAF_IND
AA      &CR 2018-06-22-06.48.49.601000  
AA      &CR 2018-06-22-11.43.29.859000  
AA      &CR 2018-06-22-11.54.52.633000  
AA      EZZ 2018-06-22-11.05.13.371000  

我想从CREAT_TS获取月份和年份。如果month > 6,则year应该是year+1,否则应该是存在的year

关于, 仁。

1 个答案:

答案 0 :(得分:1)

您可以使用np.where()完成所需的操作

根据您的示例,我创建了一个简化的数据框进行演示。请注意,我将最后一个月更改为7,因此我们有一个示例,其中您的条件评估为True。

df
Out[74]: 
   Month_  Year_
0       6     18
1       6     18
2       6     18
3       7     18

为避免混淆,我将新变量保存在“ Years_”中,以便您可以看到更改。

df['Years'] = np.where(df['Month_'] > 6, df['Year_'] + 1, df['Year_'])

df
Out[79]: 
   Month_  Year_  Years
0       6     18     18
1       6     18     18
2       6     18     18
3       7     18     19