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
  • 1
  • 11
  • 20
  • 40
Beta
  • 1,638
  • 5
  • 33
  • 67
  • 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
  • 194,879
  • 13
  • 39
  • 75