from openpyxl import loadworkbook import pandas as pd import datetime 打开Excel文件 wb =…
- User
from openpyxl import load_workbook import pandas as pd import datetime
打开Excel文件
wb = load_workbook(filename=r'C:\Users\LIQI\Documents\民生基金\行业打分\钢铁数据跟踪汇总.xlsx', read_only=True, data_only=True)
选择指定的sheet
sheet = wb['产销数据'] # 这里的Sheet3是你要读取的sheet名称,可以根据实际情况修改
选择指定的列
columns = [0, 11, 12, 13, 14, 15]
读取数据
data = [] for row in sheet.iter_rows(values_only=True): data.append([row[column] for column in columns])
构建DataFrame
headers = ['Column1','Column2', 'Column3', 'Column4', 'Column5', 'Column6'] # 这里是DataFrame的列名,可以根据实际情况修改 df = pd.DataFrame(data, columns=headers) df = df[1:] df.columns = df.iloc[0] df = df[1:]
将第一列作为索引并转换为datetime格式
df.set_index(df.columns[0], inplace=True) df.index = pd.to_datetime(df.index)
def calculate_percentile(df,date, column): date = pd.to_datetime(date) year = date.year month = date.month day = date.day five_years_ago = datetime.datetime.now() - pd.DateOffset(years=5) history_dates = df[(df.index.month == month) & (df.index.day == day) & (df.index.year <= year) & (df.index > five_years_ago)].index history_values = df[df.index.isin(history_dates)][column] target_value = df.loc[date, column] mean_target_value = df.loc[date:date-4, column].mean() min_value = history_values.min() max_value = history_values.max() percentile= (target_value - min_value) / (max_value - min_value) * 100 return percentile return history_dates percentile =calculate_percentile(df,'2023-08-19','螺纹钢') print(percentile) print(history_dates)
TypeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_8864/817727447.py in 43 return percentile 44 return history_dates ---> 45 percentile =calculate_percentile(df,'2023-08-19','螺纹钢') 46 print(percentile) 47 print(history_dates) 我运行上述代码 返回了如下错误 请帮我看下原因? ~\AppData\Local\Temp/ipykernel_8864/817727447.py in calculate_percentile(df, date, column) 37 history_values = df[df.index.isin(history_dates)][column] 38 target_value = df.loc[date, column] ---> 39 mean_target_value = df.loc[date:date-4, column].mean() 40 min_value = history_values.min() 41 max_value = history_values.max()
~\anaconda3\lib\site-packages\pandas_libs\tslibs\timestamps.pyx in pandas._libs.tslibs.timestamps._Timestamp.sub()
~\anaconda3\lib\site-packages\pandas_libs\tslibs\timestamps.pyx in pandas._libs.tslibs.timestamps._Timestamp.add()
TypeError: Addition/subtraction of integers and integer-arrays with Timestamp is no longer supported. Instead of adding/subtracting
n, usen * obj.freq - Assistant
在代码的第39行,出现了一个错误,错误提示为“Addition/subtraction of integers and integer-arrays with Timestamp is no longer supported. Instead of adding/subtracting
n, usen * obj.freq。”这是因为代码中使用了在Timestamp对象上加减一个整数,这种写法不再被支持。要解决这个问题,可以将整数-4替换为
pd.DateOffset(years=4),即将这一行代码修改为:mean_target_value = df.loc[date:date - pd.DateOffset(years=4), column].mean()。修改后的代码如下:
- User
(0.0, DatetimeIndex(['2020-08-19', '2021-08-19', '2022-08-19', '2023-08-19'], dtype='datetime64[ns]', name=NaT, freq=None))
NameError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_8864/724220345.py in 44 percentile =calculate_percentile(df,'2023-08-19','螺纹钢') 45 print(percentile) ---> 46 print(history_dates)
NameError: name 'history_dates' is not defined
- Assistant
根据您的代码,发现在第46行使用了未定义的变量
history_dates,导致出现了一个NameError错误。要解决这个问题,可以将
calculate_percentile函数的返回值中包含history_dates,并将其作为calculate_percentile函数的第二个返回值。修改后的代码如下:
python