1. 提取日期
有時(shí)候我們只需要從日期中提取出年、月、日等信息,以便更好地進(jìn)行數(shù)據(jù)分析和可視化??梢允褂?code>dt屬性實(shí)現(xiàn):
# 創(chuàng)建一個(gè)數(shù)據(jù)集
df = pd.DataFrame({'date': ['2019-05-01 10:00:00', '2020-07-12 12:00:00', '2022-08-10 14:00:00'],
'value': [1, 2, 3]})
# 將日期列轉(zhuǎn)化為時(shí)間格式
df['date'] = pd.to_datetime(df['date'])
# 提取年份
df['year'] = df['date'].dt.year
# 提取月份
df['month'] = df['date'].dt.month
# 提取日
df['day'] = df['date'].dt.day
# 輸出數(shù)據(jù)集
print(df)
2. 計(jì)算時(shí)間差
在時(shí)間序列分析中,我們通常需要計(jì)算時(shí)間差,例如兩個(gè)日期之間的天數(shù)、小時(shí)數(shù)等??梢允褂?code>timedelta實(shí)現(xiàn):
# 創(chuàng)建一個(gè)數(shù)據(jù)集
df = pd.DataFrame({'date': ['2019-05-01 10:00:00', '2020-07-12 12:00:00', '2022-08-10 14:00:00'],
'value': [1, 2, 3]})
# 將日期列轉(zhuǎn)化為時(shí)間格式
df['date'] = pd.to_datetime(df['date'])
# 計(jì)算兩個(gè)日期之間的天數(shù)
df['days_diff'] = (df['date'] - df['date'].min()).dt.days
# 計(jì)算兩個(gè)日期之間的月份數(shù)
df['months_diff'] = (df['date'].dt.year - df['date'].min().year) * 12 + (df['date'].dt.month - df['date'].min().month)
# 計(jì)算兩個(gè)日期之間的年份數(shù)
df['years_diff'] = (df['date'].dt.year - df['date'].min().year)
# 輸出數(shù)據(jù)集
print(df)
3. 將日期列設(shè)為索引
在時(shí)間序列分析中,我們通常需要將日期列設(shè)為索引,以便更好地進(jìn)行數(shù)據(jù)分析和可視化??梢允褂?code>set_index()函數(shù)實(shí)現(xiàn):
# 創(chuàng)建一個(gè)數(shù)據(jù)集
df = pd.DataFrame({'date': ['2019-05-01 10:00:00', '2020-07-12 12:00:00', '2022-08-10 14:00:00'],
'value': [1, 2, 3]})
# 將日期列轉(zhuǎn)化為時(shí)間格式
df['date'] = pd.to_datetime(df['date'])
# 將日期列設(shè)為索引
df = df.set_index('date')
# 輸出數(shù)據(jù)集
print(df)
4. dataframe: 日期格式轉(zhuǎn)字符串
可以使用strftime()
函數(shù)將日期格式的列轉(zhuǎn)為字符串。
在strftime()
函數(shù)中,%Y
表示四位數(shù)的年份,%m
表示兩位數(shù)的月份,%d
表示兩位數(shù)的日期。可以根據(jù)需要進(jìn)行調(diào)整。
下面是一個(gè)使用示例:
#如何使用strftime
# 創(chuàng)建一個(gè)包含日期的dataframe
df = pd.DataFrame({'date': ['2021-10-01', '2021-10-02', '2021-10-03']})
# 將日期列轉(zhuǎn)換為日期格式
df['date'] = pd.to_datetime(df['date'])
# 將日期格式的列轉(zhuǎn)為字符串
df['date'] = df['date'].dt.strftime('%Y-%m-%d')
# 輸出dataframe
print(df)
5. 字符串轉(zhuǎn)日期格式
5.1 dataframe:字符串轉(zhuǎn)日期格式 - pd.to_datetime函數(shù)
pd.to_datetime()
函數(shù)是pandas中用于將字符串或者數(shù)字轉(zhuǎn)化為時(shí)間格式的函數(shù)。該函數(shù)通常用于將數(shù)據(jù)集中的時(shí)間列轉(zhuǎn)化為pandas能夠識(shí)別的時(shí)間格式,以便更好地進(jìn)行數(shù)據(jù)分析和時(shí)間序列分析。
下面是使用pd.to_datetime()
函數(shù)的一個(gè)例子:
import pandas as pd
# 創(chuàng)建一個(gè)包含日期文本的dataframe
df = pd.DataFrame({'date': ['2021-10-01', '2021-10-02', '2021-10-03']})
# 將日期列轉(zhuǎn)換為日期格式
df['date'] = pd.to_datetime(df['date'])
# 輸出dataframe
print(df)
5.2 dataframe:字符串轉(zhuǎn)日期格式 - strptime
除了pd.to_datetime()
函數(shù),還可以使用Python中的datetime
模塊中的datetime.strptime()
函數(shù)將字符串轉(zhuǎn)為日期格式。
下面是一個(gè)使用datetime.strptime()
函數(shù)的例子:
import pandas as pd
from datetime import datetime
# 創(chuàng)建一個(gè)包含日期文本的dataframe
df = pd.DataFrame({'date_str': ['2021-10-01', '2021-10-02', '2021-10-03']})
# 將日期列轉(zhuǎn)換為日期格式
df['date'] = df['date_str'].apply(lambda x: datetime.strptime(x, '%Y-%m-%d'))
# 輸出dataframe
print(df)
在datetime.strptime()函數(shù)中,第一個(gè)參數(shù)是要轉(zhuǎn)換的字符串,第二個(gè)參數(shù)是字符串的格式。例如,%Y表示四位數(shù)的年份,%m表示兩位數(shù)的月份,%d表示兩位數(shù)的日期??梢愿鶕?jù)需要進(jìn)行調(diào)整。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-414281.html
pd.to_datetime()函數(shù)在處理日期時(shí)更加靈活和方便,因此一般情況下建議使用pd.to_datetime()函數(shù)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-414281.html
到了這里,關(guān)于python: 處理表格日期的常用場(chǎng)景和方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!