【发布时间】:2020-05-03 11:40:50
【问题描述】:
我真的在这里度过了一段艰难的时光。
我的 DataFrame 是这样的
Purchase_Date Customer_ID Gender
0 2012-12-18 00:00:00 7223 F
1 2012-12-20 00:00:00 7841 M
2 2012-12-21 00:00:00 8374 F
我的目标是将“购买日期”列从字符串更改为日期时间对象,以便我可以通过应用此函数来运行同期群分析:
def get_month(x): return dt.datetime(x.year, x.month, 1)
data['InvoiceMonth'] = data['Purchase_Date'].apply(get_month)
grouping = data.groupby('Customer_ID')['InvoiceMonth']
data['CohortMonth'] = grouping.transform('min')
函数返回错误:'str'对象没有属性'year' 我尝试了以下函数并使用了所有参数(dayfirst、yearfirst...)
data["Purchase_Date"] = pd.to_datetime(data["Purchase_Date"])
pd.to_datetime()
datetime.datetime.strptime()
我不断收到 ValueError: day is out of range for month
请帮忙
【问题讨论】:
标签: python pandas time-series string-to-datetime