stackoverflow 解决方案链接:
https://stackoverflow.com/questions/31468176/setting-values-on-a-copy-of-a-slice-from-a-dataframe?rq=1
出错代码:
def filter_date(date,df):
df['start_time'] = pd.to_datetime(df['start_time']) # 将数据类型转换为日期类型,可以直接获取相应年份的数据
df = df.set_index('start_time')
df_date = df[date]
return df_date
修改后代码:
def filter_date(date,df):
x = df.copy()
x.loc[:,'start_time'] = pd.to_datetime(x.loc[:,'start_time']) # 将数据类型转换为日期类型,可以直接获取相应年份的数据
x = x.set_index('start_time')
df_date = x[date]
return df_date
主要做了两点修改:
1.copy()
2.采用loc提取列
实际操作中增加copy()即可解决问题
https://blog.csdn.net/zaishijizhidian/article/details/98207892