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()
)
CodePudding user response:
You can use a list comprehension:
pd.Series([[str(d.strftime('%d.%m.%Y'))
for x in re.split('\s*,\s*', s)
for d in (pd.date_range(*map(lambda d: pd.to_datetime(d, format='%d.%m.%Y'),
x.split(' - ')),
freq='D')
if ' - ' in x else [pd.to_datetime(x.strip(), format='%d.%m.%Y')])]
for s in df['Dates']])
output:
0 [02.07.2021, 03.07.2021, 04.07.2021, 05.07.202...
1 [13.02.2021, 14.02.2021, 15.02.2021, 03.03.2021]
dtype: object
Used input:
d = ['02.07.2021 - 07.07.2021 , 04.08.2021, 19.06.2021 - 21.06.2021',
'13.02.2021 - 15.02.2021 , 03.03.2021 ']
df = pd.DataFrame({'Dates': d})