# 导入数据包
import pandas as pd
import datetime
# 导入数据,包含所有年份的数据
df = pd.read_excel("C:\\Users\\Thinkpad\\Desktop\\A.xls", "振弦")
#把时间列转化成日期格式并写入到新的test.xls表中
df['时间'] = pd.to_datetime(df['时间']).dt.date
df.to_excel('C:\\Users\\Thinkpad\\Desktop\\test.xls')
#获取新的test.xls表中Sheet1的数据
stock = pd.read_excel("C:\\Users\\Thinkpad\\Desktop\\test.xls", "Sheet1")
# 显示导入的数据
print(stock['时间'])
# 设置起始时间
s_date = datetime.datetime.strptime('2022/01/01', '%Y/%m/%d').date()
#设置结束时间
e_date = datetime.datetime.strptime('2022/12/31', '%Y/%m/%d').date()
# 选取2022年数据
stock_2022 = stock[(stock['时间'].dt.date >= s_date) & (stock['时间'].dt.date <= e_date)]
# 查看选取2022年数据并重新写入test.xls表中
print(stock_2022)
stock_2022.to_excel('C:\\Users\\Thinkpad\\Desktop\\test.xls')