excel填充日期

 1 #在pandas中如果一列出现nan,则会认为他是float类型
 2 import  pandas
 3 from  datetime  import  datetime,timedelta
 4 
 5 #关于月份加一的函数
 6 def   add_month(d,md):  
 7     yd=md//12
 8     m=d.month+md%12
 9     if m!=12:
10         yd=yd+m//12
11         m=m%12
12     return datetime(d.year+yd,m,d.day)
13 
14 #header和sikprows作用一样
15 excel=pandas.read_excel('填充数据.xlsx',header=3,usecols='C:F',index_col=None,dtype={'id':str,'store':str,'data':str})
16 start=datetime(2020,2,10)
17 
18 for  i in excel.index:
19     excel.at[i,'id']=i+1
20     excel['store'].at[i]='Yes' if i%2==0 else  'No'
21     #excel['data'].at[i]=start  +  timedelta(days=i)   #加一天,最多只能加到天
22     #excel['data'].at[i]=datetime(start.year+i,start.month,start.day)  #每一年加一
23     excel['data'].at[i]=add_month(start,i)  #对每个月加一
24 excel.set_index('id',inplace=True)
25 print(excel)
26 
27 
28 timedelta函数:
29 >>>from datetime  import  timedelta
30 >>>time1=timedelta(seconds=23,minutes=35,hours=13,days=6)
31 >>>time2=timedelta(seconds=59,minutes=57,hours=18,days=8)
32 >>>time2-time1
33 datetime.timedelta(days=2, seconds=19356)  #返回时间差

 

posted @ 2020-06-07 13:31  misherry  阅读(373)  评论(0编辑  收藏  举报