英文:
Efficient days between two dates in Pandas
问题
works
pre_start['exposure_days'] = (datetime.now() - pd.to_datetime(pre_start['First_Exposure']))
doesn't work
pre_start['exposure_days'] = (datetime.now() - pd.to_datetime(pre_start['First_Receive_Date'])).days
我得到的错误是`AttributeError: 'Series' object has no attribute 'days'`。
除了像这样做一些繁琐和低效的事情
`pre_start['real_exposure_days'] = pre_start['exposure_days'].apply(lambda x: x.days)`,是否有一种绕过此错误获取天数的方法?
<details>
<summary>英文:</summary>
```python
# works
pre_start['exposure_days'] = (datetime.now() - pd.to_datetime(pre_start['First_Exposure']))
# doesn't work
pre_start['exposure_days'] = (datetime.now() - pd.to_datetime(pre_start['First_Receive_Date'])).days
The error I get is AttributeError: 'Series' object has no attribute 'days'
.
Without doing something obnoxious and inefficient like
pre_start['real_exposure_days'] = pre_start['exposure_days'].apply(lambda x: x.days)
is there a way to get the number of days, circumventing this error?
答案1
得分: 1
你应该使用dt
访问器:
pre_start['exposure_days'] = (datetime.now() - pd.to_datetime(pre_start['First_Receive_Date'])).dt.days
英文:
What you should do is use the dt
accessor:
pre_start['exposure_days'] = (datetime.now() - pd.to_datetime(pre_start['First_Receive_Date'])).dt.days
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论