gpt4 book ai didi

python - 如何从时间戳中减去充满日期的 pandas.core.series.Series ,以找到每行日期与该时间戳日期的差异?

转载 作者:太空宇宙 更新时间:2023-11-03 20:36:06 28 4
gpt4 key购买 nike

我想从时间戳中减去日期。settings.dataset_end_date 是一个 pandas._libs.tslibs.timestamps.Timestamp引用['date_of_patent']是一个pandas.core.series.Seriespatent['date'] 是一个 pandas.core.series.Series

我创建了一个示例代码,其中我已经执行了似乎工作正常的 dtypes 转换,但是当将其应用到我的完整数据集时,我收到了上面的错误。

```python

settings.dataset_end_date = pd.to_datetime('2019-01-01')

#Find citations and dates from 'uspatentcitation' and 'patent'
citation = citation.rename(columns={'citation_id': 'id', 'date_citation': 'date_of_patent', 'date_id': 'date_cited'})

# Find time between citation and target patent grant date
citation['delta_citation'] = (citation['date_cited'].subtract(citation['date_of_patent'])).dt.days/365

# Find the years available
citation['years_available'] = (settings.dataset_end_date - citation['date_of_patent']).dt.days/365
patent['years_available'] = (settings.dataset_end_date - patent['date']).dt.days/365
```

I expect column with differences in years

```python
____________________________________________________________________
Error_______________________________________________________________

OverflowError Traceback (most recent call last)
<ipython-input-70-300ff02274d2> in <module>()
5
6 # Find time between citation and target patent grant date
----> 7 citation['delta_citation'] = (citation['date_cited'] - citation['date_of_patent']).dt.days/365
8
9 # Find the years available (used for nulling where the dataset ends before a time could be reached)

/anaconda/envs/py35/lib/python3.5/site-packages/pandas/core/ops.py in wrapper(left, right)
1550 # test_dt64_series_add_intlike, which the index dispatching handles
1551 # specifically.
-> 1552 result = dispatch_to_index_op(op, left, right, pd.DatetimeIndex)
1553 return construct_result(left, result,
1554 index=left.index, name=res_name,

/anaconda/envs/py35/lib/python3.5/site-packages/pandas/core/ops.py in dispatch_to_index_op(op, left, right, index_class)
1189 left_idx = left_idx._shallow_copy(freq=None)
1190 try:
-> 1191 result = op(left_idx, right)
1192 except NullFrequencyError:
1193 # DatetimeIndex and TimedeltaIndex with freq == None raise ValueError

/anaconda/envs/py35/lib/python3.5/site-packages/pandas/core/ops.py in wrapper(left, right)
1550 # test_dt64_series_add_intlike, which the index dispatching handles
1551 # specifically.
-> 1552 result = dispatch_to_index_op(op, left, right, pd.DatetimeIndex)
1553 return construct_result(left, result,
1554 index=left.index, name=res_name,

/anaconda/envs/py35/lib/python3.5/site-packages/pandas/core/ops.py in dispatch_to_index_op(op, left, right, index_class)
1189 left_idx = left_idx._shallow_copy(freq=None)
1190 try:
-> 1191 result = op(left_idx, right)
1192 except NullFrequencyError:
1193 # DatetimeIndex and TimedeltaIndex with freq == None raise ValueError

/anaconda/envs/py35/lib/python3.5/site-packages/pandas/core/ops.py in rsub(left, right)
146
147 def rsub(left, right):
--> 148 return right - left
149
150

/anaconda/envs/py35/lib/python3.5/site-packages/pandas/core/indexes/datetimelike.py in __sub__(self, other)
499 def __sub__(self, other):
500 # dispatch to ExtensionArray implementation
--> 501 result = self._data.__sub__(maybe_unwrap_index(other))
502 return wrap_arithmetic_op(self, other, result)
503

/anaconda/envs/py35/lib/python3.5/site-packages/pandas/core/arrays/datetimelike.py in __sub__(self, other)
1273 elif is_datetime64_dtype(other) or is_datetime64tz_dtype(other):
1274 # DatetimeIndex, ndarray[datetime64]
-> 1275 result = self._sub_datetime_arraylike(other)
1276 elif is_period_dtype(other):
1277 # PeriodIndex

/anaconda/envs/py35/lib/python3.5/site-packages/pandas/core/arrays/datetimes.py in _sub_datetime_arraylike(self, other)
722 other_i8 = other.asi8
723 new_values = checked_add_with_arr(self_i8, -other_i8,
--> 724 arr_mask=self._isnan)
725 if self._hasnans or other._hasnans:
726 mask = (self._isnan) | (other._isnan)

/anaconda/envs/py35/lib/python3.5/site-packages/pandas/core/algorithms.py in checked_add_with_arr(arr, b, arr_mask, b_mask)
936
937 if to_raise:
--> 938 raise OverflowError("Overflow in int64 addition")
939 return arr + b
940

OverflowError: Overflow in int64 addition

I have updated my Pandas and if I only run this part of the code:

```python
# Find the years available
citation['years_available'] = (settings.dataset_end_date - citation['date_of_patent']).dt.days/365
patent['years_available'] = (settings.dataset_end_date - patent['date']).dt.days/365
```

I get the following error:

```python
/anaconda/envs/py35/lib/python3.5/site-packages/pandas/core/indexes/datetimelike.py in __sub__(self, other)
499 return super()._convert_scalar_indexer(key, kind=kind)
500
--> 501 @classmethod
502 def _add_datetimelike_methods(cls):
503 """/anaconda/envs/py35/lib/python3.5/site-packages/pandas/core/arrays/datetimelike.py in __sub__(self, other)
1253 return NotImplemented
1254
-> 1255 if is_timedelta64_dtype(result) and isinstance(result, np.ndarray):
1256 from pandas.core.arrays import TimedeltaArray
1257

/anaconda/envs/py35/lib/python3.5/site-packages/pandas/core/arrays/datetimes.py in _sub_datetimelike_scalar(self, other)
761
762 # -----------------------------------------------------------------
--> 763 # Comparison Methods
764
765 _create_comparison_method = classmethod(_dt_array_cmp)

/anaconda/envs/py35/lib/python3.5/site-packages/pandas/core/algorithms.py in checked_add_with_arr(arr, b, arr_mask, b_mask)
936 def checked_add_with_arr(arr, b, arr_mask=None, b_mask=None):
937 """
--> 938 Perform array addition that checks for underflow and overflow.
939
940 Performs the addition of an int64 array and an int64 integer (or array)

OverflowError: Overflow in int64 addition

最佳答案

检查您的日期时间列是否实际上是日期时间类型,否则在减去时间戳之前将其转换:

citation['date_of_patent'] = pd.to_datetime(citation['date_of_patent'])
citation['date_cited'] = pd.to_datetime(citation['date_cited'])

关于python - 如何从时间戳中减去充满日期的 pandas.core.series.Series ,以找到每行日期与该时间戳日期的差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57160764/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com