iT邦幫忙

2024 iThome 鐵人賽

DAY 13
0
Python

Python和R入門語法比較系列 第 13

06 日期 in Python and R [16th 鐵人 Day 13]

  • 分享至 

  • xImage
  •  

下載song_rank.csv

Python

import pandas as pd
with open('data/song_rank.csv') as f:
    p = pd.read_csv(f)
p

https://ithelp.ithome.com.tw/upload/images/20240822/20162398cXNyaaj5U3.png

日期欄

p.Date
    0    2021-07-28
    1    2021-07-27
    2    2021-08-06
    3    2021-08-06
    4    2021-07-30
    5    2021-08-01
    6    2021-07-30
    7    2021-07-29
    8    2021-07-20
    9    2021-07-25
    Name: Date, dtype: object

日期欄的資料類型

type(p.Date)
    pandas.core.series.Series

第1筆

p.Date[0]
    '2021-07-28'

什麼型態

d0 = p.Date[0]
type(d0)
    str

轉成日期格式

d0 = pd.to_datetime(d0)
d0
    Timestamp('2021-07-28 00:00:00')
type(d0)
    pandas._libs.tslibs.timestamps.Timestamp

年 月 日

print(type(d0.year))
print(d0.month)
print(d0.day)
    <class 'int'>
    7
    28

減1天

d0-1
ValueError: Cannot add integral value to Timestamp without freq.
d0 - pd.to_timedelta(1, 'day')
    Timestamp('2021-07-27 00:00:00')

加5天

d0 + pd.to_timedelta(5, 'day')
    Timestamp('2021-08-02 00:00:00')

與距今幾天

今天幾號

pd.Timestamp.now()
    Timestamp('2024-08-06 17:30:23.815539')

相減

now = pd.Timestamp.now()
print(now-d0)
    1105 days 17:30:44.858765

相減後是 什麼型態

type(now-d0)
    pandas._libs.tslibs.timedeltas.Timedelta

天數

(now-d0).days
    int

天數是什麼型態

type((now-d0).days)
    int

與特定日期相減

d0 = p.Date[0]
d0
    '2021-07-28'
d0 = pd.to_datetime(d0)
pd.to_datetime('2021-07-29')-d0
    Timedelta('1 days 00:00:00')

製作日期格式

類型一(可以直接丟進pd.to_datetime( )裡 )

a = '20210729'
b = '2021.7.29'
c = '2021/07/29'
d = '29/7/2021'
e = '29th July 2021'
f = '29th of July, 2021'
pd.to_datetime(d)
    Timestamp('2021-07-29 00:00:00')

類型二(要另外處理)

a = '2021年7月29日'
b = '110年7月29日'
c = '1100729'
d = '110.7.29'
e = '110.07.29'
f = '110/7/29'

R

# 06 日期

setwd('/Users/carplee/Desktop/untitled folder')
r = read.csv('data/song_rank.csv')

#### 日期欄 ####
r$Date
#日期欄的資料類型
class(r$Date)
#第1筆
r$Date[1]
#什麼型態
class(r$Date[1])
#factor 在python是string

#### 轉成日期格式 ####
d1 = as.Date(r$Date[1])
class(d1)

#年 月 日
class(format(d1, '%Y')) 
#character python是int
format(d1,'%m')
format(d1,'%d')

#減1天
d1-1
#加5天
d1+5

#### 距今幾天 ####
#今天幾號 
Sys.Date()
#相減
now = Sys.Date()
now-d1
#Time difference of 1105 days

class(now-d1)
#difftime python是Timedelta

#可以直接把difftime轉成integer
as.integer(now-d1)

#### 與特定日期相減 ####
d1 = as.Date(r$Date[1])
as.Date('2021-07-29')-d1

#### 製作日期格式 ####
# 類型一
a = '20210729'
b = '2021.7.29'
c = '2021/07/29'
d = '29/7/2021'
e = '29th July 2021'
f = '29th of July, 2021'
as.Date(c)
#只有c可以直接丟進 as.Date()
as.Date(a, format='%Y%m%d')
as.Date(b, format='%Y.%m.%d')
as.Date(c, format='%Y/%m/%d')
as.Date(d, format='%d/%m/%y')
as.Date(e, format='%dth %b %Y') # %b 是指 July
as.Date(f, format='%dth of %b, %Y')
help(strptime)

# 類型二
a = '2021年7月29日'
b = '110年7月29日'
c = '1100729'
d = '110.7.29'
e = '110.07.29'
f = '110/7/29'
#以上通通不能 直接丟 as.Date()
as.Date(a, format='%Y年%m月%d日')

as.Date(b, format='%Y年%m月%d日')
as.Date(c, format='%Y%m%d')
as.Date(d, format='%y.%m.%d')
as.Date(e, format='%Y.%m.%d')
as.Date(f, format='%Y/%m/%d')
#b c d e f 要另外處理
library(lubridate)
years(110)+years(1911)

內容預告:

07 Python 和 R 的 字串處理

08 [R] 用Regular Expression(正規表示法)處理文字

08 [python] 用Regular Expression(正規表示法)處理文字

09 [python] 表格 dataframe.insert插入欄位 和 字串處理 Series.str.split

10 [python] pandas的欄列選擇工具 dataframe.loc[ ]和.iloc[ ]

10 [R] r的dataframe欄列選擇方式

11 [python] 取得欄位位置

11 [R] 取得欄位位置


上一篇
05 Python: Pandas Series 字串資料 v. R:文字向量 [16th 鐵人 Day 12]
系列文
Python和R入門語法比較13
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言