简体   繁体   中英

How to fill the missing date values in data frame by generating the range of dates between two time periods

I have a dataframe in which few date values for date column are missing and all other data values are present. I have to fill the date values. The date values must be in between the two values. For eg fill the missing dates between the 13/12/1988 to 20/01/2012.

Please help me.

Many Thanks, Aniket Patel

You can use the datetime.timedelta function to get a time offset. This can be used to generate every date from time a to time b .

a = datetime.datetime(1988, 12, 13)
b = datetime.datetime(2012, 1, 20)

# get all date between a and b
date_list = [a + datetime.timedelta(days=x) for x in range((b - a).days)]

# format dates for pretty printing
date_str_list = ['{}/{}/{}'.format(d.year, d.month, d.day) for d in date_list]

# print first five dates
print(date_str_list[:5])

Convert your date to timedeltas since epoche in seconds. Call pandas interpolate function for this series. Convert back if you want to have the date-representation

df = pd.DataFrame(dict(date = pd.date_range('20130101',periods=10)))
df.iloc[5]['date'] = ''

        date
0 2013-01-01
1 2013-01-02
2 2013-01-03
3 2013-01-04
4 2013-01-05
5        NaT
6 2013-01-07
7 2013-01-08
8 2013-01-09
9 2013-01-10

in_sec = pd.to_timedelta(df['date'], unit='second') / np.timedelta64(1, 's')

result = in_sec.interpolate()

result = pd.to_datetime(result ,unit='s')

0   2013-01-01
1   2013-01-02
2   2013-01-03
3   2013-01-04
4   2013-01-05
5   2013-01-06
6   2013-01-07
7   2013-01-08
8   2013-01-09
9   2013-01-10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM