Pandas 尽管使用了.loc,但仍在熊猫中设置复制警告
我有一个数据帧,df。我对所选列应用了一个函数,创建了新列“est_arr_date”和“act_arr_date”,并写入了值。然后,我计算了两个新创建列之间的日期差,并将值写入一个新列“delta”。这是我的密码Pandas 尽管使用了.loc,但仍在熊猫中设置复制警告,pandas,Pandas,我有一个数据帧,df。我对所选列应用了一个函数,创建了新列“est_arr_date”和“act_arr_date”,并写入了值。然后,我计算了两个新创建列之间的日期差,并将值写入一个新列“delta”。这是我的密码 import pandas as pd import datetime df = pd.DataFrame( { "estimated_arrival_date": [ "2018-06-30T00:0
import pandas as pd
import datetime
df = pd.DataFrame(
{
"estimated_arrival_date": [
"2018-06-30T00:00:00",
"2018-07-01T00:00:00",
"2018-07-02T00:00:00",
"2018-07-02T00:00:00",
"2018-07-02T00:00:00",
"2018-07-01T00:00:00",
],
"actual_arrival_date": [
"2018-07-01T00:00:00",
"2018-07-01T00:00:00",
"2018-07-01T00:00:00",
"2018-07-01T00:00:00",
"2018-07-01T00:00:00",
"2018-07-01T00:00:00",
],
}
)
def convert_StrTime(str_tme):
"""This is the helper function."""
tme = str_tme[0:10]
convert_tme = datetime.datetime.strptime(tme, "%Y-%m-%d").date()
return convert_tme
df.loc[:, "est_arr_date"] = df.loc[:, "estimated_arrival_date"].apply(convert_StrTime)
df.loc[:, "act_arr_date"] = df.loc[:, "actual_arrival_date"].apply(convert_StrTime)
df["delta"] = df["est_arr_date"] - df["act_arr_date"]
我不明白的是这个错误:
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
有人能给我解释一下吗?我想我已经按照建议使用了.loc了?我想这行代码上面有一些问题,你能在你的代码样本上面添加5-6行吗?Hi@jezrael,按要求添加了DF。我得到了我的输出DF,只是它让我不明白为什么我会得到这个错误。谢谢运行上述代码不会触发警告。也许警告来自你程序中的另一行?@w-m嗨!我刚刚运行了这3行,它触发了警告。我无法用你的例子重现这个问题。我没有收到任何警告