Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: how to get difference between two months from integers?

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
like image 426
emax Avatar asked Mar 07 '26 12:03

emax


2 Answers

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')
like image 127
jezrael Avatar answered Mar 09 '26 02:03

jezrael


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)

Output df:

   A  NUM_YYYYMM  deltaT
0  a      201605       7
1  b      201602      10
2  c      201603       9
3  d      201601      11
like image 35
ALollz Avatar answered Mar 09 '26 00:03

ALollz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!