Looking for a sophisticated way loop through date range and run only on every Sunday.
import pandas
for da in pandas.date_range("20181101","20181217",freq='B'):
runJob()
But are there some options which runs the loop for every Sunday in the date range ?
Set freq='W-SUN'
to specify Sundays only:
pd.date_range("20181101", "20181217", freq='W-SUN')
DatetimeIndex(['2018-11-04', '2018-11-11', '2018-11-18', '2018-11-25',
'2018-12-02', '2018-12-09', '2018-12-16'],
dtype='datetime64[ns]', freq='W-SUN')
Doing a little crosschecking...
dt = pd.date_range("20181101", "20181217", freq='W-SUN')
assert dt.strftime('%A').unique().tolist() == ['Sunday']
You can actually specify the anchor to be any day of the week, as long as the offset specified is in the form "W-<day_of_week>"
.
In this case, the default is actually Sunday, so just using freq='W'
is sufficient.
pd.date_range("20181101","20181217", freq='W')
See the docs section on Anchored Date Offsets for more information.
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.