我有以下df
,
inv_date inv_id
2017-10-01 100117
2018-04-02 040218
2018-05-06 060518
其中inv_date
是datetime
dtype
,而inv_id
是str
;我想根据以下inv_id
将datetime
转换为formats
,
formats = {'%m%d%y': 6, '%d%m%y': 6}
L = [pd.to_datetime(s.str[:v], format=k, errors='coerce') for k, v in formats.items()]
df1 = pd.concat(L, axis=1, keys=[s.name + '_' + str(i) for i, s in zip(count(), L)])
df1 = df.apply(lambda x: x.where(x.between('2000-01-01', datetime.now())))
我想创建一个布尔列dummy_inv_id
,如果任何非NaT转换的True
在datetime
的+/- 180天内,则将其设置为inv_date
,
df1 = df1.assign(inv_date=df['inv_date'])
df1['inv_id_1'].between(df1['inv_date'] - Timedelta(180, unit='d'), df1['inv_date'] + Timedelta(180, unit='d'))
df1['inv_id_2'].between(df1['inv_date'] - Timedelta(180, unit='d'), df1['inv_date'] + Timedelta(180, unit='d'))
我想知道如何集中考虑inv_id_1
中的所有日期时间列(inv_id_2
和df1
),因此,如果有人在inv_date +/- 180 days
之间,则分配true
到df
对应的日期时间;
结果df
如下,
inv_date inv_id dummy_inv_id
2017-10-01 100117 true
2018-04-02 040218 true
2018-05-06 060518 true
答案 0 :(得分:1)
您可以使用np.logical_or.reduce
:
a = df1['inv_id_1'].between(df1['inv_date'] - pd.Timedelta(180, unit='d'), df1['inv_date'] + pd.Timedelta(180, unit='d'))
b = df1['inv_id_2'].between(df1['inv_date'] - pd.Timedelta(180, unit='d'), df1['inv_date'] + pd.Timedelta(180, unit='d'))
c = [a,b]
df['dummy_inv_id'] = np.logical_or.reduce(c)
print (df)
inv_date inv_id dummy_inv_id
0 2017-10-01 100117 True
1 2018-04-02 40218 True
2 2018-05-06 60518 True