I have a dataframe with two date columns X and Y. I want the list of dates between these two columns. I have tried pd.date_range. but it doesnt't work. How can I achieve this?
Thanks
I have a dataframe with two date columns X and Y. I want the list of dates between these two columns. I have tried pd.date_range. but it doesnt't work. How can I achieve this?
Thanks
You can retrieve date range using the mentioned pd.date_range as follows:
import pandas as pd
from datetime import date, timedelta
start_date, end_date = date(2020, 9, 15), date(2020, 11 , 1)
date_range = pd.date_range(start_date, end_date - timedelta(days = 1), freq='d')
Which gives the output:
DatetimeIndex(['2020-09-15', '2020-09-16', '2020-09-17', '2020-09-18',
'2020-09-19', '2020-09-20', '2020-09-21', '2020-09-22',
'2020-09-23', '2020-09-24', '2020-09-25', '2020-09-26',
'2020-09-27', '2020-09-28', '2020-09-29', '2020-09-30',
'2020-10-01', '2020-10-02', '2020-10-03', '2020-10-04',
'2020-10-05', '2020-10-06', '2020-10-07', '2020-10-08',
'2020-10-09', '2020-10-10', '2020-10-11', '2020-10-12',
'2020-10-13', '2020-10-14', '2020-10-15', '2020-10-16',
'2020-10-17', '2020-10-18', '2020-10-19', '2020-10-20',
'2020-10-21', '2020-10-22', '2020-10-23', '2020-10-24',
'2020-10-25', '2020-10-26', '2020-10-27', '2020-10-28',
'2020-10-29', '2020-10-30', '2020-10-31'],
dtype='datetime64[ns]', freq='D')
You can check if a date is in a given range using isin():
date_range.isin(['2020-10-29']).any()
It can be converted to string:
date_range1 = [str(d.date()) for d in date_range]
Which gives the output:
['2020-09-15', '2020-09-16', '2020-09-17', '2020-09-18', '2020-09-19', '2020-09-20', '2020-09-21', '2020-09-22', '2020-09-23', '2020-09-24', '2020-09-25', '2020-09-26', '2020-09-27', '2020-09-28', '2020-09-29', '2020-09-30', '2020-10-01', '2020-10-02', '2020-10-03', '2020-10-04', '2020-10-05', '2020-10-06', '2020-10-07', '2020-10-08', '2020-10-09', '2020-10-10', '2020-10-11', '2020-10-12', '2020-10-13', '2020-10-14', '2020-10-15', '2020-10-16', '2020-10-17', '2020-10-18', '2020-10-19', '2020-10-20', '2020-10-21', '2020-10-22', '2020-10-23', '2020-10-24', '2020-10-25', '2020-10-26', '2020-10-27', '2020-10-28', '2020-10-29', '2020-10-30', '2020-10-31']