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 Programming skills with w3resource's quiz.
Python: Tips of the Day
Find current directory and file's directory:
To get the full path to the directory a Python file is contained in, write this in that file:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
To get the current working directory use
import os cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The os and os.path modules.
- The __file__ constant
- os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
- os.path.dirname(path) (returns "the directory name of pathname path")
- os.getcwd() (returns "a string representing the current working directory")
- os.chdir(path) ("change the current working directory to path")
Ref: https://bit.ly/3fy0R6m
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework