0

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?

Sunderam Dubey
  • 2,294
  • 9
  • 12
  • 22
Beta
  • 1,480
  • 5
  • 31
  • 63
  • You can use [string formatting](https://stackoverflow.com/questions/517355/string-formatting-in-python). – Henry Yik Aug 18 '21 at 14:29

1 Answers1

1

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')
mozway
  • 81,317
  • 8
  • 19
  • 49