How to print date starting from the given date for n number of days using Pandas?
Last Updated :
23 Jan, 2023
In this article, we will print all the dates starting from the given date for n number days. It can be done using the pandas.date_range() function. This function is used to get a fixed frequency DatetimeIndex.
Syntax: pandas.date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, closed=None, **kwargs)
Approach:
- Import pandas module
- Create a parameter function for computing the Date series between starting date and periods.
- Generate sequences of dates between starting and periods with pandas.date_range() within the function
- Store into the pandas series within the function
- Return the pandas series.
Below is the implementation.
Python3
import pandas as pd
def Time_series(date, per):
date_series = pd.date_range(date, periods = per)
Result = pd.Series(date_series)
print (Result)
date = "2020-03-01"
per = 10
Time_series(date, per)
|
Output :
0 2020-03-01
1 2020-03-02
2 2020-03-03
3 2020-03-04
4 2020-03-05
5 2020-03-06
6 2020-03-07
7 2020-03-08
8 2020-03-09
9 2020-03-10
dtype: datetime64[ns]
Time Complexity:
Time complexity of this code is O(1). This algorithm does not involve any loops, so it just takes constant time to execute.
Space Complexity:
Space complexity of this code is O(1). As this code does not involve any extra space, it requires constant space.
Share your thoughts in the comments
Please Login to comment...