Python 3.x 将对象列复制为类别类型以发出警告

Python 3.x 将对象列复制为类别类型以发出警告,python-3.x,pandas,Python 3.x,Pandas,我正在将一个pandas对象列复制到一个单独的有序列中,但是我得到了一个警告,并且我一生都没有找到正确的方法 我无法发布整个数据帧,但以下是我使用的语法: marriage_cat_type = CategoricalDtype(categories= ['M_22', 'M_23', 'M_24', 'M_25', 'M_26', 'M_27', 'M_28', 'M_29', 'M_30' ,

我正在将一个pandas对象列复制到一个单独的有序列中,但是我得到了一个警告,并且我一生都没有找到正确的方法

我无法发布整个数据帧,但以下是我使用的语法:

marriage_cat_type = CategoricalDtype(categories= ['M_22', 'M_23', 'M_24', 'M_25', 'M_26', 'M_27', 'M_28', 'M_29', 'M_30'
                                                  , 'M_31', 'M_32', 'M_33', 'M_34', 'M_35', 'M_36', 'M_37', 'M_38', 'M_39'
                                                  , 'M_40', 'M_41', 'M_42', 'M_43', 'M_44', 'M_45', 'M_46', 'M_47', 'M_48'
                                                  , 'M_49', 'M_50', 'M_51', 'M_52', 'M_53', 'M_54', 'M_55', 'M_56', 'M_57'
                                                  , 'M_58', 'M_59', 'M_60', 'M_61', 'M_62', 'M_63', 'M_64', 'M_65', 'M_66'
                                                  , 'M_67', 'M_68', 'M_69', 'M_70', 'M_71', 'M_72', 'M_73', 'M_74', 'M_75'
                                                  , 'M_76', 'M_77', 'M_78', 'M_79', 'M_80', 'M_81', 'M_82', 'M_999', 'S_18'
                                                  , 'S_19', 'S_20', 'S_21', 'S_22', 'S_23', 'S_24', 'S_25', 'S_26', 'S_27'
                                                  , 'S_28', 'S_29', 'S_30', 'S_31', 'S_32', 'S_33', 'S_34', 'S_35', 'S_36'
                                                  , 'S_37', 'S_38', 'S_39', 'S_40', 'S_41', 'S_42', 'S_43', 'S_44', 'S_45'
                                                  , 'S_46', 'S_47', 'S_48', 'S_49', 'S_50', 'S_51', 'S_52', 'S_53', 'S_54'
                                                  , 'S_55', 'S_56', 'S_57', 'S_58', 'S_59', 'S_60', 'S_61', 'S_62', 'S_63'
                                                  , 'S_64', 'S_65', 'S_66', 'S_67', 'S_68', 'S_69', 'S_70', 'S_71', 'S_72'
                                                  , 'S_73', 'S_74', 'S_75', 'S_77', 'S_79', 'S_999'], ordered = True)

coll_train['marriage_statusXage_codes'] = coll_train['marital_statusXage2'].astype(marriage_cat_type)
我收到了这个警告

C:\ProgramData\Anaconda3\lib\site packages\ipykernel\u launcher.py:2: SettingWithCopyWarning:正在尝试在副本上设置值 从数据帧切片。尝试使用.loc[行索引器、列索引器]= 取而代之的是价值观

请参阅文档中的注意事项:

我试过这个,结果失败了:

coll_train[‘婚姻状况密码’]=coll_train.loc[:, “婚姻状况表2'].aType(婚姻类型)


有人能给我指出正确的方向吗?

这是一个链式分配问题。可由
pd.set_选项('chained_assignment',None |'warn'|'raise')
操作

警告已打开,熊猫不喜欢
coll\u train

有两个选项:确保
coll\u train
是您要修改的源数据帧(在其中放置一个名为
marriage\u statusXage\u codes
的新列)。如果是,并且熊猫是错误的,则设置
pd.set\u选项('chained\u assignment',None)
。熊猫会错吗?我不知道

下面是在切片上设置值的图示

import pandas as pd
from pandas.compat import StringIO

print(pd.__version__)

csvdata = StringIO("""date,LASTA,LASTB,LASTC
1999-03-15,2.5597,8.20145,16.900
1999-03-31,2.7724,7.73057,16.955
1999-04-01,2.8321,7.63714,17.500
1999-04-06,2.8537,7.63703,17.750""")

df = pd.read_csv(csvdata, sep=",", index_col="date", parse_dates=True, infer_datetime_format=True)

pd.set_option('chained_assignment','warn')

a_slice = df['1999-03-31':'1999-04-01']
print(id(df), id(a_slice))
# generates the warning
a_slice['LASTA'] = 10
# original does not have the data set on a slice!
print(df[df['LASTA'] == 10]['LASTA'].any())

# create a new object to which values can be set, no warning.
a_slice = a_slice.copy()
a_slice['LASTA'] = 10
print(a_slice[a_slice['LASTA'] == 10]['LASTA'].any())

结果

0.20.3
(4549520208, 4594637776)
slicecopy.py:20: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  a_slice['LASTA'] = 10
False
True