image.png
    stackoverflow 解决方案链接:
    https://stackoverflow.com/questions/31468176/setting-values-on-a-copy-of-a-slice-from-a-dataframe?rq=1
    出错代码:

    1. def filter_date(date,df):
    2. df['start_time'] = pd.to_datetime(df['start_time']) # 将数据类型转换为日期类型,可以直接获取相应年份的数据
    3. df = df.set_index('start_time')
    4. df_date = df[date]
    5. return df_date

    修改后代码:

    1. def filter_date(date,df):
    2. x = df.copy()
    3. x.loc[:,'start_time'] = pd.to_datetime(x.loc[:,'start_time']) # 将数据类型转换为日期类型,可以直接获取相应年份的数据
    4. x = x.set_index('start_time')
    5. df_date = x[date]
    6. return df_date

    主要做了两点修改:
    1.copy()
    2.采用loc提取列

    实际操作中增加copy()即可解决问题

    https://blog.csdn.net/zaishijizhidian/article/details/98207892