目录
1. 把时间戳整体 减10min.
from datetime import timedelta
data = pd.read_excel(file, parse_dates=['time'], sheet_name=0)
data['time'] = data['time'].apply(lambda x: x-timedelta(minutes=10))
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
2. 时间特征提取
df['ymdh'] = df['date'].apply(lambda x: x.strftime('%Y%m%d%H'))
df['年'] = df['date'].dt.year
df['季度'] = df['date'].dt.quarter
df['月'] = df['date'].dt.month
df['日'] = df['date'].dt.day
df['h'] = df['date'].dt.hour
df['分'] = df['日期'].dt.minute
df['秒'] = df['日期'].dt.second
df['星期几'] = df['日期'].dt.dayofweek
df['周次'] = df['日期'].dt.week
3. 时间戳四舍五入
data_tower_1S_1H['date'] = data_tower_1S_1H['date'].dt.round('1ms')
4. 时间格式转换
import time
dt = "2016-05-05 20:28:54"
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S") # 转换成时间数组
timestamp = time.mktime(timeArray) # 转换成时间戳
print(timestamp)
1462451334.0
5. 两个日期相差多少天
strptime
,而不是 strftime
d1 = datetime.datetime.strptime('2012-03-05 17:41:20', '%Y-%m-%d %H:%M:%S')
d2 = datetime.datetime.strptime('2012-03-02 17:41:20', '%Y-%m-%d %H:%M:%S')
delta = d1 - d2
print(delta.days)
# 3
import numpy as np
import pandas as pd
from datetime import *
# 输入2个日期
date1 = "2022.05.11 13:30:00"
date2 = "2022.05.10 12:00:00"
# 将输入的日期转换为“datetime.datetime”类型
# 由于日期的类型是字符串,因此不能直接进行计算,会报错
date1 = datetime.strptime(date1, "%Y.%m.%d %H:%M:%S")
date2 = datetime.strptime(date2, "%Y.%m.%d %H:%M:%S")
print(" date1:", date1, "\n" ,"date2:", date2)
print(" 2个日期的类型分别是:\n", type(date1), type(date2))
# 计算时间差:时间差的类型为“datetime.timedelta”类型
duration = date1 - date2
print(duration)
# 对计算差值进行天数(days)和秒数(seconds)的提取,并将秒数转换为小时数
day = duration.days
hour = duration.seconds/3600
print("days:", day)
print("hours:", hour)
6. 将分钟变成整10分钟,并提取
将 ‘2023-08-18 13:16:52.850000’ 变成 ‘2023-08-18 13:10:00.000000’
df['time2'] = df['time'].apply(lambda x: x.replace(minute=(x.minute // 10) * 10, second=0, microsecond=0))
将 ‘2023-08-18 13:16:52.850000’ 变成 ‘2023-08-18 13:16:00.000000’
df['time2'] = df['time'].apply(lambda x: x.replace(second=0, microsecond=0))
7. 筛选某个时间段内的数据
start_date = pd.Timestamp('2021-07-11')
end_date = pd.Timestamp('2021-08-10') # 此处过滤的不是很精准
Su_real_filtered = Su_real_filtered[
(Su_real_filtered['TimeStamp'] >= start_date) & (Su_real_filtered['TimeStamp'] < end_date)]
8. 某个时间段内应有10min的个数
2024-02-05 10:00:00 ~ 2024-04-03 05:50:00,这段时间共有多少个10分钟,算上起止
from datetime import datetime, timedelta
# Define the start and end times
start_time = datetime(2024, 2, 5, 10, 0, 0)
end_time = datetime(2024, 4, 3, 5, 50, 0)
# Calculate the total duration in minutes
total_duration_minutes = (end_time - start_time).total_seconds() / 60
# Calculate the number of 10-minute intervals
num_10_min_intervals = total_duration_minutes / 10
# Since we need to include both start and end times
num_10_min_intervals_inclusive = num_10_min_intervals + 1
num_10_min_intervals_inclusive # 8238
从2024年2月5日10:00:00到2024年4月3日05:50:00这段时间内,共有8328个10分钟的时间段(包括起止时间)。
参考链接:
[1] Python计算时间差及时间加减法 2021.2;
[2] Python日期时间差的计算(天/小时/分钟)及timedelta函数的使用(附python代码)2022.5;