Dynamic Level in Pandas `date_range`

Issue

I wanted to make pandas date_range dynamic.

So, let x = 30 — This can take any values.

pd.date_range(start='2020-01-01', end='2020-01-31', freq='xH')

'30H' is giving result, but not 'x30'.

Can someone please guide me how to make it dynamic?

Solution

This is basic string formatting, here are two examples:

x = 30
pd.date_range(start='2020-01-01', end='2020-01-31', freq='%dH' % x)
x = 30
pd.date_range(start='2020-01-01', end='2020-01-31', freq=f'{x}H')

Answered By – mozway

Answer Checked By – Mary Flores (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.