Skill - Split a time interval using pandas date_range
Skills Required
- Setup python development environment
- Basic Printing in Python
- Commenting in Python
- Managing Variables in python
- Pandas DataFrame Basics
- datetime module in Python
Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision
pandas date_range
function can be used to split a time range based on a variety of frequency options
split time interval by a specified time period
use the freq
parameter of date_range
function
import pandas as pd
import datetime as dt
startDt = dt.datetime(2020,1,1)
endDt = dt.datetime(2020,1,20)
splitDates = pd.date_range(startDt, endDt, freq='D').tolist()
print(splitDates)
splitDates = pd.date_range(startDt, endDt, freq=dt.timedelta(days=3)).tolist()
print(splitDates)
split time interval into a fixed number of intervals
use the periods
parameter of date_range
function
import pandas as pd
import datetime as dt
startDt = dt.datetime(2020,1,1)
endDt = dt.datetime(2020,1,20)
splitDates = pd.date_range(startDt, endDt, periods=15).tolist()
print(splitDates)
Video
You can the video on this post here
Online Interpreter
Although we recommend to practice the above examples in Visual Studio Code, you can run these examples online at https://www.tutorialspoint.com/execute_python_online.php
Comments
Post a Comment