Search code examples
pythonpandasdatetimetimedelta

Incrementing datetime object by with a range


I've got the following column in my pandas dataframe. My intention is to increment each hour by +1 within a certain range(0,10). I could easily say: df['time'] = np.arange(0,10) to get those values, however I lose the datetime formatting.

Datetime column in pandas dataframe

To create the the datetime, I used the following code:

df['time'] = (
    datetime(
        start.year, start.month, start.day, hour=1, tzinfo=timezone.utc
    ))

I am not opposed to a method to just iterate the dataframe and increment them by 1, or create it properly given the range. Any advice would be appreciated.


Solution

  • You can use timedelta arithmetic. Or use a date_range. Ex:

    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame()
    num = 10
    start = pd.Timestamp("now").floor("d") # current date, h/m/s set zero
    
    # add range as timedelta to start date:
    df["Time"] = start + pd.to_timedelta(np.arange(num), unit="H")
    # even simpler: date_range
    df["Time2"] = pd.date_range(start, periods=num, freq="H")
    
    
    df
                     Time               Time2
    0 2022-10-06 00:00:00 2022-10-06 00:00:00
    1 2022-10-06 01:00:00 2022-10-06 01:00:00
    2 2022-10-06 02:00:00 2022-10-06 02:00:00
    3 2022-10-06 03:00:00 2022-10-06 03:00:00
    4 2022-10-06 04:00:00 2022-10-06 04:00:00
    5 2022-10-06 05:00:00 2022-10-06 05:00:00
    6 2022-10-06 06:00:00 2022-10-06 06:00:00
    7 2022-10-06 07:00:00 2022-10-06 07:00:00
    8 2022-10-06 08:00:00 2022-10-06 08:00:00
    9 2022-10-06 09:00:00 2022-10-06 09:00:00