如何修复 – int64 加法溢出

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

How to fix - Overflow in int64 addition

问题

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

df['sampling_date'] = pd.to_datetime(df['sampling_date'], errors='coerce')
df['future_date'] = df['sampling_date'] + pd.to_timedelta(df['num_days'], unit='D')
df['future_date'] = pd.to_datetime(df['future_date']).dt.strftime('%Y-%m-%d')
df['future_date'] = df['future_date'].astype(np.str)
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-

df[&#39;sampling_date&#39;]=pd.to_datetime(df[&#39;sampling_date&#39;], errors=&#39;coerce&#39;)
df[&#39;future_date&#39;] = df[&#39;sampling_date&#39;] + pd.to_timedelta(df[&#39;num_days&#39;], unit=&#39;D&#39;)
df[&#39;future_date&#39;] = pd.to_datetime(df[&#39;future_date&#39;]).dt.strftime(&#39;%Y-%m-%d&#39;)
df[&#39;future_date&#39;] = df[&#39;future_date&#39;].astype(np.str)
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

import numpy as np
import pandas as pd

# 这是一个示例数据框
df = pd.DataFrame({
    'sampling_date': ['2022-01-01', '2022-02-01', '2022-03-01', '2022-04-01', '2022-05-01', '2022-06-01'],
    'num_days': [0, 866, 729, 48357555, 567, 478]
})

df['sampling_date'] = pd.to_datetime(df['sampling_date'], errors='coerce')

def calculate_future_date(row):
    try:
        return row['sampling_date'] + pd.to_timedelta(row['num_days'], unit='D')
    except:
        return pd.NaT

# 对每一行应用函数
df['future_date'] = df.apply(calculate_future_date, axis=1)
df['future_date'] = np.where(df['num_days'] <= 0, df['sampling_date'], df['future_date'])
df['future_date'] = df['future_date'].dt.strftime('%Y-%m-%d').replace(pd.NaT, '0').astype(str)
print(df)
   sampling_date  num_days future_date
0    2022-01-01         0  2022-01-01
1    2022-02-01       866  2024-06-16
2    2022-03-01       729  2024-02-28
3    2022-04-01  48357555           0
4    2022-05-01       567  2023-11-19
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:

import numpy as np
import pandas as pd

# Here is an example df
df = pd.DataFrame({
    &#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;],
    &#39;num_days&#39;: [0, 866, 729, 48357555, 567, 478]
})

df[&#39;sampling_date&#39;] = pd.to_datetime(df[&#39;sampling_date&#39;], errors=&#39;coerce&#39;)

def calculate_future_date(row):
    try:
        return row[&#39;sampling_date&#39;] + pd.to_timedelta(row[&#39;num_days&#39;], unit=&#39;D&#39;)
    except:
        return pd.NaT

# Apply the function to each row
df[&#39;future_date&#39;] = df.apply(calculate_future_date, axis=1)
df[&#39;future_date&#39;] = np.where(df[&#39;num_days&#39;] &lt;= 0, df[&#39;sampling_date&#39;], df[&#39;future_date&#39;])
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)
print(df)


  sampling_date  num_days future_date
0    2022-01-01         0  2022-01-01
1    2022-02-01       866  2024-06-16
2    2022-03-01       729  2024-02-28
3    2022-04-01  48357555           0
4    2022-05-01       567  2023-11-19
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:

确定