如何从给定登录日期之前过去发生的 timedelta 值中获取确切日期?

How do I get exact dates from timedelta values having taken place in the past before given logon dates?

我正在尝试将一列 timedelta 值转换为表示过去的确切日期,并与登录环境的多个用户的特定终止日期进行比较。如何为 ID # 122770、123526 等显示 2020-09-28 之前 -132 天 +21:38:00 的确切日期

Fighting timedelta 是一项艰巨的工作,因此非常感谢您提供的任何支持....;o))) 非常感谢您的支持。 BR @Hubsandspokes

我有这个:

User Name col_1                  col_2     
122770    -132 days +21:38:00    2020-09-28  1
          -122 days +00:41:00    2020-09-28  1
123526    -30 days +12:04:00     2020-06-21  1
          -29 days +16:39:00     2020-06-21  1
          -27 days +18:16:00     2020-06-21  1
...                                         ..
201685    -131 days +21:21:00    2020-10-08  1
202047    -106 days +10:14:00    2020-09-14  1
202076    -132 days +10:22:00    2020-10-09  1
          -132 days +14:46:00    2020-10-09  1
          -131 days +21:21:00    2020-10-09 

我想要这个

User Name col_1                  term_date     date_of_latest_logon (i.e. term_date - col_1 as a date)     
122770    -132 days +21:38:00    2020-09-28    exact date derived from col_1
          -122 days +00:41:00    2020-09-28    exact date derived from col_1
123526    -30 days +12:04:00     2020-06-21    exact date derived from col_1
          -29 days +16:39:00     2020-06-21    exact date derived from col_1
          -27 days +18:16:00     2020-06-21    exact date derived from col_1
...                                        
201685    -131 days +21:21:00    2020-10-08    exact date derived from col_1
202047    -106 days +10:14:00    2020-09-14    exact date derived from col_1
202076    -132 days +10:22:00    2020-10-09    exact date derived from col_1
          -132 days +14:46:00    2020-10-09    exact date derived from col_1
          -131 days +21:21:00    2020-10-09    exact date derived from col_1

试过这个:

df['col_1'] = df['col_1'].apply(lambda x: x.date())
print(df['col_1'])

但得到了以下信息:

Traceback (most recent call last):
  File "C:/..../FFA_Initial_Data_Insight_ADFS.py", line 1319, in <module>
    df['col_1'] = df['col_1'].apply(lambda x: x.date())
  File "C:\....\venv\lib\site-packages\pandas\core\series.py", line 4200, in apply
    mapped = lib.map_infer(values, f, convert=convert_dtype)
  File "pandas\_libs\lib.pyx", line 2401, in pandas._libs.lib.map_infer
  File "C:/..../FFA_Initial_Data_Insight_ADFS.py", line 1319, in <lambda>
    df['col_1'] = df['col_1'].apply(lambda x: x.date())
AttributeError: 'Timedelta' object has no attribute 'date'

假设 col_1 中显示的时间增量引用 col_2 中显示的日期,确保转换为适当的类型,以便您可以进行日期时间计算。例如:

import pandas as pd
# dummy df:
df = pd.DataFrame({"col_1": ["-132 days +21:38:00", "-122 days +00:41:00", "-30 days +12:04:00"],
                   "col_2": ["2020-09-28", "2020-09-28", "2020-06-21"]})

# make sure timdelta is timedelta and datetime is datetime dtype:
df['col_1'] = pd.to_timedelta(df['col_1'])
df['col_2'] = pd.to_datetime(df['col_2'])

# now you can do calculations:
df['col_3'] = df['col_2'] + df['col_1']
# df['col_3']
# 0   2020-05-19 21:38:00
# 1   2020-05-29 00:41:00
# 2   2020-05-22 12:04:00
# Name: col_3, dtype: datetime64[ns]