Python 分配给数据帧的切片

Python 分配给数据帧的切片,python,pandas,Python,Pandas,我正在努力为任何给定的日期制定一个生效日期。数据帧 有一列,由bmmonthend(每月的最后一个工作日)填充 考虑假期-按此处未显示的代码计算) 下面显示的部分数据框的生效日期等于日期1 台阶 我现在尝试选择需要更改的数据: df[~(df.Date<df.BMonthEnd)].EffectiveDate # giving the expected slice # but df[~(df.Date<df.BMonthEnd)].EffectiveDate = 1 # give

我正在努力为任何给定的日期制定一个生效日期。数据帧 有一列,由bmmonthend(每月的最后一个工作日)填充 考虑假期-按此处未显示的代码计算)

下面显示的部分数据框的生效日期等于日期1 台阶

我现在尝试选择需要更改的数据:

df[~(df.Date<df.BMonthEnd)].EffectiveDate  # giving the expected slice
# but 
df[~(df.Date<df.BMonthEnd)].EffectiveDate = 1
# gives error

SettingWithCopyWarning: A value is trying to be set on a copy of a slice
from a DataFrame. Try using .loc[row_index,col_indexer] = value instead
self[name] = value

df[~(df.Date算出了它。选择数据帧中的序列可以有效地分配给它和原始数据帧。这允许我使用切片syntac应用影响结果的逻辑:

# not all methods, classes shown
def effective_date(dr):
    df = pd.DataFrame(dr, index=dr, columns=['Date'])
    df['BMonthEnd'] = df.Date.apply(h.last_business_day)
    df['MonthEnd'] = df.Date.apply(h.month_end)
    df['EffectiveDate'] = df.Date
    # df.EffectiveDate[~(df.Date<df.BMonthEnd)] = df.MonthEnd
    df.loc[~(df.Date<df.BMonthEnd),'EffectiveDate'] = df.MonthEnd
    return df.EffectiveDate
#并非所有方法、类都显示
def生效日期(dr):
df=pd.DataFrame(dr,index=dr,columns=['Date'])
df['BMonthEnd']=df.Date.apply(最后一个营业日)
df['MonthEnd']=df.日期.应用(h.月底)
df['EffectiveDate']=df.Date

#df.EffectiveDate[~(df.Dateuse df.loc[row\u indexer,col\u indexer]=值就像它所说的(您没有这样做)df.EffectiveDate[~(df.date)
df['EffectiveDate'的数据类型是什么
?如果当天是当月的最后一个工作日,但不是当月的最后一个日历日,则生效日期应为最后一个日历日。例如,确保在当月的最后一个工作日使用完整的日历月进行财务计算……否则会有一些天不被计算在内。pandas的文档在一般来说,本节提供了一些关于返回数据视图和副本的方法的有用提示:使用这个:
df.loc[~(df.Date
df.loc[~(df.Date<df.BMonthEnd)].EffectiveDate = 1
# not all methods, classes shown
def effective_date(dr):
    df = pd.DataFrame(dr, index=dr, columns=['Date'])
    df['BMonthEnd'] = df.Date.apply(h.last_business_day)
    df['MonthEnd'] = df.Date.apply(h.month_end)
    df['EffectiveDate'] = df.Date
    # df.EffectiveDate[~(df.Date<df.BMonthEnd)] = df.MonthEnd
    df.loc[~(df.Date<df.BMonthEnd),'EffectiveDate'] = df.MonthEnd
    return df.EffectiveDate