Try   HackMD

第十六天 時間與日期

LHB阿好伯, 2020/02/09

tags: Python_30 R & python

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

之前有整理過一篇R語言_時間資料處理今天就來討論看看R跟python的差異
在資料處理之中,時常會需要遇到時間格式的處理
實驗數據、股市資料、氣候資訊隨著時間不斷的流逝
各式資料也不斷的生成的情況下時間的判讀就顯得重要

取得時間資訊

在R中內建許多函數可以取得時間資訊

# R Code Sys.Date() #當前日期 lubridate::today() date() #當前系統日期和時間 Sys.time() #當前系統日期和時間 lubridate::now() Sys.timezone() #有關時區的信息將返回當前時區的名稱

[1] "2020-12-25"
[1] "Fri Dec 25 23:01:32 2020"
[1] "2020-12-25 23:01:33 CST"
[1] "Asia/Taipei"

而Python則是使用 datetime套件

# Python code from datetime import datetime now = datetime.now() #現在詳細日期與時間 print(now) day = now.day #現在日期 print(day) month = now.month print(month) #現在月份 year = now.year print(year) #現在年份 hour = now.hour print(hour) #現在小時數 minute = now.minute print(minute) #現在分鐘數 second = now.second print(second) #現在現在秒數 timestamp = now.timestamp() #距離1970年過去幾秒 print(timestamp)

2020-12-25 23:06:07.196302
25
12
2020
23
6
7
1608908767.196302

date日期函數

date(year, month, day)
max = datetime.date(9999, 12, 31)
min = datetime.date(1, 1, 1)

# Python code from datetime import date d = date(2020, 1, 1) print(d) print('Current date:', d.today()) today = date.today() print("Current year:", today.year) print("Current month:", today.month) print("Current day:", today.day)

Current date: 2020-12-27
Current year: 2020
Current month: 12
Current day: 27

time 時間函數

time([hour], [minute], [second],[microsecond])
參數皆可選
max = datetime.time(23, 59, 59, 999999)
min = datetime.time(0, 0)

# Python code from datetime import time a = time() print("a =", a) b = time(10, 30, 50) print("b =", b) c = time(hour=10, minute=30, second=50) print("c =", c) d = time(10, 30, 50, 200555) print("d =", d)

a = 00:00:00
b = 10:30:50
c = 10:30:50
c = 10:30:50
d = 10:30:50.200555

strftime時間格式

date,datetime和time的物件都支持一種strftime(format)格式
該可以在格式字串的控制下創建表示時間的字串
下表為可以使用的格式

格式 含義 Example
%a 工作日名稱的縮寫 Sun, Mon,
%A 工作日全名 Sunday, Monday,
%w 工作日為十進制數字 0, 1, , 6
%d 月份中的一天,以零填充的十進制數表示 01, 02, , 31
%-d 以十進制數表示的月份中的一天 1, 2, , 30
%b 月份的縮寫 Jan, Feb, , Dec
%B 完整的月份名稱 January, February,
%m 以零填充的十進制數字表示的月份 01, 02, , 12
%-m 以十進制數表示的月份 1, 2, , 12
%y 無世紀的年份,為零填充的十進制數字 00, 01, , 99
%-y 沒有世紀的年份作為十進制數字 0, 1, , 99
%Y 以世紀作為十進制數字的年份 2013, 2019 etc.
%H 小時(24小時制),為零填充的十進制數字 00, 01, , 23
%-H 小時(24小時制)為十進制數字 0, 1, , 23
%I 小時(12小時制),為零填充的十進制數字 01, 02, , 12
%-I 小時(12小時制)為十進制數字 1, 2, 12
%p 語言環境的上午或下午 AM, PM
%M 分鐘,為零填充的十進制數字 00, 01, , 59
%-M 以十進制數字表示 0, 1, , 59
%S 第二個為零填充的十進制數 00, 01, , 59
%-S 第二個十進制數字 0, 1, , 59
%f 微秒,十進制數,在左側補零 000000 - 999999
%z UTC偏移量,格式為+ HHMM或-HHMM
%Z 時區名稱
%j 一年中的一天,以零填充的十進制數表示 001, 002, , 366
%-j 一年中的天,以十進制數字表示 1, 2, , 366
%U 一年中的第幾週(星期日為一周的第一天)新年中第一個星期日之前的所有天均視為第0週 00, 01, , 53
%W 一年中的第幾週(星期一為一周中的第一天)第一個星期一之前的新的一年中的所有天均視為在第0週 00, 01, , 53
%c 語言環境的適當日期和時間表示 Mon Sep 30 07:06:05 2013
%x 語言環境的適當日期表示形式 09/30/13
%X 語言環境的適當時間表示形式 07:06:05
%% 文字'%'字符 %

strftime 格式化日期輸出

# Python code now = datetime.now() t = now.strftime("%H:%M:%S") print("time:", t) time_one = now.strftime("%m/%d/%Y, %H:%M:%S") # mm/dd/YY H:M:S format print("time one:", time_one)

time: 21:56:34
time one: 12/27/2020, 21:56:34

strptime 字串轉換為時間格式

# Python code from datetime import datetime date_string = "27 12, 2019" print("date_string =", date_string) date_object = datetime.strptime(date_string, "%d %m, %Y") print("date_object =", date_object)

timedelata 計算日期差

timedelta()函數存在於datetime套件中
該函數常用於計算日期差
也可以用於Python中的日期操作
這是執行日期操作的方法之一

datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

# Python code from datetime import timedelta t1 = timedelta(weeks=12, days=10, hours=4, seconds=20) t2 = timedelta(days=7, hours=5, minutes=3, seconds=30) t3 = t1 - t2 print("t3 =", t3)

t3 = 86 days, 22:56:50

後面發現其實使用date函數也是可以直接計算

# Python code from datetime import date t1 = date(2021, 1, 7) t2 = date(2020,12,28) t3 = t1 - t2 print("t3 =", t3)

t3 = 10 days, 0:00:00

Pythone關於時間的套件就簡單介紹到這裡
若是有機會可以嘗試看看使用R的lubridate套件
超級好用大推沒意外未來一段時間若是遇到時間資料
我應該都會使用R來處理

其他相關套件

Arrow:Python更好的日期與時間處理套件

🌟

🌟全文可以至下方連結觀看或是補充

參考資料

datetime—基本日期和時間類型

全文分享至

https://www.facebook.com/LHB0222/

https://www.instagram.com/ahb0222/

有疑問想討論的都歡迎於下方留言

喜歡的幫我分享給所有的朋友 \o/

有所錯誤歡迎指教

全部文章列表