将pandas数据帧日期列转换为“周开始”列的最有效方法是什么?在我的情况下,我想转换为该周的星期日。例如,我希望2016-04-01转换为2016-03-27。
答案 0 :(得分:2)
使用to_period("W-SAT")
转换为期间系列,然后使用start_time
获取期间的开始时间:
import pandas as pd
di = pd.date_range("2016/01/01", "2016/04/10").to_series()
di.dt.to_period("W-SAT").dt.start_time
答案 1 :(得分:1)
您可以对索引使用to_period('W')
转换,并在上周使用偏移量
In [56]: ts
Out[56]:
2016-04-01 -1.174966
2016-04-02 -0.518799
2016-04-03 -0.598929
2016-04-04 0.085304
2016-04-05 -0.648838
2016-04-06 -0.427322
2016-04-07 0.146146
2016-04-08 -1.957471
2016-04-09 -0.302514
2016-04-10 1.249215
Freq: D, dtype: float64
In [57]: ts.index.to_period('W').to_timestamp('W') + pd.offsets.DateOffset(-7)
Out[57]:
DatetimeIndex(['2016-03-27', '2016-03-27', '2016-03-27', '2016-04-03',
'2016-04-03', '2016-04-03', '2016-04-03', '2016-04-03',
'2016-04-03', '2016-04-03'],
dtype='datetime64[ns]', freq=None)
如果您有日期栏,如
In [90]: ds
Out[90]:
date val
0 2016-04-01 0.087695
1 2016-04-02 -0.163535
2 2016-04-03 -1.069274
3 2016-04-04 1.415452
4 2016-04-05 -1.100268
5 2016-04-06 0.239589
6 2016-04-07 -1.045833
7 2016-04-08 -0.325026
8 2016-04-09 -0.423831
9 2016-04-10 -1.320371
您可以使用dt
- accessor对象获取Series值的datetimelike属性。
In [91]: ds['date'].dt.to_period('W').dt.to_timestamp('W') + pd.offsets.DateOffset(-7)
Out[91]:
DatetimeIndex(['2016-03-27', '2016-03-27', '2016-03-27', '2016-04-03',
'2016-04-03', '2016-04-03', '2016-04-03', '2016-04-03',
'2016-04-03', '2016-04-03'],
dtype='datetime64[ns]', freq=None)
答案 2 :(得分:1)
您可以查看此信息并在此处应用:Find the Friday of previous/last week in python
from dateutil.relativedelta import relativedelta, SU
from datetime import datetime
import pandas as pd
df = pd.DataFrame({'date': [datetime(2016, 4, 1), datetime(2016, 1, 1)]})
relative_delta = relativedelta(weekday=SU(-1))
df['date'] = df['date'].apply(lambda x: x+relative_delta)