Hello everyone, I encountered a date looping problem on JupyterLab, the problem is as shown in the attached picture:
It is very strange that the red circle of B should be displayed the same as the red circle of A. Why is week 6 missing?
And "if d.weekday() in [5, 6]: dates.remove(d)". It should be 5 and 6 removed, how can there be 4/3 and 4/10?
I have restarted the core and the result is the same. It's amazing...
CodePudding user response:
You should not modify the list dates
while iterating over it. Please check Strange result when removing item from a list while iterating over it for more details.
To removes dates which are Saturdays or Sundays, try this:
import pandas as pd
from datetime import datetime
from datetime import timedelta
d = datetime(2022, 4, 1)
dates = [d timedelta(days=idx) for idx in range(10)]
for d in dates:
print(f'{d.date()} {d.weekday()}' )
new_dates = dates.copy()
for d in dates:
if d.weekday() in [5, 6]:
new_dates.remove(d)
print("Removed Saturdays and Sundays!")
for d in new_dates:
print(f'{d.date()} {d.weekday()}' )
This gives:
2022-04-01 4
2022-04-02 5
2022-04-03 6
2022-04-04 0
2022-04-05 1
2022-04-06 2
2022-04-07 3
2022-04-08 4
2022-04-09 5
2022-04-10 6
Removed Saturdays and Sundays!
2022-04-01 4
2022-04-04 0
2022-04-05 1
2022-04-06 2
2022-04-07 3
2022-04-08 4