如何修复 – int64 加法溢出

huangapple go评论122阅读模式
英文:

How to fix - Overflow in int64 addition

问题

我正在尝试通过将一个名为df['num_days']的列添加到另一个列df['sampling_date']来计算未来日期,但在int64加法中出现了溢出。
源代码-

  1. df['sampling_date'] = pd.to_datetime(df['sampling_date'], errors='coerce')
  2. df['future_date'] = df['sampling_date'] + pd.to_timedelta(df['num_days'], unit='D')
  3. df['future_date'] = pd.to_datetime(df['future_date']).dt.strftime('%Y-%m-%d')
  4. df['future_date'] = df['future_date'].astype(np.str)
  5. df['future_date'] = np.where(df['num_days'] <= 0, 0, df['future_date'])

对于df['num_days']列,其值如下:[0, 866, 729, 48357555, 567, 478]

我正在尝试在Unix服务器上运行此代码。请帮助我解决这个问题。

英文:

I am trying to calculate future dates by adding a column with number of days df['num_days'] to another column df["sampling_date"] but getting Overflow in int64 addition.
Source code-

  1. df[&#39;sampling_date&#39;]=pd.to_datetime(df[&#39;sampling_date&#39;], errors=&#39;coerce&#39;)
  2. df[&#39;future_date&#39;] = df[&#39;sampling_date&#39;] + pd.to_timedelta(df[&#39;num_days&#39;], unit=&#39;D&#39;)
  3. df[&#39;future_date&#39;] = pd.to_datetime(df[&#39;future_date&#39;]).dt.strftime(&#39;%Y-%m-%d&#39;)
  4. df[&#39;future_date&#39;] = df[&#39;future_date&#39;].astype(np.str)
  5. df[&#39;future_date&#39;] = np.where(df[&#39;num_days&#39;]&lt;=0,0, df[&#39;future_date&#39;])

for column df['num_days'], the values are as follows [0, 866, 729, 48357555, 567, 478]

I am trying to run this in unix server. Please help me resolving it.

答案1

得分: 0

问题在于这个数值:48357555

您可以创建一个简单的函数如下所示,以在出错时返回NaT

  1. import numpy as np
  2. import pandas as pd
  3. # 这是一个示例数据框
  4. df = pd.DataFrame({
  5. 'sampling_date': ['2022-01-01', '2022-02-01', '2022-03-01', '2022-04-01', '2022-05-01', '2022-06-01'],
  6. 'num_days': [0, 866, 729, 48357555, 567, 478]
  7. })
  8. df['sampling_date'] = pd.to_datetime(df['sampling_date'], errors='coerce')
  9. def calculate_future_date(row):
  10. try:
  11. return row['sampling_date'] + pd.to_timedelta(row['num_days'], unit='D')
  12. except:
  13. return pd.NaT
  14. # 对每一行应用函数
  15. df['future_date'] = df.apply(calculate_future_date, axis=1)
  16. df['future_date'] = np.where(df['num_days'] <= 0, df['sampling_date'], df['future_date'])
  17. df['future_date'] = df['future_date'].dt.strftime('%Y-%m-%d').replace(pd.NaT, '0').astype(str)
  18. print(df)
  1. sampling_date num_days future_date
  2. 0 2022-01-01 0 2022-01-01
  3. 1 2022-02-01 866 2024-06-16
  4. 2 2022-03-01 729 2024-02-28
  5. 3 2022-04-01 48357555 0
  6. 4 2022-05-01 567 2023-11-19
  7. 5 2022-06-01 478 2023-09-22
英文:

The issue is this value: 48357555

You can create a simple function as shown below to return NaT if error is thrown:

  1. import numpy as np
  2. import pandas as pd
  3. # Here is an example df
  4. df = pd.DataFrame({
  5. &#39;sampling_date&#39;: [&#39;2022-01-01&#39;, &#39;2022-02-01&#39;, &#39;2022-03-01&#39;, &#39;2022-04-01&#39;, &#39;2022-05-01&#39;, &#39;2022-06-01&#39;],
  6. &#39;num_days&#39;: [0, 866, 729, 48357555, 567, 478]
  7. })
  8. df[&#39;sampling_date&#39;] = pd.to_datetime(df[&#39;sampling_date&#39;], errors=&#39;coerce&#39;)
  9. def calculate_future_date(row):
  10. try:
  11. return row[&#39;sampling_date&#39;] + pd.to_timedelta(row[&#39;num_days&#39;], unit=&#39;D&#39;)
  12. except:
  13. return pd.NaT
  14. # Apply the function to each row
  15. df[&#39;future_date&#39;] = df.apply(calculate_future_date, axis=1)
  16. df[&#39;future_date&#39;] = np.where(df[&#39;num_days&#39;] &lt;= 0, df[&#39;sampling_date&#39;], df[&#39;future_date&#39;])
  17. df[&#39;future_date&#39;] = df[&#39;future_date&#39;].dt.strftime(&#39;%Y-%m-%d&#39;).replace(pd.NaT, &#39;0&#39;).astype(str)
  18. print(df)
  19. sampling_date num_days future_date
  20. 0 2022-01-01 0 2022-01-01
  21. 1 2022-02-01 866 2024-06-16
  22. 2 2022-03-01 729 2024-02-28
  23. 3 2022-04-01 48357555 0
  24. 4 2022-05-01 567 2023-11-19
  25. 5 2022-06-01 478 2023-09-22

huangapple
  • 本文由 发表于 2023年6月1日 23:30:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定