I have the following df :
我有以下df:
import pandas as pd
from datetime import datetime, timedelta
df = pd.DataFrame([
["A", "2018-08-03"],
["B", "2018-08-20"]
])
df.columns = ["Item", "Date"]
I want to get the first day of the week for every line of my df. I tried to do this :
我希望得到我的df的每一行的一周的第一天。我试着这样做:
df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d')
df["Day_of_Week"] = df.Date.dt.weekday
df["First_day_of_the_week"] = df.Date - timedelta(days=df.Day_of_Week)
But I got that error message :
但是我收到了错误消息:
TypeError: unsupported type for timedelta days component: Series
How can I get the first day of the week for a Series ? My expected result is that :
我怎样才能获得系列赛的第一天?我的预期结果是:
- "A", "2018-08-03", "2018-07-30"
- “A”,“2018-08-03”,“2018-07-30”
- "B", "2018-08-20", "2018-08-20"
- “B”,“2018-08-20”,“2018-08-20”
4 个解决方案
#1
5
Unfortunately timedelta
doesn't support a vectorized form so I would go for an apply
不幸的是timedelta不支持矢量化形式,所以我会去申请
df["First_day_of_the_week"] = df.apply(lambda x: x['Date'] - timedelta(days=x['Day_of_Week']), axis=1)
EDIT
编辑
timedelta
doesn't support vectorized arguments but can be multiplied by a vector :)
timedelta不支持向量化参数,但可以乘以向量:)
df["First_day_of_the_week"] = df.Date - df.Day_of_Week * timedelta(days=1)
#2
3
Leave out your 'Day of week" calculation and do this.
省略“星期几”计算并执行此操作。
df["First_day_of_the_week"] = df['Date'].apply(lambda x: (x - timedelta(days=x.dayofweek)))
print(df)
giving
给
Item Date First_day_of_the_week
0 A 2018-08-03 2018-07-30
1 B 2018-08-20 2018-08-20
#3
3
A vectorised solution is possible with NumPy:
使用NumPy可以实现矢量化解决方案:
df['First_day'] = df['Date'] - df['Date'].dt.weekday * np.timedelta64(1, 'D')
print(df)
Item Date First_day
0 A 2018-08-03 2018-07-30
1 B 2018-08-20 2018-08-20
#4
2
You can stay in Pandas and use its DateOffset objects:
您可以留在Pandas并使用其DateOffset对象:
>>> from pandas.tseries.offsets import Week
>>> df.Date.where(df.Date.dt.weekday == 0, df.Date - Week(weekday=0))
0 2018-07-30
1 2018-08-20
Name: Date, dtype: datetime64[ns]
The trick being that you need to not do the subtraction where the weekday is already Monday (weekday == 0). This says, "in cases where weekday is already zero, do nothing; else, return Monday of that week."
诀窍是你不需要在工作日已经是星期一(工作日== 0)的情况下进行减法。这说,“在工作日已经为零的情况下,什么都不做;否则,返回那周的星期一。”
#1
5
Unfortunately timedelta
doesn't support a vectorized form so I would go for an apply
不幸的是timedelta不支持矢量化形式,所以我会去申请
df["First_day_of_the_week"] = df.apply(lambda x: x['Date'] - timedelta(days=x['Day_of_Week']), axis=1)
EDIT
编辑
timedelta
doesn't support vectorized arguments but can be multiplied by a vector :)
timedelta不支持向量化参数,但可以乘以向量:)
df["First_day_of_the_week"] = df.Date - df.Day_of_Week * timedelta(days=1)
#2
3
Leave out your 'Day of week" calculation and do this.
省略“星期几”计算并执行此操作。
df["First_day_of_the_week"] = df['Date'].apply(lambda x: (x - timedelta(days=x.dayofweek)))
print(df)
giving
给
Item Date First_day_of_the_week
0 A 2018-08-03 2018-07-30
1 B 2018-08-20 2018-08-20
#3
3
A vectorised solution is possible with NumPy:
使用NumPy可以实现矢量化解决方案:
df['First_day'] = df['Date'] - df['Date'].dt.weekday * np.timedelta64(1, 'D')
print(df)
Item Date First_day
0 A 2018-08-03 2018-07-30
1 B 2018-08-20 2018-08-20
#4
2
You can stay in Pandas and use its DateOffset objects:
您可以留在Pandas并使用其DateOffset对象:
>>> from pandas.tseries.offsets import Week
>>> df.Date.where(df.Date.dt.weekday == 0, df.Date - Week(weekday=0))
0 2018-07-30
1 2018-08-20
Name: Date, dtype: datetime64[ns]
The trick being that you need to not do the subtraction where the weekday is already Monday (weekday == 0). This says, "in cases where weekday is already zero, do nothing; else, return Monday of that week."
诀窍是你不需要在工作日已经是星期一(工作日== 0)的情况下进行减法。这说,“在工作日已经为零的情况下,什么都不做;否则,返回那周的星期一。”