Skip to content Skip to sidebar Skip to footer

Pandas Date_range() For Middle And End Of Month

I can specify the date range of month ends using import pandas as pd monthend_range = pd.date_range(datetime.date(2017,12,10), datetime.date(2018,2,2), freq='BM') Is there a strai

Solution 1:

Idea is create business day range for each value with omit first and select value in the middle, last use Index.union for join togetehr:

a = [] 
for x in monthend_range[1:]:
    r = pd.date_range(x.to_period('m').to_timestamp(), x, freq='B')
    a.append(r[len(r)//2])
print (a)
[Timestamp('2018-01-16 00:00:00', freq='B')]

out = monthend_range.union(a)
print (out)           
DatetimeIndex(['2017-12-29', '2018-01-16', '2018-01-31'], dtype='datetime64[ns]', freq=None)

Post a Comment for "Pandas Date_range() For Middle And End Of Month"