Python|使用 Pandas 处理日期和时间
在处理数据时,遇到时间序列数据是很常见的。在处理时间序列数据时,Pandas 是一个非常有用的工具。
Pandas 提供了一组不同的工具,我们可以使用它们对日期时间数据执行所有必要的任务。让我们尝试通过下面讨论的示例来理解。
代码 #1:创建日期数据框
Python3
import pandas as pd
# Create dates dataframe with frequency
data = pd.date_range('1/1/2011', periods = 10, freq ='H')
data
Python3
# Create date and time with dataframe
data = pd.date_range('1/1/2011', periods = 10, freq ='H')
x = pd.datetime.now()
x.month, x.year
Python3
# Create date and time with dataframe
rng = pd.DataFrame()
rng['date'] = pd.date_range('1/1/2011', periods = 72, freq ='H')
# Print the dates in dd-mm-yy format
rng[:5]
# Create features for year, month, day, hour, and minute
rng['year'] = rng['date'].dt.year
rng['month'] = rng['date'].dt.month
rng['day'] = rng['date'].dt.day
rng['hour'] = rng['date'].dt.hour
rng['minute'] = rng['date'].dt.minute
# Print the dates divided into features
rng.head(3)
Python3
# Input present datetime using Timestamp
t = pandas.tslib.Timestamp.now()
t
Python3
# Convert timestamp to datetime
t.to_datetime()
Python3
# Directly access and print the features
t.year
t.month
t.day
t.hour
t.minute
t.second
Python3
import pandas as pd
url = 'http://bit.ly/uforeports'
# read csv file
df = pd.read_csv(url)
df.head()
Python3
# Convert the Time column to datetime format
df['Time'] = pd.to_datetime(df.Time)
df.head()
Python3
# shows the type of each column data
df.dtypes
Python3
# Get hour detail from time data
df.Time.dt.hour.head()
Python3
# Get name of each date
df.Time.dt.weekday_name.head()
Python3
# Get ordinal day of the year
df.Time.dt.dayofyear.head()
输出:
代码 #2:创建日期范围并显示基本特征
Python3
# Create date and time with dataframe
data = pd.date_range('1/1/2011', periods = 10, freq ='H')
x = pd.datetime.now()
x.month, x.year
输出:
(9, 2018)
日期时间特征可以分为两类。第一个时间段内的时刻,第二个时间段自特定时间段以来经过的时间。这些功能对于理解数据中的模式非常有用。
将给定日期划分为特征 -
pandas.Series.dt.year返回日期时间的年份。
pandas.Series.dt.month返回日期时间的月份。
pandas.Series.dt.day返回日期时间的日期。
pandas.Series.dt.hour返回日期时间的小时。
pandas.Series.dt.minute返回日期时间的分钟。
从这里参考所有日期时间属性。
代码 #3:将日期和时间分解为单独的功能
Python3
# Create date and time with dataframe
rng = pd.DataFrame()
rng['date'] = pd.date_range('1/1/2011', periods = 72, freq ='H')
# Print the dates in dd-mm-yy format
rng[:5]
# Create features for year, month, day, hour, and minute
rng['year'] = rng['date'].dt.year
rng['month'] = rng['date'].dt.month
rng['day'] = rng['date'].dt.day
rng['hour'] = rng['date'].dt.hour
rng['minute'] = rng['date'].dt.minute
# Print the dates divided into features
rng.head(3)
输出:
代码#4:要获取当前时间,请使用 Timestamp.now() ,然后将时间戳转换为日期时间并直接访问年、月或日。
Python3
# Input present datetime using Timestamp
t = pandas.tslib.Timestamp.now()
t
Timestamp('2018-09-18 17:18:49.101496')
Python3
# Convert timestamp to datetime
t.to_datetime()
datetime.datetime(2018, 9, 18, 17, 18, 49, 101496)
Python3
# Directly access and print the features
t.year
t.month
t.day
t.hour
t.minute
t.second
2018
8
25
15
53
让我们在一个真实的数据集 uforeports 上分析这个问题。
Python3
import pandas as pd
url = 'http://bit.ly/uforeports'
# read csv file
df = pd.read_csv(url)
df.head()
输出:
Python3
# Convert the Time column to datetime format
df['Time'] = pd.to_datetime(df.Time)
df.head()
Python3
# shows the type of each column data
df.dtypes
City object
Colors Reported object
Shape Reported object
State object
Time datetime64[ns]
dtype: object
Python3
# Get hour detail from time data
df.Time.dt.hour.head()
0 22
1 20
2 14
3 13
4 19
Name: Time, dtype: int64
Python3
# Get name of each date
df.Time.dt.weekday_name.head()
0 Sunday
1 Monday
2 Sunday
3 Monday
4 Tuesday
Name: Time, dtype: object
Python3
# Get ordinal day of the year
df.Time.dt.dayofyear.head()
0 152
1 181
2 46
3 152
4 108
Name: Time, dtype: int64