Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateOffset Panda substraction

I have a dataFrame

[in] MyDates
[out]  
2017-04-04        -5.0
2017-04-03        -5.0
2017-03-31        -4.0
2017-03-30        -6.0
2017-03-29        -5.0
2017-03-28        -5.0

Each numbers correspond of how much day I should add or remove from the corresponding date. I want to crate a new column with the index date minus the number of days which are in the column one. I know I can do it with DateOffset but I can't figure out how...

Thank you!

like image 568
user6457870 Avatar asked Mar 13 '26 00:03

user6457870


2 Answers

You can convert column to TimedeltaIndex or to_timedelta and add (+) or substract (-) values:

df['new'] = df.index - pd.TimedeltaIndex(df['col'], unit='d')
print (df)
            col        new
2017-04-04 -5.0 2017-04-09
2017-04-03 -5.0 2017-04-08
2017-03-31 -4.0 2017-04-04
2017-03-30 -6.0 2017-04-05
2017-03-29 -5.0 2017-04-03
2017-03-28 -5.0 2017-04-02

Or:

df['new'] = df.index + pd.to_timedelta(df['col'], unit='d')
print (df)
            col        new
2017-04-04 -5.0 2017-03-30
2017-04-03 -5.0 2017-03-29
2017-03-31 -4.0 2017-03-27
2017-03-30 -6.0 2017-03-24
2017-03-29 -5.0 2017-03-24
2017-03-28 -5.0 2017-03-23

If Series as input add to_frame:

df = s.to_frame('date')
df['new'] = df.index - pd.TimedeltaIndex(df['date'], unit='d')
print (df)
            date        new
2017-04-04  -5.0 2017-04-09
2017-04-03  -5.0 2017-04-08
2017-03-31  -4.0 2017-04-04
2017-03-30  -6.0 2017-04-05
2017-03-29  -5.0 2017-04-03
2017-03-28  -5.0 2017-04-02
like image 157
jezrael Avatar answered Mar 14 '26 13:03

jezrael


IIUC you want to construct a TimedeltaIndex and add this:

In [173]:    
df.index + pd.TimedeltaIndex(df['days'], unit='d')

Out[173]:
DatetimeIndex(['2017-03-30', '2017-03-29', '2017-03-27', '2017-03-24',
               '2017-03-24', '2017-03-23'],
              dtype='datetime64[ns]', freq=None)

If it's a column you just do df['Dates'] + pd.TimedeltaIndex(df['days'], unit='d')

In [176]:
df['offset_date'] = df['Dates'] + pd.TimedeltaIndex(df['days'], unit='d')
df

Out[176]:
       Dates  days offset_date
0 2017-04-04  -5.0  2017-03-30
1 2017-04-03  -5.0  2017-03-29
2 2017-03-31  -4.0  2017-03-27
3 2017-03-30  -6.0  2017-03-24
4 2017-03-29  -5.0  2017-03-24
5 2017-03-28  -5.0  2017-03-23

If it's the index and you want to add as a column it's pretty much the same op:

In [180]:    
df['offset_date'] = df.index + pd.TimedeltaIndex(df['days'], unit='d')
df

Out[180]:
            days offset_date
Dates                       
2017-04-04  -5.0  2017-03-30
2017-04-03  -5.0  2017-03-29
2017-03-31  -4.0  2017-03-27
2017-03-30  -6.0  2017-03-24
2017-03-29  -5.0  2017-03-24
2017-03-28  -5.0  2017-03-23
like image 28
EdChum Avatar answered Mar 14 '26 14:03

EdChum