两个日期之间的月份 - pandas 系列和 datetime.date.today()
我正在尝试计算两个日期之间的月数。我正在 pandas 系列上运行该操作。
示例系列:
3645 2014-06-24
3646 2020-11-03
3647 2016-06-28
3648 2017-07-20
3649 2000-03-27
Name: lastSaleDate, Length: 1797, dtype: datetime64[ns]
我想计算自该日期以来的月数,即列中的日期与今天之间的差异。
import pandas as pd
import datetime as dt
import numpy as np
# Calc. months since last txn
df['mos'] = ((df.Date - dt.date.today())/np.timedelta64(1, 'M'))
回溯:
TypeError Traceback (most recent call last)
/var/folders/d0/gnksqzwn2fn46fjgrkp6045c0000gn/T/ipykernel_92607/847000123.py in <module>
5
6 # Calc. months
----> 7 df['mos'] = ((df.Date - dt.date.today())/np.timedelta64(1, 'M'))
/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/ops/common.py in new_method(self, other)
67 other = item_from_zerodim(other)
68
---> 69 return method(self, other)
70
71 return new_method
/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/arraylike.py in __sub__(self, other)
98 @unpack_zerodim_and_defer("__sub__")
99 def __sub__(self, other):
--> 100 return self._arith_method(other, operator.sub)
101
102 @unpack_zerodim_and_defer("__rsub__")
/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/series.py in _arith_method(self, other, op)
5524
5525 with np.errstate(all="ignore"):
-> 5526 result = ops.arithmetic_op(lvalues, rvalues, op)
5527
5528 return self._construct_result(result, name=res_name)
/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/ops/array_ops.py in arithmetic_op(left, right, op)
216 # Timedelta/Timestamp and other custom scalars are included in the check
217 # because numexpr will fail on it, see GH#31457
--> 218 res_values = op(left, right)
219 else:
220 # TODO we should handle EAs consistently and move this check before the if/else
TypeError: unsupported operand type(s) for -: 'DatetimeArray' and 'datetime.date'
I am trying to calculate the number of months between two dates. I am running the operation on a pandas Series.
Sample series:
3645 2014-06-24
3646 2020-11-03
3647 2016-06-28
3648 2017-07-20
3649 2000-03-27
Name: lastSaleDate, Length: 1797, dtype: datetime64[ns]
I'd like to calculate the number of months since this date, i.e. the difference between date in the column and today.
import pandas as pd
import datetime as dt
import numpy as np
# Calc. months since last txn
df['mos'] = ((df.Date - dt.date.today())/np.timedelta64(1, 'M'))
Traceback:
TypeError Traceback (most recent call last)
/var/folders/d0/gnksqzwn2fn46fjgrkp6045c0000gn/T/ipykernel_92607/847000123.py in <module>
5
6 # Calc. months
----> 7 df['mos'] = ((df.Date - dt.date.today())/np.timedelta64(1, 'M'))
/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/ops/common.py in new_method(self, other)
67 other = item_from_zerodim(other)
68
---> 69 return method(self, other)
70
71 return new_method
/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/arraylike.py in __sub__(self, other)
98 @unpack_zerodim_and_defer("__sub__")
99 def __sub__(self, other):
--> 100 return self._arith_method(other, operator.sub)
101
102 @unpack_zerodim_and_defer("__rsub__")
/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/series.py in _arith_method(self, other, op)
5524
5525 with np.errstate(all="ignore"):
-> 5526 result = ops.arithmetic_op(lvalues, rvalues, op)
5527
5528 return self._construct_result(result, name=res_name)
/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/ops/array_ops.py in arithmetic_op(left, right, op)
216 # Timedelta/Timestamp and other custom scalars are included in the check
217 # because numexpr will fail on it, see GH#31457
--> 218 res_values = op(left, right)
219 else:
220 # TODO we should handle EAs consistently and move this check before the if/else
TypeError: unsupported operand type(s) for -: 'DatetimeArray' and 'datetime.date'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如错误所示,类型不匹配。您必须将
datetime.date
对象转换为 pandas datetime,或者可以使用to_datetime('today')
代替:或(更易读)
输出:
As the error says, the types don't match. You have to either convert
datetime.date
object to pandas datetime or you could useto_datetime('today')
instead:or (more readably)
Output: