【学习笔记】Datetime 数据转换时 ‘A value is trying to be set on a copy of a slice from a DataFrame’ 错误处理

在获取Datetime数据时,有时候需要吧数字数据(如20200413)转化为时间序列,直接用parse转换时出现‘A value is trying to be set on a copy of a slice from a DataFrame’ 错误提示,我在网上看别人的解决方法,感觉没有说清楚,这个错误是因为Datefream数据使用=赋值时,传入变量的是原数据的映射,如果修改变量,则相当于修改原始数据,错误警告是提示不应该有此套娃行为。

错误代码如下:

import pandas as pd
from dateutil.parser import parse
xg = '000700'
dt_factor = pd.read_csv(open(r'D:\python project\数据储存\%s stock_factor.csv' % xg), encoding='UTF-8', index_col=0)
fac_max = dt_factor['adj_factor'].max()
dt_factor['factor_q'] = dt_factor['adj_factor']/fac_max
for i in dt_factor.index:
    dt_factor['trade_date'][i] = parse(str(dt_factor['trade_date'][i])).date()
print(dt_factor)

这里的目的是为了将20200413(数据类型是int)转化为时间序列,使用了parse方法。

运行结果

D:/python project/text2.py:10: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  dt_factor['trade_date'][i] = parse(str(dt_factor['trade_date'][i])).date()
        ts_code  trade_date  adj_factor  factor_q
0     000700.SZ  2020-04-13      15.631  1.000000
1     000700.SZ  2020-04-10      15.631  1.000000
2     000700.SZ  2020-04-09      15.631  1.000000
3     000700.SZ  2020-04-08      15.631  1.000000
4     000700.SZ  2020-04-07      15.631  1.000000
...         ...         ...         ...       ...
 

解决套娃问题只需要将映射copy为新变量就ok了,代码如下:

import pandas as pd
from dateutil.parser import parse
xg = '000700'
dt_factor = pd.read_csv(open(r'D:\python project\数据储存\%s stock_factor.csv' % xg), encoding='UTF-8', index_col=0)
fac_max = dt_factor['adj_factor'].max()
dt_factor['factor_q'] = dt_factor['adj_factor']/fac_max
x = dt_factor['trade_date'].copy()
for i in dt_factor.index:
    x[i] = parse(str(dt_factor['trade_date'][i])).date()
dt_factor['trade_date'] = x
print(dt_factor)

运行结果:

Connected to pydev debugger (build 193.5662.61)
        ts_code  trade_date  adj_factor  factor_q
0     000700.SZ  2020-04-13      15.631  1.000000
1     000700.SZ  2020-04-10      15.631  1.000000
2     000700.SZ  2020-04-09      15.631  1.000000
3     000700.SZ  2020-04-08      15.631  1.000000
4     000700.SZ  2020-04-07      15.631  1.000000
...         ...         ...         ...       ...
3995  000700.SZ  2003-11-05       4.532  0.289937
3996  000700.SZ  2003-11-04       4.532  0.289937
3997  000700.SZ  2003-11-03       4.532  0.289937
3998  000700.SZ  2003-10-31       4.532  0.289937
3999  000700.SZ  2003-10-30       4.532  0.289937

[4000 rows x 4 columns]
错误提示消除

 

你可能感兴趣的:(【学习笔记】Datetime 数据转换时 ‘A value is trying to be set on a copy of a slice from a DataFrame’ 错误处理)