首頁 > 軟體

Pandas時間型別轉換與處理的實現範例

2022-07-29 14:00:25

在平時的需求開發中,經常涉及到利用Pandas處理日期相關型別欄位的轉換和操作,為此特地記錄以下練習案例,幫助大家的同時,也便於日後的學習和覆盤

案例1

問題: 提取'W1|2022/7/28'欄位中的年月日資訊,取名為week_start,即一週開始的日期,並根據week_start計算出該周結束的具體日期week_end

import pandas as pd
import datetime
df1 = pd.DataFrame([[6,3],[6,3]], columns = ['Working day','W1|2022/7/28'])
# 一週開始的日期
# '2022/7/28'——>str型別
week_start = df1.columns[1].split('|')[1]
# 將start_day型別轉換成date型別(2022-07-28)
week_start = datetime.datetime.strptime(week_start, '%Y/%m/%d').date()
# 一週結束的日期(2022-08-03)
week_end = week_start + datetime.timedelta(days=6)

df1

案例2

問題: 根據'Date'欄位生成'Date - 2'欄位

import pandas as pd
from datetime import timedelta
from datetime import datetime
 
df2 = pd.DataFrame([[1,'20191031'],
                   [2,'20191106'],
                   [3,'20191106']],columns=['Id','Date'])
# 'Date'欄位中的值減去2天,生成'Date - 2'欄位
df2['Date - 2'] = df2['Date'].apply(lambda x:(datetime.strptime(x,'%Y%m%d') - timedelta(days=datetime.strptime(x,'%Y%m%d').weekday())).strftime("%Y%m%d"))

df2

案例3

問題:從字串表示的日期時間中僅獲取“年/月/日” 

import pandas as pd
from datetime import datetime
 
df3 = pd.DataFrame([[1,'2017-01-02 00:00:00'],
                   [2,'2017-01-09 00:00:00']
                   ],columns = ['Id','Wk'])

df3

錯誤寫法

# 執行以下程式碼會報錯'str' object has no attribute 'strftime'
df3['new_wk'] = df3['Wk'].apply(lambda x:x.strftime("%Y%m%d"))

正確寫法

# 先利用.strptime()將str格式的變數轉化成datetime下的時間格式
# 然後再利用.strftime()獲取「年/月/日」
df3['Wk'] = df3['Wk'].apply(lambda x:datetime.strptime(x,"%Y-%m-%d %H:%M:%S"))
df3['new_Wk'] = df3['Wk'].apply(lambda x:x.strftime("%Y/%m/%d"))

處理過後的df3

案例4

問題:將'月/日/年 時間'格式的值轉換為'年月日'(10/11/19 05:28:27 => 20191011)

import pandas as pd
 
df4 = pd.DataFrame([['A','10/11/19 05:28:27','08/04/20 08:38:59'],
                   ['B','10/11/19 05:28:27',None],
                   ['C','10/11/19 05:28:27',None]
                  ],columns = ['site','creation_date','closure_date'])

df4

# 將'creation_date'欄位的值變形
# 10/11/19 05:28:27 => 20191011
df4['creation_date'] = df4['creation_date'].apply(lambda x:pd.to_datetime(x).strftime("%Y%m%d"))
 
# 將'closure_date'欄位中nan值填充為0
df4['closure_date'] = df4['closure_date'].fillna(0)
# 篩選closure_date'欄位中值為0的資料記錄,取名為df4_na
df4_na = df4[df4['closure_date'].isin([0])]
# 篩選closure_date'欄位中值不為0的資料記錄,取名為df4
df4 = df4[~df4['closure_date'].isin([0])]
 
# 將'closure_date'欄位的值變形
# 08/04/20 08:38:59 => 20200804
df4['closure_date'] = df4['closure_date'].apply(lambda x:pd.to_datetime(x).strftime("%Y%m%d"))
 
df4 = pd.concat([df4, df4_na], ignore_index = True)

 處理過後的df4

補充知識

我們通常使用pd.to_datetime()和s.astype('datetime64[ns]')來做時間型別轉換

import pandas as pd
 
t = pd.Series(['20220720','20220724'])
# dtype: datetime64[ns]
new_t1 = pd.to_datetime(t)
new_t2 = t.astype('datetime64[ns]')

t

new_t1

new_t2 

案例5

問題: 新增欄位'Week',逐行遞增

import pandas as pd
 
df5 = pd.DataFrame(columns=['Week','Materials'])
all_material = ['A32456','B78495']
 
for row in range(0,3):
    week = row + 1
    datas = [week, all_material]
    df5.loc[row] = datas
'''
df5:
 
  Week         Materials
0    1  [A32456, B78495]
1    2  [A32456, B78495]
2    3  [A32456, B78495]
'''
print(df5)

案例6

問題:日期型轉換為字元型

import datetime
today = datetime.date.today() # date型別 2022-07-28
today.strftime('%Y-%m-%d') # '2022-07-28'
import datetime
dt = datetime.datetime.now() # datetime型別 2022-07-28 22:46:20.528813
dt.strftime('%Y-%m-%d') # '2022-07-28'
import datetime
today = str(datetime.date.today()) # str型別 2022-07-28
today.replace("-","") # '20220728'

案例7

問題:文字型轉日期型

#文字型日期轉為日期型日期
import pandas as pd
from datetime import datetime
df7=pd.DataFrame({'銷售日期':['2022-05-01','2022-05-02','2022-05-03','2022-05-04','2022-05-05','2022-05-06','2022-05-07','2022-05-08','2022-05-09','2022-05-10'],
                '城市':['蘭州','白銀','天水','武威','金昌','隴南','嘉峪關','酒泉','敦煌','甘南']})

df7

文字型轉為日期型可用datetime.strptime函數 

# "%Y-%m-%d"表示將文字日期解析為年月日的日期格式
df7['日期'] = df7['銷售日期'].map(lambda x:datetime.strptime(x,"%Y-%m-%d"))

文字型轉為日期型也可用pd.to_datetime函數

# "%Y-%m-%d"表示將文字日期解析為年月日的日期格式
df7['日期'] = pd.to_datetime(df7['銷售日期'],format='%Y-%m-%d')

處理過後的df7

案例8

問題:提取日期欄位的年份、月份、日份和週數

import pandas as pd
from datetime import datetime
df8=pd.DataFrame({'銷售日期':['2022-05-01','2022-05-02','2022-05-03','2022-05-04','2022-05-05','2022-05-06','2022-05-07','2022-05-08','2022-05-09','2022-05-10'],
                '城市':['蘭州','白銀','天水','武威','金昌','隴南','嘉峪關','酒泉','敦煌','甘南']})
 
df8['日期'] = df8['銷售日期'].map(lambda x:datetime.strptime(x,"%Y-%m-%d"))

df8 

#由日期資料提取年
df8['年份'] = df8['日期'].apply(lambda x: x.year)
df8['年份'] =df8['年份'].astype(str)+'年'
 
#由日期資料提取月
df8['月份'] = df8['日期'].apply(lambda x: x.month)
df8['月份'] =df8['月份'].astype(str)+'月'
 
#由日期資料提取日
df8['日份'] = df8['日期'].apply(lambda x: x.day)
df8['日份'] =df8['日份'].astype(str)+'日'
 
# 日期中的周使用date.isocalendar()[1]提取
#根據日期返回週數,以週一為第一天開始
df8['週數'] = [date.isocalendar()[1] for date in df8['日期'].tolist()]
df8['週數'] = df8['週數'].astype(str)+'周'

處理後的df8

案例9

問題:藉助offset時間偏移函數將日期加3天 

import pandas as pd
from datetime import datetime
df9=pd.DataFrame({'銷售日期':['2022-05-01','2022-05-02','2022-05-03','2022-05-04','2022-05-05','2022-05-06','2022-05-07','2022-05-08','2022-05-09','2022-05-10'],
                '城市':['蘭州','白銀','天水','武威','金昌','隴南','嘉峪關','酒泉','敦煌','甘南']})
 
df9['日期'] = df9['銷售日期'].map(lambda x:datetime.strptime(x,"%Y-%m-%d"))

df9

#藉助offset時間偏移函數將日期加3天
from pandas.tseries.offsets import Day
df9['日期_3']=df9['日期']+Day(3)

處理後的df9

案例10

問題:將文字型日期轉換為日期型日期

#文字型日期轉為日期型日期
import pandas as pd
import datetime as dt
from datetime import datetime
df1=pd.DataFrame({'銷售時間':['2022-05-01 00:00:00','2022-05-02 00:00:00','2022-05-03 00:00:00','2022-05-04 00:00:00','2022-05-05 00:00:00',
                         '2022-05-06 00:00:00','2022-05-07 00:00:00','2022-05-08 00:00:00','2022-05-09 00:00:00','2022-05-10 00:00:00',]})
#df['日期']=df['銷售日期'].map(lambda x:datetime.strptime(x,"%Y-%m-%d"))
df1['日期_x']=df1['銷售時間'].str.split(' ',expand=True)[0]
df1['日期_y']=pd.to_datetime(df1['銷售時間'],format='%Y-%m-%d')
df1

df10

日期中帶有時分秒'00:00:00',有如下方法將其處理為'%Y-%m-%d'形式

df10['日期']=df10['銷售時間'].str.split(' ',expand=True)[0]

處理後的df10

到此這篇關於Pandas時間型別轉換與處理的實現範例的文章就介紹到這了,更多相關Pandas時間型別轉換內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com