Skip to content
Advertisement

split a string representation with ranges into a list of dates

I have this pandas dataframe column with timeranges (02.07.2021 – 07.07.2021 ) and single days (04.08.2021) as a list.

Dates
‘02.07.2021 – 07.07.2021 , 04.08.2021, 19.06.2021 – 21.06.2021’
‘13.02.2021 – 15.02.2021 , 03.03.2021 ‘
NaN
NaN

I want this:

Dates
02.07.2021, 03.07.2021, 04.07.2021, 05.07.2021, 06.07.2021, 07.07.2021, 04.08.2021, 19.06.2021, 20.06.2021, 21.06.2021
13.02.2021, 14.02.2021, 15.02.2021, 03.03.2021
NaN
NaN

So basically I want every day within every time range in a list.

Is there a pandas solution for that? (I’ve tried to solve it with range and iloc but this is way to mucht for this “simple” task).

Bonus: The dates should have the datetime dytpe (pd.to_datetime())

Advertisement

Answer

You can use a list comprehension:

JavaScript

output:

JavaScript

Used input:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement