w3resource

Pandas: Combine together day and intraday offsets


24. Day and Intraday Offsets

Write a Pandas program to generate time series combining day and intraday offsets intervals.

Sample Solution:

Python Code :

import pandas as pd
dateset1 = pd.date_range('2029-01-01 00:00:00', periods=20, freq='3h10min')
print("Time series with frequency 3h10min:")
print(dateset1)
dateset2 = pd.date_range('2029-01-01 00:00:00', periods=20, freq='1D10min20U')
print("\nTime series with frequency 1 day 10 minutes and 20 microseconds:")
print(dateset2)

Sample Output:

Time series with frequency 3h10min:
DatetimeIndex(['2029-01-01 00:00:00', '2029-01-01 03:10:00',
               '2029-01-01 06:20:00', '2029-01-01 09:30:00',
               '2029-01-01 12:40:00', '2029-01-01 15:50:00',
               '2029-01-01 19:00:00', '2029-01-01 22:10:00',
               '2029-01-02 01:20:00', '2029-01-02 04:30:00',
               '2029-01-02 07:40:00', '2029-01-02 10:50:00',
               '2029-01-02 14:00:00', '2029-01-02 17:10:00',
               '2029-01-02 20:20:00', '2029-01-02 23:30:00',
               '2029-01-03 02:40:00', '2029-01-03 05:50:00',
               '2029-01-03 09:00:00', '2029-01-03 12:10:00'],
              dtype='datetime64[ns]', freq='190T')

Time series with frequency 1 day 10 minutes and 20 microseconds:
DatetimeIndex([       '2029-01-01 00:00:00', '2029-01-02 00:10:00.000020',
               '2029-01-03 00:20:00.000040', '2029-01-04 00:30:00.000060',
               '2029-01-05 00:40:00.000080', '2029-01-06 00:50:00.000100',
               '2029-01-07 01:00:00.000120', '2029-01-08 01:10:00.000140',
               '2029-01-09 01:20:00.000160', '2029-01-10 01:30:00.000180',
               '2029-01-11 01:40:00.000200', '2029-01-12 01:50:00.000220',
               '2029-01-13 02:00:00.000240', '2029-01-14 02:10:00.000260',
               '2029-01-15 02:20:00.000280', '2029-01-16 02:30:00.000300',
               '2029-01-17 02:40:00.000320', '2029-01-18 02:50:00.000340',
               '2029-01-19 03:00:00.000360', '2029-01-20 03:10:00.000380'],
              dtype='datetime64[ns]', freq='87000000020U')

For more Practice: Solve these Related Problems:

  • Write a Pandas program to create a time-series by combining daily dates with random intraday minute offsets and then sort the series.
  • Write a Pandas program to generate a series of timestamps by adding both day and second offsets to a base date.
  • Write a Pandas program to create a time-series with fixed daily intervals and then add variable intraday offsets, computing the time differences.
  • Write a Pandas program to combine a daily date range with random intraday offsets and then verify the resulting time differences.

Go to:


Previous: Write a Pandas program to generate sequences of fixed-frequency dates and time spans intervals.
Next: Write a Pandas program to extract the day name from a specified date. Add 2 days and 1 business day with the specified date.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.