python - 我收到零和​​负日 python 和 pandas csv 文件

标签 python pandas datetime

是否可以使用pandas库替换csv文件中的那些零和负数天,我只需要用零或三十天替换任何负数天

这是我的示例:

import datetime
from dateutil.relativedelta import relativedelta
import pandas as pd
import numpy as np

df = pd.read_csv("csv_data.csv", encoding="UTF-8",
    parse_dates = [
        "firstdate",
        "lastdate",
    ], dayfirst=True
)
diff = pd.firstdate - pd.lastdate
#When i skip the following loop
"""for d in diff:
    if d < 0:
        df['thedif'] = 0
    else:
        df['thedif'] = diff"""
#and add this line below

  df['thedif'] = diff
index firstdate   lastdate   thedif
0     2021-03-02  2021-04-02 -31days
1     2021-04-02  2021-03-02 31days
2     2021-03-03  2021-03-03 0days

但是如果我们在 numpy 中使用 where 关键字,我们会得到

Invalid comparison between dtype=timedelta64[ns] and int

最佳答案

Tou 可以通过 Series.dt.days 将天数转换为整数:

diff = (df.firstdate - df.lastdate).dt.days

df['thedif'] = np.where(diff < 0, 0, diff)

或者通过pd.Timedelta(0)进行比较:

diff = df.firstdate - df.lastdate

df['thedif'] = np.where(diff < pd.Timedelta(0), pd.Timedelta(0), diff)

关于python - 我收到零和​​负日 python 和 pandas csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69667562/

相关文章:

python-3.x - pandas DatetimeIndex 到 matplotlib x-ticks

php - 将到期时间插入数据库并在另一页上倒计时

datetime - 如果我想使用过去/ future 数百万年的日期和时间来工作,我会怎么做?

python - 在目录结构中向上移动

python - 比较 Pandas Dataframe 中行内的列值

python - pandas 中的条件滚动计算

python - 通过将某些列转置为行来 reshape pandas 数据框

python - 如何聚合扩展值 - python - pandas

python - 将 pandas 数据框转换为具有新键名的字典

python - SPA (Ember) 身份验证与后端