0

I already have a list of dates in a dataframe. I would like to create a new column which calculates the number of days between these dates and 2020-06-26. First time asking question here, so apologize for phrasing poorly.

The dates column is currently an index column

Jonathan
  • 33
  • 4
  • 2
    Welcome to Stackoverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Jun 17 '20 at 07:23
  • [Please don't post images of code/data (or links to them)](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question) – jezrael Jun 17 '20 at 07:23
  • have a look at [pandas time deltas](https://pandas.pydata.org/pandas-docs/stable/user_guide/timedeltas.html) – FObersteiner Jun 17 '20 at 07:35
  • This is a clear duplicate question from here https://stackoverflow.com/a/22132649/503835 please search SO before asking a question. – eNc Jun 17 '20 at 07:41
  • 3
    Does this answer your question? [Add column with number of days between dates in DataFrame pandas](https://stackoverflow.com/questions/22132525/add-column-with-number-of-days-between-dates-in-dataframe-pandas) – eNc Jun 17 '20 at 07:41

2 Answers2

0

Use the datetime library and panda's apply function

from datetime import date

df = pd.DataFrame(
    {'date': [date(2020, 6, 10), date(2020, 6, 11), date(2020, 6, 12), date(2020, 6, 13)],
    })

def number_of_days_since(df):
    return date(2020,6, 26) - df['date']

df['days_since'] = df.apply(number_of_days_since, axis=1)

  • you don't need a `lambda` here (also no `apply`); just `df['date'] - pd.to_datetime('2020-6-26')` - if you would use pandas' datetime instead of `datetime.date`... – FObersteiner Jun 17 '20 at 07:41
0

You can make these two columns as datetime and just subtract them it will create new column with number of days(difference between two dates).

df['Date1'] = pd.to_datetime(df['Date1']) 
df['Date2'] = pd.to_datetime(df['Date2']) 
df ['days'] = df['Date1'] - df['Date2']