Reputation: 7245
I have a dataframe df
like the following:
df
A NUM_YYYYMM
0 a 201605
1 b 201602
2 c 201603
3 d 201601
where type(df['NUM_YYYYMM'])
returns int
. I want to compute the difference in months between t0=201612
and the column df['NUM_YYYYMM']
. So:
df
A NUM_YYYYMM deltaT
0 a 201605 7
1 b 201602 10
2 c 201603 9
3 d 201601 11
Upvotes: 3
Views: 647
Reputation: 59519
You can define your own subtraction using integer and modulus division given your standard format YYYYMM
def my_subtract(x, t0):
return (t0//100 - x//100)*12 + (t0%100 - x%100)
df['deltaT'] = my_subtract(df.NUM_YYYYMM, 201612)
df
: A NUM_YYYYMM deltaT
0 a 201605 7
1 b 201602 10
2 c 201603 9
3 d 201601 11
Upvotes: 2
Reputation: 862481
Convert column to_datetime
and then to month periods by to_period
, which is subtracted by Period
from t0
:
t0 = '201612'
t = pd.to_datetime(t0, format='%Y%m').to_period('m')
df['deltaT'] = t - pd.to_datetime(df['NUM_YYYYMM'], format='%Y%m').dt.to_period('m')
print (df)
A NUM_YYYYMM deltaT
0 a 201605 7
1 b 201602 10
2 c 201603 9
3 d 201601 11
If is possible change format of t0
:
t = pd.Period('2016-12')
df['deltaT'] = t - pd.to_datetime(df['NUM_YYYYMM'], format='%Y%m').dt.to_period('m')
Upvotes: 5