w3resource

Pandas: Combine together day and intraday offsets

Pandas Time Series: Exercise-24 with Solution

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')

Python Code Editor:


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

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.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Merging two dicts in Python 3.5+ with a single expression

Example:

# How to merge two dictionaries
# in Python 3.5+

x = {'p': 1, 'q': 3}
y = {'q': 5, 'r': 8}

z = {**x, **y}

z
{'r': 4, 'p': 1, 'q': 3}

z = dict(x, **y)
print(z)

Output:

{'p': 1, 'q': 5, 'r': 8}