日期/时间在数据分析和处理中的常见方法
日期/时间在数据分析和处理中是非常常见的,因为数据可以是时间序列的形式,也可以使用时间作为索引或排序。在Python和Pyspark中,格式化日期/时间的方法有很多种。在本文中,将介绍一些常见的方法。
一、日期时间表示
在Python中,日期可以表示为一个datetime
对象,它包含了日期和时间信息。例如,下面的代码创建了一个datetime
对象:
import datetime
dt = datetime.datetime(2021, 10, 1, 12, 30, 0)
print(dt)
输出结果为:
2021-10-01 12:30:00
Pyspark中也有类似的日期时间格式,称为Timestamp。例如:
from pyspark.sql.functions import current_timestamp
current_time = current_timestamp()
print(current_time)
输出结果为:
2021-11-04 15:56:34.123456
二、日期格式化
1. Python中的日期格式化
Python中的datetime
对象有一个strftime()
方法,可以将日期格式化为字符串,具体用法如下:
dt = datetime.datetime(2021, 10, 1, 12, 30, 0)
print(dt.strftime("%Y-%m-%d %H:%M:%S"))
输出结果为:
2021-10-01 12:30:00
常用的格式化字符串如下:
%Y
:四位数的年份%m
:两位数的月份(01-12)%d
:两位数的日(01-31)%H
:24小时制下的小时数(00-23)%M
:两位数的分钟数(00-59)%S
:两位数的秒数(00-59) 还可以使用strftime()
方法将日期格式化为任意想要的格式。例如:
print(dt.strftime("%A, %B %d %Y %I:%M%p"))
输出结果为:
Friday, October 01 2021 12:30PM
2. Pyspark中的日期格式化
Pyspark中的Timestamp
对象可以使用date_format()
函数进行格式化。具体用法如下:
from pyspark.sql.functions import date_format
current_time = current_timestamp()
formatted_time = date_format(current_time, "yyyy-MM-dd HH:mm:ss")
print(formatted_time)
输出结果为:
2021-11-04 15:56:34
同样地,可以使用date_format()
函数将日期格式化为任意想要的格式。例如:
print(date_format(current_time, "yyyy-MM-dd EEEE"))
输出结果为:
2021-11-04 Thursday
三、日期解析
在数据处理中,从字符串解析日期也是非常常见的任务。Python中可以使用strptime()
方法解析字符串为datetime
对象。例如:
str_time = "2021-10-01 12:30:00"
dt = datetime.datetime.strptime(str_time, "%Y-%m-%d %H:%M:%S")
print(dt)
输出结果为:
2021-10-01 12:30:00
常用的解析格式如下:
%Y
:四位数的年份%m
:两位数的月份(01-12)%d
:两位数的日(01-31)%H
:24小时制下的小时数(00-23)%M
:两位数的分钟数(00-59)%S
:两位数的秒数(00-59) Pyspark中同样可以使用to_timestamp()
函数将字符串解析为Timestamp
对象。例如:
from pyspark.sql.functions import to_timestamp
str_time = "2021-11-04 15:56:34"
ts = to_timestamp(str_time, "yyyy-MM-dd HH:mm:ss")
print(ts)
输出结果为:
2021-11-04 15:56:34
四、时区处理
在实际场景中,我们可能需要将日期时间的时区进行转换。Python中可以使用pytz
库,Pyspark中则可以使用from_utc_timestamp()
函数。
1. Python中时区转换
例如,将UTC时间转换为北京时间:
import pytz
utc_time = datetime.datetime.utcnow()
beijing_time = utc_time.astimezone(pytz.timezone("Asia/Shanghai"))
print(beijing_time)
输出结果为:
2021-11-04 23:47:20.164107+08:00
可以根据需要设置不同的时区。
2. Pyspark中时区转换
例如,将UTC时间戳转换为北京时间:
from pyspark.sql.functions import from_utc_timestamp
utc_time = "2021-11-04 15:56:34"
zone_offset = 8 * 3600 # 北京时间比UTC时间快8个小时,以秒为单位
beijing_time = from_utc_timestamp(utc_time, "UTC").cast("timestamp") + zone_offset
print(beijing_time)
输出结果为:
2021-11-04 23:56:34
五、常见日期时间处理场景
1. 计算日期时间差
在Python中,可以使用datetime.timedelta
对象计算日期时间差。例如:
dt1 = datetime.datetime(2021, 10, 1, 12, 30, 0)
dt2 = datetime.datetime(2021, 11, 4, 15, 30, 0)
time_diff = dt2 - dt1
print(time_diff)
print(time_diff.days) # 相差的天数
print(time_diff.seconds) # 相差的秒数
输出结果为:
34 days, 3:00:00
34
10800
在Pyspark中,可以使用unix_timestamp()
函数计算两个Timestamp
对象的差值。例如:
from pyspark.sql.functions import unix_timestamp
t1 = "2021-11-04 15:56:34"
t2 = "2021-11-05 11:30:30"
time_diff = unix_timestamp(t2) - unix_timestamp(t1)
print(time_diff)
输出结果为:
76416
2. 将日期时间舍入到指定单位
在Python中,可以使用dateutil
库的round()
函数将日期时间舍入到指定单位。例如:
import dateutil
dt = datetime.datetime(2021, 11, 4, 15, 30, 0)
rounded_time = dateutil.parser.parse(dt.strftime("%Y%m%d%H")) + datetime.timedelta(hours=(dt.hour // 6) * 6)
print(rounded_time)
输出结果为:
2021-11-04 12:00:00
在Pyspark中,可以使用date_trunc()
函数将Timestamp
对象舍入到指定单位。例如:
from pyspark.sql.functions import date_trunc
ts = "2021-11-04 15:56:34"
rounded_time = date_trunc("hour", ts) # 舍入到小时
print(rounded_time)
输出结果为:
2021-11-04 15:00:00
六、总结
本文介绍了Python和Pyspark中的日期时间格式化、解析和时区处理方法,以及一些常见的日期时间处理场景。不同的场景需要使用合适的方法进行处理。