pandas(python)是否提供了一种从日期序列中轻松获取当前一周(1:4)的方法?在
data = {'date': ['2014-05-01', '2014-05-01', '2014-05-02', '2014-05-02', '2014-05-02', '2014-05-02', '2014-05-03', '2014-05-03', '2014-05-04', '2014-05-04']}
df = pd.DataFrame(data, columns = ['date'])
df['date']=pd.to_datetime(df['date'])
def week_of_month(dt):
""" Returns the week of the month for the specified date.
"""
first_day = dt.replace(day=1)
dom = dt.day
adjusted_dom = dom + first_day.weekday()
return int(ceil(adjusted_dom/7.0))
df ['week']=np.nan
for i in range(len(df)):
df ['week'][i] = week_of_month(df.date.iloc[i])
对于短数据集,它可以工作,但对于大数据集,则需要大量资源
解决问题:
相关问题 更多 >
编程相关推荐